Skip to content

Commit

Permalink
Merge pull request #91 from deep-compute/response_headers_propery
Browse files Browse the repository at this point in the history
Supported setting response headers
  • Loading branch information
jaswanth098 authored May 15, 2019
2 parents 886daaa + 427a9a2 commit 379be82
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ deploy:
- LICENSE
- kwikapi/api.py
- kwikapi/__init__.py
name: kwikapi-0.5.6
tag_name: 0.5.6
name: kwikapi-0.5.7
tag_name: 0.5.7
on:
repo: deep-compute/kwikapi
- provider: pypi
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,12 @@ $ wget "http://localhost:8888/api/v1/add" --header="X-KwikAPI-Protocol: numpy" -
$ wget "http://localhost:8888/api/v1/add?a=10&b=20" --header="X-KwikAPI-Protocol: raw"
```

#### We can also change outgoing protocol
#### We can also change outgoing/response protocol
ex:
```python
class Calc(object):
def add(self, req: Request, a: int, b: int) -> int:
req.headers['X-KwikAPI-Protocol'] = 'numpy'
req.response.headers['X-KwikAPI-Protocol'] = 'numpy'
return a + b
```

Expand Down
22 changes: 18 additions & 4 deletions kwikapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def flush(self):
def close(self):
pass

@abc.abstractproperty
def headers(self):
pass

class MockRequest(BaseRequest):

def __init__(self, **kwargs):
Expand Down Expand Up @@ -153,7 +157,7 @@ def headers(self):
class MockResponse(BaseResponse):
def __init__(self):
super().__init__()
self.headers = CaseInsensitiveDict()
self._headers = CaseInsensitiveDict()
self.raw_response = None

def write(self, data, protocol, stream=False):
Expand All @@ -169,6 +173,10 @@ def flush(self):
def close(self):
pass

@property
def headers(self):
return self._headers

class API(object):
"""
A collection of APIFragments
Expand Down Expand Up @@ -460,10 +468,16 @@ def _resolve_call_info(self, request):

return r

def _find_request_protocol(self, request):
protocol = request.headers.get(PROTOCOL_HEADER, self.default_protocol)
def _find_request_protocol(self, r):
protocol = r.headers.get(PROTOCOL_HEADER, self.default_protocol)
return self.PROTOCOLS[protocol]

def _find_response_protocol(self, r):
protocol = r.response.headers.get(PROTOCOL_HEADER, None)
if protocol:
return self.PROTOCOLS[protocol]
return self._find_request_protocol(r)

def _handle_exception(self, req, e):
message_value = e.message if hasattr(e, 'message') else str(e)
code_value = e.code if hasattr(e, 'code') else self.DEFAULT_ERROR_CODE
Expand Down Expand Up @@ -520,7 +534,7 @@ def handle_request(self, request):
self._invoke_pre_call_hook(request)
result = request.fn(**request.fn_params)

protocol = self._find_request_protocol(request)
protocol = self._find_response_protocol(request)

self._invoke_post_call_hook(request, result=result)

Expand Down
9 changes: 4 additions & 5 deletions kwikapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,18 @@ def _make_request(self, url, post_body, headers):
req = urllib.request.Request(url, data=post_body, headers=headers)
res = urllib.request.urlopen(req)

proto = PROTOCOLS[res.headers.get('X-KwikAPI-Protocol', self._protocol)]

if self._stream:
proto = PROTOCOLS[self._protocol]
res = proto.deserialize_stream(res)
res = Client._extract_stream_response(res, self._raise_exception)
else:
res = self._deserialize_response(res.read(), self._protocol,
self._raise_exception)
res = self._deserialize_response(res.read(), proto, self._raise_exception)

return res

@staticmethod
def _deserialize_response(data, protocol, raise_exception=True):
proto = PROTOCOLS[protocol]
def _deserialize_response(data, proto, raise_exception=True):
r = proto.deserialize(data)
return Client._extract_response(r, raise_exception)

Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '0.5.6'
version = '0.5.7'
setup(
name="kwikapi",
version=version,
Expand All @@ -21,9 +21,9 @@
'requests>=2.18.4',
],
extras_require={
'django': ['kwikapi-django==0.2.5'],
'tornado': ['kwikapi-tornado==0.3.4'],
'all': ['kwikapi-django==0.2.5', 'kwikapi-tornado==0.3.4']
'django': ['kwikapi-django==0.2.6'],
'tornado': ['kwikapi-tornado==0.3.7'],
'all': ['kwikapi-django==0.2.6', 'kwikapi-tornado==0.3.7']
},
classifiers=[
'Environment :: Web Environment',
Expand Down

0 comments on commit 379be82

Please sign in to comment.