Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[concern] A single global http client is confusing #152

Open
qw-in opened this issue Jul 5, 2022 · 0 comments
Open

[concern] A single global http client is confusing #152

qw-in opened this issue Jul 5, 2022 · 0 comments
Labels
Request: Help wanted Extra attention is needed

Comments

@qw-in
Copy link
Contributor

qw-in commented Jul 5, 2022

Currently when using the admin api all stores (tokens) share the same global httpx.AsyncClient instance:

client: ClassVar[AsyncClient] = AsyncClient()

While I understand the motivation, I think this can lead to confusing behaviour and could potentially mean spylib users have to eject from spylib entirely in certain situations.

I think each token containing its own AsyncClient instance would be very helpful. Maybe I want to do my own error handling, or implement different rate limit handling (round-robin on multiple tokens for instance). That way we could say here are the nice features we provide, but if you need anything custom just grab mytoken.client which has base_url, headers, etc. set up with sane defaults.

This then also leads into a discussion about who is responsible for calling aclose on the client...but that may be a discussion for another time 😜

An (contrived) example test where things don't work as expected:

async def test_event_based_credential_leak(respx_mock: MockRouter):
from spylib.admin_api import OfflineTokenABC
from spylib.utils.rest import GET
class OfflineToken(OfflineTokenABC):
async def save(self):
raise NotImplementedError()
@classmethod
async def load(cls, store_name: str):
raise NotImplementedError()
store_one = 'store_one'
store_two = 'store_two'
token_one = OfflineToken(
store_name=store_one, access_token=f'secret_token_for_{store_one}', scope=[]
)
token_two = OfflineToken(
store_name=store_two, access_token=f'secret_token_for_{store_two}', scope=[]
)
token_one_headers = []
async def capture_request(request):
token_one_headers.append(request.headers['X-Shopify-Access-Token'])
token_one.client.event_hooks['request'] = [capture_request]
respx_mock.get().mock(
return_value=Response(200, json={}, headers={'X-Shopify-Shop-Api-Call-Limit': '10/20'})
)
await token_two.execute_rest(
request=GET,
endpoint='/test',
)
assert token_one_headers == []

@qw-in qw-in added the Request: Help wanted Extra attention is needed label Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Request: Help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant