コード例 #1
0
ファイル: service.py プロジェクト: dillonhicks/rekt
def create_api_call_func(api, verb):
    """
    From an api definition object create the related api call method
    that will validate the arguments for the api call and then
    dynamically dispatch the request to the appropriate requests module
    convenience method for the specific HTTP verb .
    """

    # Scopes some local context in which we can build
    # request functions with reflection that primed with
    # some static parameters.
    def api_call_func(self, **kwargs):

        request = api.request_classes[verb](**kwargs)
        params = dict([(k, v) for k, v in request.items() if v is not None])

        if HTTPVerb.GET == verb:
            raw_response = requests.get(api.url, params=params, **self.reqargs)

        elif HTTPVerb.POST == verb:
            raw_response = requests.post(api.url, data=params, **self.reqargs)

        else:
            raise RuntimeError("{} is not a handled http verb".format(verb))

        if raw_response.status_code != HTTPStatus.OK:
            raw_response.raise_for_status()

        # The object hook will convert all dictionaries from the json
        # objects in the response to a . attribute access
        ResponseClass = api.response_classes[verb]
        try:
            response = raw_response.json(object_hook=lambda obj: ResponseClass(obj))
        except ValueError as e:
            response = ResponseClass({"content": raw_response.content})

        return response

    method_name = api_method_name(verb, api)

    api_call_func.__name__ = method_name
    api_call_func.__doc__ = "{}\n{}".format(
        method_name, "".join(api.request_classes[verb].__doc__.splitlines(True)[1:])
    )

    return api_call_func
コード例 #2
0
ファイル: service.py プロジェクト: dillonhicks/rekt
 def _async_call_handler():
     api_method = getattr(self, api_method_name(verb, api))
     return api_method(**kwargs)