From 056821f823387cbc9ab4c0352226868e8115b9a4 Mon Sep 17 00:00:00 2001 From: RamanjaneyuluIdavalapati Date: Mon, 6 Aug 2018 09:54:36 +0200 Subject: [PATCH] KwikAPI Client handling streaming response --- .travis.yml | 4 ++-- README.md | 13 ++++++++++++- kwikapi/client.py | 20 ++++++++++---------- kwikapi/protocols.py | 5 ++--- setup.py | 2 +- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index e9d1698..974b8b4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/README.md b/README.md index cb42921..a3b94c3 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/kwikapi/client.py b/kwikapi/client.py index 1aad18d..7030492 100644 --- a/kwikapi/client.py +++ b/kwikapi/client.py @@ -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 {} @@ -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: @@ -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() @@ -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): @@ -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 diff --git a/kwikapi/protocols.py b/kwikapi/protocols.py index 15dd586..6f19c63 100644 --- a/kwikapi/protocols.py +++ b/kwikapi/protocols.py @@ -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 @@ -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') @@ -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): diff --git a/setup.py b/setup.py index a84463d..223dc3c 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -version = '0.3.8' +version = '0.3.9' setup( name="kwikapi", version=version,