Skip to content

Commit

Permalink
Merge pull request #82 from deep-compute/KwikAPI_client_exceptions
Browse files Browse the repository at this point in the history
support to return exception_objects in client
  • Loading branch information
Pavan Kumar Reddy Maddula authored Jan 24, 2019
2 parents c2a0cfc + 5ba283b commit 03ed74a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 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.0
tag_name: 0.5.0
name: kwikapi-0.5.1
tag_name: 0.5.1
on:
repo: deep-compute/kwikapi
- provider: pypi
Expand Down
2 changes: 1 addition & 1 deletion kwikapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def handle_request(self, request):
try:
result = request.fn(**request.fn_params)
except TypeError as e:
if 'got an unexpected keyword argument' in str(e):
if 'got an unexpected keyword argument' in str(e): # FIXME: handle in better way
raise KeywordArgumentError(e.args[0])
else:
raise e
Expand Down
29 changes: 18 additions & 11 deletions kwikapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from deeputil import Dummy, ExpiringCache

from .protocols import PROTOCOLS
from .exception import NonKeywordArgumentsError
from .exception import NonKeywordArgumentsError, ResponseError
from .api import PROTOCOL_HEADER, REQUEST_ID_HEADER
from .utils import get_loggable_params

Expand Down Expand Up @@ -46,7 +46,9 @@ class Client:

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

headers = headers or {}

self._url = url
Expand All @@ -61,6 +63,7 @@ def __init__(self, url, version=None, protocol=DEFAULT_PROTOCOL,
self._auth = auth
self._stream = stream
self._log = log
self._raise_exception = raise_exception

if not self._dnscache:
self._dnscache = DNSCache()
Expand All @@ -70,7 +73,8 @@ 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, stream=self._stream,log=self._log)
auth=self._auth, stream=self._stream,log=self._log,
raise_exception=self._raise_exception)

def _copy(self, **kwargs):
_kwargs = self._get_state()
Expand Down Expand Up @@ -110,33 +114,36 @@ def _make_request(self, url, post_body, headers):
if self._stream:
proto = PROTOCOLS[self._protocol]
res = proto.deserialize_stream(res)
res = Client._extract_stream_response(res)
res = Client._extract_stream_response(res, self._raise_exception)
else:
res = self._deserialize_response(res.read(), self._protocol)
res = self._deserialize_response(res.read(), self._protocol,
self._raise_exception)

return res

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

@staticmethod
def _extract_response(r):
def _extract_response(r, raise_exception=True):
success = r['success']
if not success:
r.pop('success')
raise Exception(r) # FIXME: raise proper exc
r = ResponseError(r)
if raise_exception:
raise r
else:
r = r['result']

return r

@staticmethod
def _extract_stream_response(res):
def _extract_stream_response(res, raise_exception=True):
for r in res:
yield Client._extract_response(r)
yield Client._extract_response(r, raise_exception)

@staticmethod
def _serialize_params(params, protocol):
Expand Down
5 changes: 5 additions & 0 deletions kwikapi/exception.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# -*- coding: utf-8 -*
import abc


class ResponseError(Exception):
pass


class BaseException(Exception):
__metaclass__ = abc.ABCMeta

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.5.0'
version = '0.5.1'
setup(
name="kwikapi",
version=version,
Expand Down

0 comments on commit 03ed74a

Please sign in to comment.