Skip to content

Commit

Permalink
Add support for requests' data paremeter.
Browse files Browse the repository at this point in the history
  • Loading branch information
bujniewicz committed Oct 23, 2016
1 parent 9e3268a commit c18cfee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions devourer/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,23 @@ 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
arguments and http method already calculated.
: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}
if self.params:
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):
Expand Down Expand Up @@ -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)):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[metadata]
description-file = README.md
description-file = README.md
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='devourer',
packages=['devourer'],
version='0.4.0',
version='0.4.1',
install_requires=[
'requests',
'futures',
Expand All @@ -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=[]
)

0 comments on commit c18cfee

Please sign in to comment.