From 056cedc4a4755ff7fa2eebe761b40d57846b6c2f Mon Sep 17 00:00:00 2001 From: Allisson Azevedo Date: Tue, 3 Oct 2023 10:38:17 -0300 Subject: [PATCH] fix: Add a fix for stricter enforcement around client scoping (#59) --- docs/quickstart.rst | 6 ++++++ examples/async_github.py | 1 + examples/async_httpbin.py | 1 + examples/async_httpbin_disable_ssl.py | 1 + examples/github.py | 1 + examples/httpbin.py | 1 + examples/httpbin_disable_ssl.py | 1 + simple_rest_client/api.py | 8 ++++++++ simple_rest_client/resource.py | 9 +++++++-- tests/test_resource.py | 8 ++++++++ 10 files changed, 35 insertions(+), 2 deletions(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 4ad1be1..6ae83b3 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -55,6 +55,8 @@ Let's start building a client for users resource in https://reqres.in/ service:: >>> response = api.users.destroy(2, body=None, params={}, headers={}) >>> response.status_code 204 + >>> # close client connections + >>> api.close_client() Building async client for users resource in https://reqres.in/ service:: @@ -75,6 +77,8 @@ Building async client for users resource in https://reqres.in/ service:: >>> api.add_resource(resource_name='users', resource_class=AsyncResource) >>> async def main(): ... print(await api.users.list()) + ... # close client connections + ... await api.aclose_client() ... >>> loop = asyncio.get_event_loop() >>> loop.run_until_complete(main()) @@ -125,6 +129,8 @@ Now, building a client for github events resource (https://developer.github.com/ 'https://api.github.com/repos/allisson/python-simple-rest-client/events?access_token=valid-token' >>> response.method 'GET' + >>> # close client connections + >>> api.close_client() Create API without certificate validation diff --git a/examples/async_github.py b/examples/async_github.py index 3bcae85..cec9cb5 100644 --- a/examples/async_github.py +++ b/examples/async_github.py @@ -32,6 +32,7 @@ async def main(): await github_api.events.repository_events("allisson", "python-simple-rest-client") ) ) + await github_api.aclose_client() asyncio.run(main()) diff --git a/examples/async_httpbin.py b/examples/async_httpbin.py index 9f080e0..de2fcc1 100644 --- a/examples/async_httpbin.py +++ b/examples/async_httpbin.py @@ -17,6 +17,7 @@ class BasicAuthResource(AsyncResource): async def main(): response = await httpbin_api.basic_auth.retrieve("username", "password", auth=auth) print("httpbin_api.basic_auth.retrieve={!r}".format(response.body)) + await httpbin_api.aclose_client() asyncio.run(main()) diff --git a/examples/async_httpbin_disable_ssl.py b/examples/async_httpbin_disable_ssl.py index 7bf847a..c308d60 100644 --- a/examples/async_httpbin_disable_ssl.py +++ b/examples/async_httpbin_disable_ssl.py @@ -17,6 +17,7 @@ class BasicAuthResource(AsyncResource): async def main(): response = await httpbin_api.basic_auth.retrieve("username", "password", auth=auth) print("httpbin_api.basic_auth.retrieve={!r}".format(response.body)) + await httpbin_api.aclose_client() asyncio.run(main()) diff --git a/examples/github.py b/examples/github.py index a9a4d31..38d8e62 100644 --- a/examples/github.py +++ b/examples/github.py @@ -27,3 +27,4 @@ class EventResource(Resource): github_api.events.repository_events("allisson", "python-simple-rest-client") ) ) +github_api.close_client() diff --git a/examples/httpbin.py b/examples/httpbin.py index b6ea146..97783f5 100644 --- a/examples/httpbin.py +++ b/examples/httpbin.py @@ -15,3 +15,4 @@ class BasicAuthResource(Resource): httpbin_api.basic_auth.retrieve("username", "password", auth=auth).body ) ) +httpbin_api.close_client() diff --git a/examples/httpbin_disable_ssl.py b/examples/httpbin_disable_ssl.py index 2378e0e..f1248cd 100644 --- a/examples/httpbin_disable_ssl.py +++ b/examples/httpbin_disable_ssl.py @@ -15,3 +15,4 @@ class BasicAuthResource(Resource): httpbin_api.basic_auth.retrieve("username", "password", auth=auth).body ) ) +httpbin_api.close_client() diff --git a/simple_rest_client/api.py b/simple_rest_client/api.py index 783fab7..4f13d51 100644 --- a/simple_rest_client/api.py +++ b/simple_rest_client/api.py @@ -56,3 +56,11 @@ def get_resource_list(self): def correct_attribute_name(self, name): slug_name = slugify(name) return slug_name.replace("-", "_") + + def close_client(self): + for resource in self._resources.values(): + resource.close_client() + + async def aclose_client(self): + for resource in self._resources.values(): + await resource.close_client() diff --git a/simple_rest_client/resource.py b/simple_rest_client/resource.py index 8c7803e..0aa6ce8 100644 --- a/simple_rest_client/resource.py +++ b/simple_rest_client/resource.py @@ -81,6 +81,9 @@ def __init__(self, *args, **kwargs): for action_name in self.actions.keys(): self.add_action(action_name) + def close_client(self): + self.client.close() + def add_action(self, action_name): def action_method( self, *args, body=None, params=None, headers=None, action_name=action_name, **kwargs @@ -110,6 +113,9 @@ def __init__(self, *args, **kwargs): for action_name in self.actions.keys(): self.add_action(action_name) + async def close_client(self): + await self.client.aclose() + def add_action(self, action_name): async def action_method( self, *args, body=None, params=None, headers=None, action_name=action_name, **kwargs @@ -127,7 +133,6 @@ async def action_method( ) request.params.update(self.params) request.headers.update(self.headers) - async with self.client as client: - return await make_async_request(client, request) + return await make_async_request(self.client, request) setattr(self, action_name, MethodType(action_method, self)) diff --git a/tests/test_resource.py b/tests/test_resource.py index 42fb6d5..f2faa16 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -135,6 +135,10 @@ def test_resource_response_body( response = reqres_resource.list() assert response.body == expected_response_body + # call again to validate the fix for "Stricter enforcement around client scoping" + response = reqres_resource.list() + assert response.body == expected_response_body + @pytest.mark.asyncio @pytest.mark.parametrize( @@ -181,3 +185,7 @@ async def test_asyncresource_response_body( response = await reqres_async_resource.list() assert response.body == expected_response_body + + # call again to validate the fix for "Stricter enforcement around client scoping" + response = await reqres_async_resource.list() + assert response.body == expected_response_body