Skip to content

Commit

Permalink
KwikAPI Client handling streaming response
Browse files Browse the repository at this point in the history
  • Loading branch information
RamanjaneyuluIdavalapati committed Aug 6, 2018
1 parent 98c3658 commit 056821f
Show file tree
Hide file tree
Showing 5 changed files with 27 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.3.8
tag_name: 0.3.8
name: kwikapi-0.3.9
tag_name: 0.3.9
on:
repo: deep-compute/kwikapi
- provider: pypi
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ True
- API Doc
- Bulk request handling
- KwikAPI Client
- Authentication

### Versioning support
Versioning support will be used if user wants different versions of functionality with slightly changed behaviour.
Expand Down Expand Up @@ -601,7 +602,17 @@ print(c.namespace.add(a=10, b=10))

# Parameters can be changed that are passed to the Client object

print(c(version='v2', prtocol='pickle').namespace.add(a=10, b=10))
print(c(version='v2', protocol='pickle').namespace.add(a=10, b=10))
```

Streaming response is handled by passing the parameter `stream` as True.
```python
c = Client('http://localhost:8818/api/', version='v1')
res = c(stream=True).add(a=10, b=10)

# You will get generator object on stream True
for r in res:
print(r)
```

### Authentication
Expand Down
20 changes: 10 additions & 10 deletions kwikapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Client:

def __init__(self, url, version=None, protocol=DEFAULT_PROTOCOL,
path=None, request='', timeout=None, dnscache=None,
headers=None, auth=None, log=DUMMY_LOG):
headers=None, auth=None, stream=False, log=DUMMY_LOG):

headers = headers or {}

Expand All @@ -60,6 +60,7 @@ def __init__(self, url, version=None, protocol=DEFAULT_PROTOCOL,
self._dnscache = dnscache
self._headers = CaseInsensitiveDict(headers)
self._auth = auth
self._stream = stream
self._log = log

if not self._dnscache:
Expand All @@ -70,7 +71,7 @@ def _get_state(self):
protocol=self._protocol, path=self._path,
request=self._request, timeout=self._timeout,
dnscache=self._dnscache, headers=self._headers,
auth=self._auth, log=self._log)
auth=self._auth, stream=self._stream,log=self._log)

def _copy(self, **kwargs):
_kwargs = self._get_state()
Expand Down Expand Up @@ -103,17 +104,17 @@ def _prepare_request(self, post_body, get_params=None):

return url, post_body, headers

@staticmethod
def _make_request(url, post_body, headers):
def _make_request(self, url, post_body, headers):
req = urllib.request.Request(url, data=post_body, headers=headers)
res = urllib.request.urlopen(req)

# FIXME: catch exceptions raised and
# also check the response code
#if res.status_code != requests.codes.ok:
# raise APICallFailed(res.status_code)
if self._stream:
proto = PROTOCOLS[self._protocol]
res = proto.deserialize_stream(res)
else:
res = self._deserialize_response(res.read(), self._protocol)

return res.read()
return res

@staticmethod
def _deserialize_response(data, protocol):
Expand Down Expand Up @@ -150,7 +151,6 @@ def __call__(self, *args, **kwargs):
post_body = self._serialize_params(kwargs, self._protocol)
url, post_body, headers = self._prepare_request(post_body)
res = self._make_request(url, post_body, headers)
res = self._deserialize_response(res, self._protocol)

if isinstance(res, Exception):
raise res
Expand Down
5 changes: 2 additions & 3 deletions kwikapi/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

from .exception import StreamingNotSupported
from .utils import walk_data_structure, to_python_type, liteval
from .utils import walk_data_structure, liteval

class BaseProtocol(object):
__metaclass__ = abc.ABCMeta
Expand Down Expand Up @@ -43,7 +43,6 @@ def get_name():

@staticmethod
def serialize(data):
data = to_python_type(data)
data = json.dumps(data)

return data.encode('utf-8')
Expand Down Expand Up @@ -73,7 +72,7 @@ def get_name():

@staticmethod
def serialize(data):
return msgpack.packb(data)
return msgpack.packb(data, encoding="utf-8")

@staticmethod
def deserialize(data):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '0.3.8'
version = '0.3.9'
setup(
name="kwikapi",
version=version,
Expand Down

0 comments on commit 056821f

Please sign in to comment.