<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _get_api_events(self, function):
 """ Method to return a dictionary of API Events on the function :param SamResource function: Function Resource object :return dict: Dictionary of API events along with any other configuration passed to it. Example: { FooEvent: {Path: "/foo", Method: "post", RestApiId: blah, MethodSettings: {<something>}, Cors: {<something>}, Auth: {<something>}}, BarEvent: {Path: "/bar", Method: "any", MethodSettings: {<something>}, Cors: {<something>}, Auth: {<something>}}" } """


        if not (function.valid() and
                isinstance(function.properties, dict) and
                isinstance(function.properties.get("Events"), dict)
                ):
            # Function resource structure is invalid.
            return {}

        api_events = {}
        for event_id, event in function.properties["Events"].items():

            if event and isinstance(event, dict) and event.get("Type") == "Api":
                api_events[event_id] = event

        return api_events