async def get_items(self, item_type, item_id_list, progressbar=None): async with AsyncOAuth1Client( client_id=self.config["app_token"], client_secret=self.config["app_secret"], token=self.config["access_token"], token_secret=self.config["access_token_secret"], timeout=self.config["cardmarket_request_timeout"], ) as client: tasks = [] sem = asyncio.Semaphore(100) for item_id in item_id_list: tasks.append( asyncio.ensure_future( self.fetch( sem, client, f"{self.base_url}/{item_type}/{str(item_id)}", f"{self.base_url}/{item_type}/", item_type, item_id, progressbar, ) ) ) responses = await asyncio.gather(*tasks, return_exceptions=False) return responses
async def test_fetch_request_token_via_header(): request_token = {'oauth_token': '1', 'oauth_token_secret': '2'} def assert_func(request): auth_header = request.headers.get('authorization') assert 'oauth_consumer_key="id"' in auth_header assert 'oauth_signature=' in auth_header dispatch = MockDispatch(request_token, assert_func=assert_func) async with AsyncOAuth1Client('id', 'secret', dispatch=dispatch) as client: response = await client.fetch_request_token(oauth_url) assert response == request_token
async def test_get_via_header(): mock_response = MockDispatch(b'hello') async with AsyncOAuth1Client( 'id', 'secret', token='foo', token_secret='bar', dispatch=mock_response, ) as client: response = await client.get('https://example.com/') assert response.content == b'hello' request = response.request auth_header = request.headers.get('authorization') assert 'oauth_token="foo"' in auth_header assert 'oauth_consumer_key="id"' in auth_header assert 'oauth_signature=' in auth_header
async def test_fetch_request_token_via_body(): request_token = {'oauth_token': '1', 'oauth_token_secret': '2'} def assert_func(request): auth_header = request.headers.get('authorization') assert auth_header is None assert b'oauth_consumer_key=id' in request.content assert b'&oauth_signature=' in request.content mock_response = MockDispatch(request_token, assert_func=assert_func) async with AsyncOAuth1Client( 'id', 'secret', signature_type=SIGNATURE_TYPE_BODY, dispatch=mock_response, ) as client: response = await client.fetch_request_token(oauth_url) assert response == request_token
async def test_get_via_query(): mock_response = MockDispatch(b'hello') async with AsyncOAuth1Client( 'id', 'secret', token='foo', token_secret='bar', signature_type=SIGNATURE_TYPE_QUERY, dispatch=mock_response, ) as client: response = await client.get('https://example.com/') assert response.content == b'hello' request = response.request auth_header = request.headers.get('authorization') assert auth_header is None url = str(request.url) assert 'oauth_token=foo' in url assert 'oauth_consumer_key=id' in url assert 'oauth_signature=' in url
async def test_fetch_request_token_via_query(): request_token = {'oauth_token': '1', 'oauth_token_secret': '2'} async def assert_func(request): auth_header = request.headers.get('authorization') assert auth_header is None url = str(request.url) assert 'oauth_consumer_key=id' in url assert '&oauth_signature=' in url mock_response = MockDispatch(request_token, assert_func=assert_func) async with AsyncOAuth1Client( 'id', 'secret', signature_type=SIGNATURE_TYPE_QUERY, app=mock_response, ) as client: response = await client.fetch_request_token(oauth_url) assert response == request_token
async def test_get_via_body(): async def assert_func(request): content = await request.body() assert b'oauth_token=foo' in content assert b'oauth_consumer_key=id' in content assert b'oauth_signature=' in content mock_response = MockDispatch(b'hello', assert_func=assert_func) async with AsyncOAuth1Client( 'id', 'secret', token='foo', token_secret='bar', signature_type=SIGNATURE_TYPE_BODY, app=mock_response, ) as client: response = await client.post('https://example.com/') assert response.content == b'hello' request = response.request auth_header = request.headers.get('authorization') assert auth_header is None
async def test_fetch_access_token(): request_token = {'oauth_token': '1', 'oauth_token_secret': '2'} def assert_func(request): auth_header = request.headers.get('authorization') assert 'oauth_verifier="d"' in auth_header assert 'oauth_token="foo"' in auth_header assert 'oauth_consumer_key="id"' in auth_header assert 'oauth_signature=' in auth_header mock_response = MockDispatch(request_token, assert_func=assert_func) async with AsyncOAuth1Client( 'id', 'secret', token='foo', token_secret='bar', dispatch=mock_response, ) as client: with pytest.raises(OAuthError): await client.fetch_access_token(oauth_url) response = await client.fetch_access_token(oauth_url, verifier='d') assert response == request_token