From 5ba283bddcf04aefd96f3803df64dd8d6263c7e8 Mon Sep 17 00:00:00 2001 From: pavan-maddula Date: Wed, 23 Jan 2019 14:53:23 +0530 Subject: [PATCH] support to return exception_objects in client --- .travis.yml | 4 ++-- kwikapi/api.py | 2 +- kwikapi/client.py | 29 ++++++++++++++++++----------- kwikapi/exception.py | 5 +++++ setup.py | 2 +- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5de8583..d731baf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/kwikapi/api.py b/kwikapi/api.py index 4b0f101..00cd231 100644 --- a/kwikapi/api.py +++ b/kwikapi/api.py @@ -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 diff --git a/kwikapi/client.py b/kwikapi/client.py index ad19f8a..8317e6b 100644 --- a/kwikapi/client.py +++ b/kwikapi/client.py @@ -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 @@ -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 @@ -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() @@ -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() @@ -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): diff --git a/kwikapi/exception.py b/kwikapi/exception.py index e5c409c..a032df5 100644 --- a/kwikapi/exception.py +++ b/kwikapi/exception.py @@ -1,6 +1,11 @@ # -*- coding: utf-8 -* import abc + +class ResponseError(Exception): + pass + + class BaseException(Exception): __metaclass__ = abc.ABCMeta diff --git a/setup.py b/setup.py index 58cf648..49df893 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -version = '0.5.0' +version = '0.5.1' setup( name="kwikapi", version=version,