Skip to content

Commit

Permalink
Merge aeecc72 into dd9ad49
Browse files Browse the repository at this point in the history
  • Loading branch information
alfred82santa committed Mar 3, 2021
2 parents dd9ad49 + aeecc72 commit c7790e9
Show file tree
Hide file tree
Showing 15 changed files with 1,006 additions and 954 deletions.
File renamed without changes.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: python
python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
# command to install dependencies
install:
- make requirements-test
Expand Down
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ In order to send a payload you must use ``payload`` keyword on call:
Changelog
=========


v0.7.1
------

- Python 3.9 compatible.
- Update aiohttp.


v0.6.1
------

Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ flake8
coverage
nose
asynctest
autopep8
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
dirty-loader >= 0.2.2
git+https://github.com/alfred82santa/configure.git
aiohttp >= 3.1.1
configure-fork
aiohttp >= 3.7.4
14 changes: 9 additions & 5 deletions service_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import logging
from asyncio import get_event_loop, ensure_future
from asyncio.tasks import Task
from asyncio import Task, ensure_future, get_event_loop
from urllib.parse import urlparse, urlunsplit

try:
current_task = Task.current_task
except AttributeError:
from asyncio import current_task

from aiohttp.client import ClientSession
from aiohttp.client_reqrep import ClientResponse
from aiohttp.connector import TCPConnector
from yarl import URL

from .utils import ObjectWrapper

__version__ = '0.7.0'
__version__ = '0.7.1'


class ServiceClient:
Expand All @@ -36,7 +40,7 @@ def __init__(self, name='GenericService', spec=None, plugins=None, config=None,

def create_response(self, *args, **kwargs):
response = ObjectWrapper(ClientResponse(*args, **kwargs))
task = Task.current_task(loop=self.loop)
task = current_task(loop=self.loop)

self._execute_plugin_hooks_sync('prepare_response',
endpoint_desc=task.endpoint_desc, session=task.session,
Expand Down Expand Up @@ -78,7 +82,7 @@ async def call(self, endpoint, payload=None, **kwargs):
request_params=request_params)

await self.before_request(endpoint_desc, session, request_params)
task = Task.current_task(loop=self.loop)
task = current_task(loop=self.loop)
task.session = session
task.endpoint_desc = endpoint_desc
task.request_params = request_params
Expand Down
33 changes: 24 additions & 9 deletions service_client/mocks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from asyncio import get_event_loop
from asyncio import create_task, get_event_loop
from asyncio.futures import Future
from functools import wraps

from aiohttp import RequestInfo
from aiohttp.client_reqrep import ClientResponse
from aiohttp.helpers import TimerContext
from dirty_loader import LoaderNamespaceReversedCached
from functools import wraps
from multidict import CIMultiDict
from multidict import CIMultiDict, CIMultiDictProxy
from yarl import URL

from .plugins import BasePlugin
Expand Down Expand Up @@ -204,14 +206,27 @@ async def __call__(self, *args, **kwargs):
self.url = url
self.args = args
self.kwargs = kwargs
self.response = ClientResponse(method, URL(url),
writer=None, continue100=False, timer=None,
request_info=RequestInfo(URL(url), method, kwargs.get('headers', [])),
auto_decompress=False,
traces=[], loop=self.loop, session=self.session)

async def writer(*args, **kwargs):
return None

continue100 = Future()
continue100.set_result(False)

self.response = ClientResponse(method,
URL(url),
writer=create_task(writer()),
continue100=continue100,
timer=TimerContext(loop=self.loop),
request_info=RequestInfo(URL(url),
method,
kwargs.get('headers', [])),
traces=[],
loop=self.loop,
session=self.session)

self.response.status = self.mock_desc.get('status', 200)
self.response.headers = CIMultiDict(self.mock_desc.get('headers', {}))
self.response._headers = CIMultiDictProxy(CIMultiDict(self.mock_desc.get('headers', {})))

await self.prepare_response()

Expand Down
6 changes: 3 additions & 3 deletions service_client/plugins.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
from asyncio import wait_for, TimeoutError
import weakref
from asyncio import TimeoutError, wait_for
from datetime import datetime
from functools import wraps
from urllib.parse import quote_plus

import weakref
from async_timeout import timeout as TimeoutContext
from functools import wraps
from multidict import CIMultiDict

from service_client.utils import IncompleteFormatter, random_token
Expand Down
6 changes: 2 additions & 4 deletions service_client/spec_loaders.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@


def json_loader(filename):
from json import load
with open(filename) as f:
return load(f)


def yaml_loader(filename):
from yaml import load
from yaml import load, FullLoader
with open(filename) as f:
return load(f)
return load(f, Loader=FullLoader)


def configuration_loader(filename):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
'Development Status :: 4 - Beta'],
packages=['service_client'],
include_package_data=False,
install_requires=['dirty-loader>=0.2.2', 'aiohttp>=3.1.1'],
install_requires=['dirty-loader>=0.2.2', 'aiohttp>=3.7.4', 'configure-fork'],
description="Service Client Framework powered by Python asyncio.",
long_description=open(os.path.join(os.path.dirname(__file__), 'README.rst')).read(),
test_suite="nose.collector",
Expand Down
28 changes: 23 additions & 5 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
from asyncio import get_event_loop
from asyncio import Future, get_event_loop

