From dbf4de20e864ba3790262fe07d563f218667a79d Mon Sep 17 00:00:00 2001 From: Dmitry Inyutin Date: Thu, 11 Aug 2022 22:00:38 +0300 Subject: [PATCH 1/3] fix redurant arguments due to 2.8.0 --- README.md | 3 +++ aiohttp_retry/client.py | 17 ++++++++--------- setup.py | 2 +- tests/app.py | 7 +++++++ tests/test_client.py | 13 +++++++++++++ 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index fa1973f..07d9778 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ Python 3.7 or higher. ### Breaking API changes +- 2.8.0 is incorrect and yanked. +https://github.com/inyutin/aiohttp_retry/issues/79 + - Since 2.5.6 this is a new parameter in ```get_timeout``` func called "response". If you have defined your own ```RetryOptions```, you should add this param into it. Issue about this: https://github.com/inyutin/aiohttp_retry/issues/59 diff --git a/aiohttp_retry/client.py b/aiohttp_retry/client.py index 38e5577..9d97af7 100644 --- a/aiohttp_retry/client.py +++ b/aiohttp_retry/client.py @@ -222,7 +222,7 @@ def request( url: StrOrURL, retry_options: Optional[RetryOptionsBase] = None, raise_for_status: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> _RequestContext: return self._make_request( method=method, @@ -237,7 +237,7 @@ def get( url: _URL_TYPE, retry_options: Optional[RetryOptionsBase] = None, raise_for_status: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> _RequestContext: return self._make_request( method=hdrs.METH_GET, @@ -252,7 +252,7 @@ def options( url: _URL_TYPE, retry_options: Optional[RetryOptionsBase] = None, raise_for_status: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> _RequestContext: return self._make_request( method=hdrs.METH_OPTIONS, @@ -266,7 +266,7 @@ def head( self, url: _URL_TYPE, retry_options: Optional[RetryOptionsBase] = None, - raise_for_status: Optional[bool] = None, **kwargs: Any + raise_for_status: Optional[bool] = None, **kwargs: Any, ) -> _RequestContext: return self._make_request( method=hdrs.METH_HEAD, @@ -281,7 +281,7 @@ def post( url: _URL_TYPE, retry_options: Optional[RetryOptionsBase] = None, raise_for_status: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> _RequestContext: return self._make_request( method=hdrs.METH_POST, @@ -296,7 +296,7 @@ def put( url: _URL_TYPE, retry_options: Optional[RetryOptionsBase] = None, raise_for_status: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> _RequestContext: return self._make_request( method=hdrs.METH_PUT, @@ -311,7 +311,7 @@ def patch( url: _URL_TYPE, retry_options: Optional[RetryOptionsBase] = None, raise_for_status: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> _RequestContext: return self._make_request( method=hdrs.METH_PATCH, @@ -326,7 +326,7 @@ def delete( url: _URL_TYPE, retry_options: Optional[RetryOptionsBase] = None, raise_for_status: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> _RequestContext: return self._make_request( method=hdrs.METH_DELETE, @@ -360,7 +360,6 @@ def _make_request( params_list=params_list, retry_options=retry_options, raise_for_status=raise_for_status, - **kwargs, ) def _make_requests( diff --git a/setup.py b/setup.py index b79008f..e8be2ce 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='aiohttp_retry', - version='2.8.0', + version='2.8.1', description='Simple retry client for aiohttp', long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/app.py b/tests/app.py index 8007276..b10523b 100644 --- a/tests/app.py +++ b/tests/app.py @@ -12,6 +12,7 @@ def __init__(self): app.router.add_get('/sometimes_error', self.sometimes_error) app.router.add_get('/sometimes_json', self.sometimes_json) app.router.add_get('/check_headers', self.check_headers) + app.router.add_get('/with_auth', self.with_auth) app.router.add_options('/options_handler', self.ping_handler) app.router.add_head('/head_handler', self.ping_handler) @@ -55,6 +56,12 @@ async def check_headers(self, request: web.Request) -> web.Response: return web.Response(text='Ok!', status=200) + async def with_auth(self, request: web.Request) -> web.Response: + # BasicAuth("username", "password") + if request.headers.get('Authorization') != 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=': + return web.Response(text='incorrect auth', status=403) + return web.Response(text='Ok!', status=200) + @property def web_app(self) -> web.Application: return self._web_app diff --git a/tests/test_client.py b/tests/test_client.py index 83d54fa..be3b0ed 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -3,6 +3,7 @@ import pytest from aiohttp import ( + BasicAuth, ClientResponse, ClientResponseError, ClientSession, @@ -435,3 +436,15 @@ async def test_change_headers(aiohttp_client): assert test_app.counter == 2 await retry_client.close() + + +async def test_additional_params(aiohttp_client): + # https://github.com/inyutin/aiohttp_retry/issues/79 + auth = BasicAuth("username", "password") + retry_client, _ = await get_retry_client_and_test_app_for_test(aiohttp_client) + async with retry_client.request(hdrs.METH_GET, '/with_auth', auth=auth) as response: + text = await response.text() + assert response.status == 200 + assert text == 'Ok!' + + await retry_client.close() From f313aa31fe2651bfd312998566f58d3d92f4d6da Mon Sep 17 00:00:00 2001 From: Dmitry Inyutin Date: Thu, 11 Aug 2022 22:28:54 +0300 Subject: [PATCH 2/3] fix 2.8.0 #2 --- README.md | 8 ++++---- aiohttp_retry/client.py | 11 ++++++----- setup.py | 2 +- tests/test_client.py | 26 +++++++++++++++++++------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 07d9778..6d9de4e 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,11 @@ async def main(): params_list=[ RequestParams( method='GET', - path='https://ya.ru', + url='https://ya.ru', ), RequestParams( method='GET', - path='https://ya.ru', + url='https://ya.ru', headers={'some_header': 'some_value'}, ), ] @@ -193,7 +193,7 @@ for more info see [aiohttp doc](https://docs.aiohttp.org/en/stable/client_advanc @dataclass class RequestParams: method: str - path: _RAW_URL_TYPE + url: _RAW_URL_TYPE trace_request_ctx: Optional[Dict[str, Any]] = None kwargs: Optional[Dict[str, Any]] = None ``` @@ -209,7 +209,7 @@ def requests( You can find an example of usage above or in tests. But basically `RequestParams` is a structure to define params for `ClientSession.request` func. -`method`, `path`, `headers` `trace_request_ctx` defined outside kwargs, because they are popular. +`method`, `url`, `headers` `trace_request_ctx` defined outside kwargs, because they are popular. There is also an old way to change URL between retries by specifying ```url``` as list of urls. Example: ```python diff --git a/aiohttp_retry/client.py b/aiohttp_retry/client.py index 9d97af7..16f7dce 100644 --- a/aiohttp_retry/client.py +++ b/aiohttp_retry/client.py @@ -55,7 +55,7 @@ def exception(self, msg: str, *args: Any, **kwargs: Any) -> None: pass @dataclass class RequestParams: method: str - path: _RAW_URL_TYPE + url: _RAW_URL_TYPE headers: Optional[Dict[str, Any]] = None trace_request_ctx: Optional[Dict[str, Any]] = None kwargs: Optional[Dict[str, Any]] = None @@ -98,8 +98,8 @@ async def _do_request(self) -> ClientResponse: params = self._params_list[-1] response: ClientResponse = await self._request_func( - method=params.method, - path=params.path, + params.method, + params.url, headers=params.headers, trace_request_ctx={ 'current_attempt': current_attempt, @@ -351,10 +351,11 @@ def _make_request( url_list = _url_to_urls(url) params_list = [RequestParams( method=method, - path=path, + url=url, + headers=kwargs.pop('headers', {}), trace_request_ctx=kwargs.pop('trace_request_ctx', None), kwargs=kwargs, - ) for path in url_list] + ) for url in url_list] return self._make_requests( params_list=params_list, diff --git a/setup.py b/setup.py index e8be2ce..7f55ee2 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='aiohttp_retry', - version='2.8.1', + version='2.8.2', description='Simple retry client for aiohttp', long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/test_client.py b/tests/test_client.py index be3b0ed..a1d8b07 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -368,17 +368,17 @@ async def evaluate_response(response: ClientResponse) -> bool: assert test_app.counter == 3 -async def test_multiply_paths_by_requests(aiohttp_client): +async def test_multiply_urls_by_requests(aiohttp_client): retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client) async with retry_client.requests( params_list=[ RequestParams( method='GET', - path='/internal_error', + url='/internal_error', ), RequestParams( method='GET', - path='/ping', + url='/ping', ), ] ) as response: @@ -398,11 +398,11 @@ async def test_multiply_methods_by_requests(aiohttp_client): params_list=[ RequestParams( method='POST', - path='/ping', + url='/ping', ), RequestParams( method='GET', - path='/ping', + url='/ping', ), ] ) as response: @@ -420,11 +420,11 @@ async def test_change_headers(aiohttp_client): params_list=[ RequestParams( method='GET', - path='/check_headers', + url='/check_headers', ), RequestParams( method='GET', - path='/check_headers', + url='/check_headers', headers={'correct_headers': 'True'}, ), ] @@ -448,3 +448,15 @@ async def test_additional_params(aiohttp_client): assert text == 'Ok!' await retry_client.close() + + +async def test_request_headers(aiohttp_client): + retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client) + async with retry_client.get(url='/check_headers', headers={'correct_headers': 'True'}) as response: + text = await response.text() + assert response.status == 200 + assert text == 'Ok!' + + assert test_app.counter == 1 + + await retry_client.close() From ff0964a1a17459d6238148de21189676f788d9f3 Mon Sep 17 00:00:00 2001 From: Dmitry Inyutin Date: Thu, 11 Aug 2022 22:31:56 +0300 Subject: [PATCH 3/3] fix linters --- tests/test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_client.py b/tests/test_client.py index 6af04eb..a1d8b07 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -459,4 +459,4 @@ async def test_request_headers(aiohttp_client): assert test_app.counter == 1 - await retry_client.close() \ No newline at end of file + await retry_client.close()