Skip to content

Commit

Permalink
Merge pull request #5 from oztqa/igorozt-patch-1
Browse files Browse the repository at this point in the history
fix serializer + code refactoring
  • Loading branch information
griddic authored Jun 3, 2019
2 parents d33675d + d114733 commit 55f5dfc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
56 changes: 26 additions & 30 deletions reqtools/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RemoteApiSession(Session):
__attrs__ = Session.__attrs__ + ['_base_url', '_prefix']

def __init__(self, base_url: str, *, prefix: str = None):
super(RemoteApiSession, self).__init__()
super().__init__()

self._base_url = base_url
self._prefix = prefix
Expand All @@ -32,42 +32,38 @@ def prefix(self):

@property
def url(self):
if self.prefix:
return self._glue_parts(self._base_url, self._prefix)
return self._base_url

def _build_url(self, url_path: str):
if self._prefix:
url_path = self._glue_parts(self._prefix, url_path)
return self._glue_parts(self._base_url, url_path)

def _glue_parts(self, part1, part2: str):
return part1.rstrip('/') + '/' + part2.lstrip('/')
return self._build_url(self._base_url, self._prefix)

def request(self, method: str, url_path: str, **kwargs):
url = self._build_url(url_path)
logger.info(f'Performing "{method}" request to "{url}"')

if kwargs:
for k, v in kwargs.items():
if v is not None:
message = json.dumps(v, ensure_ascii=False, indent=4)
logger.info(f'Request param "{k}": {message}')
url = self._build_url(self.url, url_path)

logger.info(
f'Performing "{method}" request to "{url}"'
f'\nRequest params: {self._serialize(kwargs)}')

resp = super(RemoteApiSession, self).request(method, url, **kwargs)
logger.info(curlify.to_curl(resp.request))
logger.info(f'Response status code is "{resp.status_code}"')
resp = super().request(method, url, **kwargs)

try:
message = json.dumps(resp.json(), ensure_ascii=False, indent=4)
logger.info('\n' + message)
resp_content = self._serialize(resp.json())
except ValueError:
logger.info('\n' + resp.text)
resp_content = resp.text

headers = json.dumps({k: v for k, v in resp.headers.items()})
logger.info(f'Headers: {headers}')

total_seconds = resp.elapsed.total_seconds()
logger.info(f'Response time is "{total_seconds}" seconds')
logger.info(
f'{curlify.to_curl(resp.request)}'
f'\nResponse status code is "{resp.status_code}"'
f'\n{resp_content}'
f'\nHeaders: {self._serialize(dict(resp.headers))}'
f'\nResponse time is "{resp.elapsed.total_seconds()}" seconds')

return resp

@staticmethod
def _build_url(base, path: str):
return base.rstrip('/') + '/' + path.lstrip('/') if path else base

@staticmethod
def _serialize(obj):
return json.dumps(
obj, ensure_ascii=False, indent=4,
default=lambda o: repr(o))
6 changes: 3 additions & 3 deletions tests/suite_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_remote_api_url_with_prefix(url, prefix, expected):

def test_remote_api_build_url():
session = RemoteApiSession('http://test.ru')
assert_that(session._build_url('/test'), is_(equal_to('http://test.ru/test')))
assert_that(session._build_url(session.url, '/test'), is_(equal_to('http://test.ru/test')))


@pytest.mark.parametrize(
Expand All @@ -63,7 +63,7 @@ def test_remote_api_build_url():
)
def test_remote_api_build_url_with_prefix(prefix, url_path, expected):
session = RemoteApiSession('http://test.ru', prefix=prefix)
assert_that(session._build_url(url_path), is_(equal_to(expected)))
assert_that(session._build_url(session.url, url_path), is_(equal_to(expected)))


@pytest.mark.parametrize(
Expand All @@ -81,7 +81,7 @@ def test_remote_api_build_url_with_prefix(prefix, url_path, expected):
)
def test_remote_api_build_url_with_prefix_and_nonorigin_base(base, prefix, url_path, expected):
session = RemoteApiSession(base, prefix=prefix)
assert_that(session._build_url(url_path), is_(equal_to(expected)))
assert_that(session._build_url(session.url, url_path), is_(equal_to(expected)))


def test_serializable():
Expand Down

0 comments on commit 55f5dfc

Please sign in to comment.