from aiohttp import ClientResponse, RequestInfo
from aiohttp.helpers import TimerContext
from yarl import URL


async def create_fake_response(method, url, *, session, headers=None, loop=None):
loop = loop or get_event_loop()

try:
from asyncio import create_task
except ImportError:
create_task = loop.create_task

async def writer(*args, **kwargs):
return None

continue100 = Future()
continue100.set_result(False)

return ClientResponse(method, URL(url),
writer=None, continue100=False, timer=None,
request_info=RequestInfo(URL(url), method,
writer=create_task(writer()),
continue100=continue100,
timer=TimerContext(loop=loop),
request_info=RequestInfo(URL(url),
method,
headers or []),
auto_decompress=False,
traces=[], loop=loop or get_event_loop(), session=session)
traces=[],
loop=loop,
session=session)
4 changes: 2 additions & 2 deletions tests/tests_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_on_parse_exception(self):
'status_code': 404,
'elapsed': timedelta(seconds=0.214),
'exception': AttributeError('test exception')})
log_text = """EXCEPTION | GET http://example.com | 404 Not Found | 214 ms | AttributeError('test exception',)
log_text = """EXCEPTION | GET http://example.com | 404 Not Found | 214 ms | AttributeError('test exception')
Headers:
Test-Header-1: header value 1
Test-Header-2: header value 2
Expand All @@ -110,7 +110,7 @@ def test_on_exception(self):
'method': 'GET',
'url': 'http://example.com',
'exception': AttributeError('test exception')})
log_text = "EXCEPTION | GET http://example.com | AttributeError('test exception',)"
log_text = "EXCEPTION | GET http://example.com | AttributeError('test exception')"

self.assertEqual(self.formatter.formatMessage(log_entry), log_text)

Expand Down
3 changes: 2 additions & 1 deletion tests/tests_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from aiohttp import hdrs
from aiohttp.client import ClientSession
from asynctest.case import TestCase
from multidict import CIMultiDict

from service_client.mocks import Mock, mock_manager, RawFileMock
from service_client.utils import ObjectWrapper
Expand Down Expand Up @@ -124,7 +125,7 @@ async def test_calling_mock_same_endpoint(self):

@mock_manager.patch_mock_desc(patch={'mock_type': 'default:RawFileMock',
'file': os.path.join(MOCKS_DIR, 'test_mock_text.data'),
'headers': {hdrs.CONTENT_TYPE: "text/plain; charset=utf8"}})
'headers': CIMultiDict({hdrs.CONTENT_TYPE: "text/plain; charset=utf8"})})
async def test_patch_mock(self):
await self.plugin.prepare_session(self.service_desc, self.session, {})
self.assertIsInstance(self.session.request, RawFileMock)
Expand Down
Loading

0 comments on commit c7790e9

Please sign in to comment.