Skip to content

Commit

Permalink
fix: Add a fix for stricter enforcement around client scoping (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
allisson authored Oct 3, 2023
1 parent 2cabb28 commit 056cedc
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand All @@ -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())
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions examples/async_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
1 change: 1 addition & 0 deletions examples/async_httpbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
1 change: 1 addition & 0 deletions examples/async_httpbin_disable_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
1 change: 1 addition & 0 deletions examples/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ class EventResource(Resource):
github_api.events.repository_events("allisson", "python-simple-rest-client")
)
)
github_api.close_client()
1 change: 1 addition & 0 deletions examples/httpbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ class BasicAuthResource(Resource):
httpbin_api.basic_auth.retrieve("username", "password", auth=auth).body
)
)
httpbin_api.close_client()
1 change: 1 addition & 0 deletions examples/httpbin_disable_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ class BasicAuthResource(Resource):
httpbin_api.basic_auth.retrieve("username", "password", auth=auth).body
)
)
httpbin_api.close_client()
8 changes: 8 additions & 0 deletions simple_rest_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
9 changes: 7 additions & 2 deletions simple_rest_client/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))
8 changes: 8 additions & 0 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

0 comments on commit 056cedc

Please sign in to comment.