diff --git a/README.md b/README.md index 674aa4e..a3e4d35 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,9 @@ api = TestApi() posts = api.posts() post = api.post(id=posts[0]['id']) comments = api.comments(id=post['id']) -new_post_id = api.add_post(userId=1, - title='Breaking news', - body='I just got devoured.') +new_post_id = api.add_post(data={'userId': 1, + 'title': 'Breaking news', + 'body': 'I just got devoured.'}) try: post = api.post(id=new_post_id) except APIError: @@ -47,6 +47,11 @@ The init function gives details so you don't need to repeat them elsewhere, enab raising exceptions on error. You can also obtain raw string with `load_json=False` and silence errors getting None instead when they happen with `throw_on_error=False`. +When calling methods: +* `data` and `payload` kwargs will be passed to requests call as `data` and `json` parameters. +* all keyword arguments matching schema will be used in schema. +* all other kwargs will be passed to requests call as query string parameters. + ### Async usage Devourer supports asynchronous calls using concurrent.futures's ThreadPoolExecutor. API subclasses diff --git a/devourer/api.py b/devourer/api.py index 10d7a8b..dd1599f 100644 --- a/devourer/api.py +++ b/devourer/api.py @@ -105,7 +105,7 @@ def params(self): """ return self._params - def __call__(self, api, payload=None, **kwargs): + def __call__(self, api, payload=None, data=None, **kwargs): """ This method sends a request to API through invoke function from API object the method is assigned to. It calls invoke with formatted schema, additional @@ -113,6 +113,7 @@ def __call__(self, api, payload=None, **kwargs): :param kwargs: Additional parameters to be passed to remote API. :param payload: The POST body to send along with the request as JSON. + :param data: Dict or encoded string to be sent as request body. :returns: API request's result. """ params = {key: value for key, value in kwargs.items() if key not in self.params} @@ -120,7 +121,7 @@ def __call__(self, api, payload=None, **kwargs): schema = self.schema.format(**kwargs) else: schema = self.schema - return api.invoke(self.http_method, schema, params=params, payload=payload) + return api.invoke(self.http_method, schema, params=params, data=data, payload=payload) class GenericAPICreator(type): @@ -255,17 +256,18 @@ def outer_call(cls, name): """ return lambda obj, *args, **kwargs: obj.call(name, *args, **kwargs) - def invoke(self, http_method, url, params, payload): + def invoke(self, http_method, url, params, data=None, payload=None): """ This method makes a request to given API address concatenating the method path and passing along authentication data. :param http_method: http method to be used for this call. :param url: exact address to be concatenated to API address. + :param data: dict or encoded string to be sent as request body. :param payload: the payload dictionary to be sent in body of the request, encoded as JSON. :returns: response object as in requests. """ - return getattr(requests, http_method)(self.url + url, auth=self.auth, params=params, json=payload) + return getattr(requests, http_method)(self.url + url, auth=self.auth, params=params, data=data, json=payload) class GenericAPI(with_metaclass(GenericAPICreator, GenericAPIBase)): diff --git a/setup.cfg b/setup.cfg index 224a779..b88034e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,2 @@ [metadata] -description-file = README.md \ No newline at end of file +description-file = README.md diff --git a/setup.py b/setup.py index 9eb3557..3e8eb11 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='devourer', packages=['devourer'], - version='0.4.0', + version='0.4.1', install_requires=[ 'requests', 'futures', @@ -14,7 +14,7 @@ author='Bonnier Business Polska / Krzysztof Bujniewicz', author_email='racech@gmail.com', url='https://github.com/bonnierpolska/devourer', - download_url='https://github.com/bonnierpolska/devourer/tarball/0.4.0', + download_url='https://github.com/bonnierpolska/devourer/tarball/0.4.1', keywords=['api', 'generic api', 'api client'], classifiers=[] )