Skip to content

Commit

Permalink
Add direct call & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alfred82santa committed Apr 29, 2016
1 parent 57f3bb5 commit bca41e8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
10 changes: 9 additions & 1 deletion service_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def call(self, service_name, payload=None, **kwargs):
e.response = response
raise e

session.close()
return response

@coroutine
Expand Down Expand Up @@ -193,5 +192,14 @@ def add_plugins(self, plugins):
for func in hooks:
func(service_client=self)

def __getattr__(self, item):

@coroutine
def wrap(*args, **kwargs):

return self.call(item, *args, **kwargs)

return wrap

def __del__(self): # pragma: no cover
self.session.close()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
name='aio-service-client',
url='https://github.com/alfred82santa/aio-service-client',
author='alfred82santa',
version='0.1.1',
version='0.1.2',
author_email='alfred82santa@gmail.com',
classifiers=[
'Intended Audience :: Developers',
Expand Down
97 changes: 97 additions & 0 deletions tests/tests_service_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,103 @@ def test_workflow_post(self):
self.assertNotIn('on_exception', self.plugin.calls, "On exception call")
self.assertNotIn('on_parse_exception', self.plugin.calls, "On parse exception call")

@coroutine
def test_workflow_post_direct_call(self):
response = yield from self.service_client.testService2(payload='aaaa')
self.assertEqual(response, self.response)

self.assertIn('assign_service_client', self.plugin.calls, "Assign service_client call")
self.assertEqual(self.plugin.calls['assign_service_client']['args'], ())
self.assertDictEqual(self.plugin.calls['assign_service_client'][
'kwargs'], {'service_client': self.service_client})

self.assertIn('prepare_session', self.plugin.calls, "Prepare session call")
self.assertEqual(self.plugin.calls['prepare_session']['args'], ())
self.assertDictEqual(
self.plugin.calls['prepare_session']['kwargs'], {
'service_desc': {'path': '/path/to/service2',
'method': 'post',
'service_name': 'testService2'},
'session': self.mock_session,
'request_params': {'data': 'aaaa',
'method': 'POST',
'url': 'http://foo.com/sdsd/path/to/service2'}})

self.assertIn('prepare_path', self.plugin.calls, "Prepare path call")
self.assertEqual(self.plugin.calls['prepare_path']['args'], ())
self.assertDictEqual(self.plugin.calls['prepare_path']['kwargs'],
{'service_desc': {'path': '/path/to/service2',
'method': 'post',
'service_name': 'testService2'},
'session': self.mock_session,
'request_params': {'method': 'POST',
'url': 'http://foo.com/sdsd/path/to/service2',
'data': 'aaaa'},
'path': 'http://foo.com/sdsd/path/to/service2'})

self.assertIn('prepare_request_params', self.plugin.calls, "Prepare request params call")
self.assertEqual(self.plugin.calls['prepare_request_params']['args'], ())
self.assertDictEqual(self.plugin.calls['prepare_request_params']['kwargs'],
{'service_desc': {'path': '/path/to/service2',
'method': 'post',
'service_name': 'testService2'},
'session': self.mock_session,
'request_params': {'method': 'POST',
'url': 'http://foo.com/sdsd/path/to/service2',
'data': 'aaaa'}})

self.assertIn('prepare_payload', self.plugin.calls, "Prepare request payload call")
self.assertEqual(self.plugin.calls['prepare_payload']['args'], ())
self.assertDictEqual(self.plugin.calls['prepare_payload']['kwargs'],
{'service_desc': {'path': '/path/to/service2',
'method': 'post',
'service_name': 'testService2'},
'session': self.mock_session,
'request_params': {'method': 'POST',
'url': 'http://foo.com/sdsd/path/to/service2',
'data': 'aaaa'},
'payload': 'aaaa'})

self.assertIn('before_request', self.plugin.calls, "Before request call")
self.assertEqual(self.plugin.calls['before_request']['args'], ())
self.assertDictEqual(self.plugin.calls['before_request']['kwargs'],
{'service_desc': {'path': '/path/to/service2',
'method': 'post',
'service_name': 'testService2'},
'session': self.mock_session,
'request_params': {'method': 'POST',
'url': 'http://foo.com/sdsd/path/to/service2',
'data': 'aaaa'}})

self.assertIn('on_response', self.plugin.calls, "On response call")
self.assertEqual(self.plugin.calls['on_response']['args'], ())
self.assertDictEqual(self.plugin.calls['on_response']['kwargs'],
{'service_desc': {'path': '/path/to/service2',
'method': 'post',
'service_name': 'testService2'},
'session': self.mock_session,
'request_params': {'method': 'POST',
'url': 'http://foo.com/sdsd/path/to/service2',
'data': 'aaaa'},
'response': self.response})

self.assertIn('on_parsed_response', self.plugin.calls, "On parse response call")
self.assertEqual(self.plugin.calls['on_parsed_response']['args'], ())
self.assertDictEqual(self.plugin.calls['on_parsed_response']['kwargs'],
{'service_desc': {'path': '/path/to/service2',
'method': 'post',
'service_name': 'testService2'},
'session': self.mock_session,
'request_params': {'method': 'POST',
'url': 'http://foo.com/sdsd/path/to/service2',
'data': 'aaaa'},
'response': self.response})

self.assertEqual(self.response.data, b'bbbb')

self.assertNotIn('on_exception', self.plugin.calls, "On exception call")
self.assertNotIn('on_parse_exception', self.plugin.calls, "On parse exception call")

@coroutine
def test_workflow_post_exception_response(self):

Expand Down

0 comments on commit bca41e8

Please sign in to comment.