def _handle_response(raw_response: aiohttp.ClientResponse) -> List[dict]: """From a raw response, return a list of extracted sites in dictionary form. Relevant dictionary keys are: "usgs_site_code" "variableName" "measurement_unit" "values" "series" Parameters ---------- raw_response : aiohttp.ClientResponse Request GET response Returns ------- List[dict] A list of handled responses """ # TODO: Speed test using orjson instead of native deserialized_response = raw_response.json() def extract_metadata(json_time_series): return { # Add site code "usgs_site_code": json_time_series["sourceInfo"]["siteCode"][0]["value"], # Add variable name "variableName": IVDataService.simplify_variable_name( json_time_series["variable"]["variableName"]), # Add units "measurement_unit": json_time_series["variable"]["unit"]["unitCode"], } flattened_data = [] for response_value_timeSeries in deserialized_response["value"][ "timeSeries"]: for indicies, site_data in enumerate( response_value_timeSeries["values"]): # Create general site metadata dictionary site_metadata = extract_metadata(response_value_timeSeries) # Add site time series values and its index number site_metadata.update({ "values": site_data["value"], "series": indicies }) flattened_data.append(site_metadata) return flattened_data
async def test_authenticate(): st = AsyncSpaceTrackClient('identity', 'wrongpassword') loop = asyncio.get_event_loop() response = ClientResponse( 'post', ST_URL / 'ajaxauth/login', request_info=Mock(), writer=Mock(), continue100=None, timer=TimerNoop(), traces=[], loop=loop, session=st.session, ) response.status = 200 response.json = Mock() async def mock_post(url, data): response.json.return_value = asyncio.Future() if data['password'] == 'wrongpassword': response.json.return_value.set_result({'Login': '******'}) elif data['password'] == 'unknownresponse': # Space-Track doesn't respond like this, but make sure anything # other than {'Login': '******'} doesn't raise AuthenticationError response.json.return_value.set_result({'Login': '******'}) else: response.json.return_value.set_result('') return response async with st: with patch.object(st.session, 'post', mock_post): with pytest.raises(AuthenticationError): await st.authenticate() assert response.json.call_count == 1 st.password = '******' await st.authenticate() # This shouldn't make a HTTP request since we're already authenticated. await st.authenticate() assert response.json.call_count == 2 st = AsyncSpaceTrackClient('identity', 'unknownresponse') async with st: with patch.object(st.session, 'post', mock_post): await st.authenticate() response.close()
async def _handle_response(resp: aiohttp.ClientResponse) -> Union[dict, bytes]: if 200 <= resp.status <= 299: is_json = resp.content_type == "application/json" return await (resp.json() if is_json else resp.content.read()) else: content = await resp.content.read() msg = f"Failed to get response with status code: {resp.status}: {content}" if resp.status == 422: raise HttpUnprocessableEntity() elif 400 <= resp.status <= 499: raise BadRequest(msg) else: raise IOError(msg)
def _patch_get( self, client_response: aiohttp.ClientResponse) -> aiohttp.ClientResponse: """ Wrap aiohttp.ClientResponse text and json coros in run_until_complete. Monkeypatch text and json with wrappers.""" # May iter through methods and wrap all coro's in the future # however that may not work if a non-coro returns a async context manager for example text = self._wrap_coro_in_callable(client_response.text) json = self._wrap_coro_in_callable(client_response.json) # resign signatures text = forge.copy(aiohttp.ClientResponse.text, exclude="self")(text) json = forge.copy(aiohttp.ClientResponse.json, exclude="self")(json) client_response.text = text client_response.json = json return client_response
async def test_authenticate(): st = AsyncSpaceTrackClient('identity', 'wrongpassword') loop = asyncio.get_event_loop() response = ClientResponse( 'post', URL('https://www.space-track.org/ajaxauth/login')) # aiohttp 2.2 uses session try: response._post_init(loop) except TypeError: response._post_init(loop, st.session) response.status = 200 response.json = Mock() async def mock_post(url, data): response.json.return_value = asyncio.Future() if data['password'] == 'wrongpassword': response.json.return_value.set_result({'Login': '******'}) elif data['password'] == 'unknownresponse': # Space-Track doesn't respond like this, but make sure anything # other than {'Login': '******'} doesn't raise AuthenticationError response.json.return_value.set_result({'Login': '******'}) else: response.json.return_value.set_result('') return response with st, patch.object(st.session, 'post', mock_post): with pytest.raises(AuthenticationError): await st.authenticate() assert response.json.call_count == 1 st.password = '******' await st.authenticate() # This shouldn't make a HTTP request since we're already authenticated. await st.authenticate() assert response.json.call_count == 2 st = AsyncSpaceTrackClient('identity', 'unknownresponse') with st, patch.object(st.session, 'post', mock_post): await st.authenticate() response.close()
async def test_authenticate(): st = AsyncSpaceTrackClient('identity', 'wrongpassword') loop = asyncio.get_event_loop() response = ClientResponse( 'post', 'https://www.space-track.org/ajaxauth/login') response._post_init(loop) response.status = 200 response.json = Mock() async def mock_post(url, data): response.json.return_value = asyncio.Future() if data['password'] == 'wrongpassword': response.json.return_value.set_result({'Login': '******'}) elif data['password'] == 'unknownresponse': # Space-Track doesn't respond like this, but make sure anything # other than {'Login': '******'} doesn't raise AuthenticationError response.json.return_value.set_result({'Login': '******'}) else: response.json.return_value.set_result('') return response with st, patch.object(st.session, 'post', mock_post): with pytest.raises(AuthenticationError): await st.authenticate() assert response.json.call_count == 1 st.password = '******' await st.authenticate() # This shouldn't make a HTTP request since we're already authenticated. await st.authenticate() assert response.json.call_count == 2 st = AsyncSpaceTrackClient('identity', 'unknownresponse') with st, patch.object(st.session, 'post', mock_post): await st.authenticate() response.close()
def get_json(response: aiohttp.ClientResponse) -> Awaitable[Any]: return response.json()
async def test_generic_request(): def mock_authenticate(self): result = asyncio.Future() result.set_result(None) return result def mock_download_predicate_data(self, class_, controller=None): result = asyncio.Future() data = [{ 'Default': '0000-00-00 00:00:00', 'Extra': '', 'Field': 'PUBLISH_EPOCH', 'Key': '', 'Null': 'NO', 'Type': 'datetime' }, { 'Default': '', 'Extra': '', 'Field': 'TLE_LINE1', 'Key': '', 'Null': 'NO', 'Type': 'char(71)' }, { 'Default': '', 'Extra': '', 'Field': 'TLE_LINE2', 'Key': '', 'Null': 'NO', 'Type': 'char(71)' }] result.set_result(data) return result st = AsyncSpaceTrackClient('identity', 'password') loop = asyncio.get_event_loop() response = ClientResponse( 'get', ST_URL / 'basicspacedata/query/class/tle_publish/format/tle', request_info=Mock(), writer=Mock(), continue100=None, timer=TimerNoop(), traces=[], loop=loop, session=st.session, ) tle = ( '1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927\r\n' '2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537\r\n' ) normalised_tle = tle.replace('\r\n', '\n') response.status = 200 response.text = Mock() response.text.return_value = asyncio.Future() response.text.return_value.set_result(tle) mock_get = asyncio.Future() mock_get.set_result(response) patch_authenticate = patch.object(AsyncSpaceTrackClient, 'authenticate', mock_authenticate) patch_download_predicate_data = patch.object(AsyncSpaceTrackClient, '_download_predicate_data', mock_download_predicate_data) patch_get = patch.object(st.session, 'get', return_value=mock_get) with patch_authenticate, patch_download_predicate_data, patch_get: assert await st.tle_publish(format='tle') == normalised_tle response.close() response = ClientResponse( 'get', ST_URL / 'basicspacedata/query/class/tle_publish', request_info=Mock(), writer=Mock(), continue100=None, timer=TimerNoop(), traces=[], loop=loop, session=st.session, ) response.status = 200 response.json = Mock() response.json.return_value = asyncio.Future() response.json.return_value.set_result({'a': 5}) mock_get = asyncio.Future() mock_get.set_result(response) patch_get = patch.object(st.session, 'get', return_value=mock_get) with patch_authenticate, patch_download_predicate_data, patch_get: result = await st.tle_publish() assert result['a'] == 5 response.close() await st.close()
async def test_generic_request(): def mock_authenticate(self): result = asyncio.Future() result.set_result(None) return result def mock_download_predicate_data(self, class_): result = asyncio.Future() data = [ { 'Default': '0000-00-00 00:00:00', 'Extra': '', 'Field': 'PUBLISH_EPOCH', 'Key': '', 'Null': 'NO', 'Type': 'datetime' }, { 'Default': '', 'Extra': '', 'Field': 'TLE_LINE1', 'Key': '', 'Null': 'NO', 'Type': 'char(71)' }, { 'Default': '', 'Extra': '', 'Field': 'TLE_LINE2', 'Key': '', 'Null': 'NO', 'Type': 'char(71)' } ] result.set_result(data) return result st = AsyncSpaceTrackClient('identity', 'password') loop = asyncio.get_event_loop() response = ClientResponse( 'get', 'https://www.space-track.org/basicspacedata/query/class' '/tle_publish/format/tle') response._post_init(loop) tle = ( '1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927\r\n' '2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537\r\n') normalised_tle = tle.replace('\r\n', '\n') response.status = 200 response.text = Mock() response.text.return_value = asyncio.Future() response.text.return_value.set_result(tle) mock_get = asyncio.Future() mock_get.set_result(response) patch_authenticate = patch.object( AsyncSpaceTrackClient, 'authenticate', mock_authenticate) patch_download_predicate_data = patch.object( AsyncSpaceTrackClient, '_download_predicate_data', mock_download_predicate_data) patch_get = patch.object(st.session, 'get', return_value=mock_get) with patch_authenticate, patch_download_predicate_data, patch_get: assert await st.tle_publish(format='tle') == normalised_tle response.close() response = ClientResponse( 'get', 'https://www.space-track.org/basicspacedata/query/class' '/tle_publish') response._post_init(loop) response.status = 200 response.json = Mock() response.json.return_value = asyncio.Future() response.json.return_value.set_result({'a': 5}) mock_get = asyncio.Future() mock_get.set_result(response) patch_get = patch.object(st.session, 'get', return_value=mock_get) with patch_authenticate, patch_download_predicate_data, patch_get: result = await st.tle_publish() assert result['a'] == 5 response.close() st.close()