Пример #1
0
async def async_get(url, retry=5):
    """
    请求指定域名,获取返回结果的json
    """
    try:
        async with async_limit:
            async with async_clint.get(url, timeout=300) as response:
                if response.status in (
                        200,
                        429,
                ):
                    json = await response.json()
                    if json.get("infocode") == '10000':
                        return json
                    else:
                        err_msg = "AMAP_ERROR: %s-%s" % (json.get("infocode"),
                                                         json.get("info"))
                        raise aiohttp.ClientResponseError(
                            response.request_info,
                            response.history,
                            message=err_msg)
                else:
                    print("HTTP_ERROR: %s" % response.status)
                    raise aiohttp.ClientResponseError()
    except Exception as err:
        if retry > 0:
            await asyncio.sleep(pow(2, 6 - retry))
            return await async_get(url, retry - 1)
        else:
            print("Retry out of chances: %s" % err)
            return None
Пример #2
0
    async def request(self, route: Route, **kwargs):
        method = route.method
        url = route.url

        for tries in range(5):
            try:
                async with self.__session.request(method, url, **kwargs) as r:
                    data = await json_or_text(r)

                    if 300 > r.status >= 200:
                        return data

                    if r.status == 429:
                        await asyncio.sleep(10)
                        continue

                    if r.status in {500, 502}:
                        await asyncio.sleep(2 * tries + 1)
                        continue

                    raise aiohttp.ClientResponseError(r.request_info, r.history, status=r.status)

            except OSError as e:
                if tries < 4 and e.errno in (54, 10054):
                    continue
                raise
        raise aiohttp.ClientResponseError(r.request_info, r.history, status=r.status)
Пример #3
0
async def test_messages(mock_send):
    """
    Test messages method : should send connect message and yield results.
    Should reconnect after yield result or timeouts.
    """
    messages = [
        {"channel": '/topic/Foo0'},
        {"channel": '/meta/connect'},
        {"channel": '/topic/Foo1'},
        {"channel": '/topic/Foo2'},
        {"channel": '/topic/Foo3'},
        {"channel": '/topic/Foo4'},
        {"channel": '/meta/connect'},
        {"channel": '/topic/Foo5'},
        {"channel": '/meta/connect'},
    ]
    mock_send.side_effect = [
        # First message : a simple success
        [messages[0]],
        # Timeout
        aiohttp.ClientResponseError(None, None, code=408),
        # Second message : multiples messages
        messages[1:4],
        # Timeout
        asyncio.TimeoutError(),
        # Other messages
        messages[4:]]

    client = SfStreamingTestClass()
    received_messages = []
    # We iterate message and ask stop after received 7 messages
    async for i, m in async_enumerate(client.messages()):
        received_messages.append(m)
        if i == 6:
            await client.ask_stop()

    # We should have only 7 messages
    assert received_messages == messages[:7]
    assert mock_send.call_count == 5

    client = SfStreamingTestClass()
    # An other error shoud be re-raised
    mock_send.side_effect = [
        aiohttp.ClientResponseError(None, None, code=404),
    ]
    with pytest.raises(aiohttp.ClientResponseError):
        async for m in client.messages():
            # Avoid infinite loop if test fail
            assert False
Пример #4
0
async def test_get_with_retries(mock_sleep):
    getfcn = CoroutineMock()
    getfcn.side_effect = [
        aiohttp.ClientResponseError(MagicMock(), MagicMock())] * 4 + ['hi']
    res = await nwp.get_with_retries(getfcn, retries=5)
    assert res == 'hi'
    assert getfcn.await_count == 5
Пример #5
0
async def test_forgot_password_view_aiohttp_error(mock_cognito, cloud_client):
    """Test unknown error while logging out."""
    mock_cognito.initiate_forgot_password.side_effect = aiohttp.ClientResponseError(
        Mock(), Mock())
    req = await cloud_client.post("/api/cloud/forgot_password",
                                  json={"email": "*****@*****.**"})
    assert req.status == 500
Пример #6
0
async def _raise_for_status(response):
    """Raise an appropriate error for a given response.

    Arguments:
      response (:py:class:`aiohttp.ClientResponse`): The API response.

    Raises:
      :py:class:`aiohttp.ClientResponseError`: The appropriate
        error for the response's status.
    """

    if 400 <= response.status:
        reason = response.reason

        spacetrack_error_msg = None

        try:
            json = await response.json()
            if isinstance(json, Mapping):
                spacetrack_error_msg = json['error']
        except (ValueError, KeyError, aiohttp.ClientResponseError):
            pass

        if not spacetrack_error_msg:
            spacetrack_error_msg = await response.text()

        if spacetrack_error_msg:
            reason += '\nSpace-Track response:\n' + spacetrack_error_msg

        raise aiohttp.ClientResponseError(
            response.request_info,
            response.history,
            status=response.status,
            message=reason,
            headers=response.headers)
Пример #7
0
 async def _raise_for_status(resp: aiohttp.ClientResponse) -> None:
     """Check resp for status and if error log additional info."""
     # Copied from aiohttp's raise_for_status() -- since it releases the
     # response payload, we need to grab the `resp.text` first to help users
     # debug.
     #
     # Useability/performance notes:
     # * grabbing the response can be slow for large files, only do it as
     #   needed
     # * we can't know in advance what encoding the files might have unless
     #   we're certain in advance that the result is an error payload from
     #   Google (otherwise, it could be a binary blob from GCS, for example)
     # * sometimes, errors are expected, so we should try to avoid polluting
     #   logs in that case
     #
     # https://github.com/aio-libs/aiohttp/blob/
     # 385b03ef21415d062886e1caab74eb5b93fdb887/aiohttp/
     # client_reqrep.py#L892-L902
     if resp.status >= 400:
         assert resp.reason is not None
         # Google's error messages are useful, pass 'em through
         body = await resp.text(errors='replace')
         resp.release()
         raise aiohttp.ClientResponseError(resp.request_info,
                                           resp.history,
                                           status=resp.status,
                                           message=f'{resp.reason}: {body}',
                                           headers=resp.headers)
Пример #8
0
async def test_form_invalid_auth(hass: HomeAssistant,
                                 aioclient_mock: AiohttpClientMocker) -> None:
    """Test we handle invalid auth error."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER})

    aioclient_mock.get(
        "http://1.1.1.1:8080/status.json?show_avail=1",
        exc=aiohttp.ClientResponseError(Mock(), (), status=401),
    )
    result2 = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {
            "host": "1.1.1.1",
            "port": 8080,
            "username": "******",
            "password": "******"
        },
    )

    assert result2["type"] == FlowResultType.FORM
    assert result2["errors"] == {
        "username": "******",
        "password": "******"
    }
Пример #9
0
async def raise_for_status(response):
    if 400 <= response.status:
        raise aiohttp.ClientResponseError(response.request_info,
                                          response.history,
                                          code=response.status,
                                          message=await response.text(),
                                          headers=response.headers)
Пример #10
0
 def raise_for_status(self):
     if self.status == 200:
         return None
     raise aiohttp.ClientResponseError(
         status=self.status,
         history=None,
         request_info=None,
     )
Пример #11
0
async def fetch(session, url: str, timeout: int = 10):
    log = logging.getLogger('fetch')
    with async_timeout.timeout(timeout):
        async with session.get(url) as response:
            if response.status < 400:
                return await response.text()
            else:
                raise aiohttp.ClientResponseError(response.request_info,
                                                  response.history)
Пример #12
0
async def test_stream_pty_raises_error_with_abnormal_status(mocker):
    mock_req_obj = asynctest.MagicMock(spec=Request)
    mock_exception = aiohttp.ClientResponseError(
        None, None, code=400, message='emulated-handshake-error')
    mock_req_obj.connect_websocket = \
        asynctest.MagicMock(side_effect=mock_exception)
    with asynctest.patch('ai.backend.client.asyncio.kernel.Request',
                         return_value=mock_req_obj) as mock_req_cls:
        with pytest.raises(BackendClientError):
            await Kernel('mykernel').stream_pty()
Пример #13
0
async def test_get_html_raise(mocker):
    """Сообщение об ошибке при некорректном URL."""
    fake_get_aiohttp_session = mocker.patch.object(resources, "get_aiohttp_session")
    fake_session = fake_get_aiohttp_session.return_value
    context_mng = fake_session.get.return_value
    respond = context_mng.__aenter__.return_value  # noqa: WPS609
    respond.raise_for_status = mocker.Mock(side_effect=aiohttp.ClientResponseError("", ""))

    with pytest.raises(outer.DataError, match=f"Данные {BAD_URL} не загружены"):
        await parser._get_html(BAD_URL)
Пример #14
0
 async def _check_empty(self, op: aiohttp.ClientResponse):
     await self._check_op(op)
     d = await op.text()
     if d:
         e = aiohttp.ClientResponseError(
             op.request_info,
             op.history,
             code=op.status,
             message="POST returned data, not empty",
             headers=op.headers)
         raise e
Пример #15
0
async def test_get_html_raise(mocker):
    """Сообщение об ошибке при некорректном URL."""
    fake_session = mocker.MagicMock()
    context_mng = fake_session.get.return_value
    respond = context_mng.__aenter__.return_value  # noqa: WPS609
    respond.raise_for_status = mocker.Mock(
        side_effect=aiohttp.ClientResponseError("", ""))

    with pytest.raises(description.ParserError,
                       match=f"Данные {BAD_URL} не загружены"):
        await parser.get_html(BAD_URL, fake_session)
Пример #16
0
 async def fake_ensure_valid_token(*args, **kwargs):
     raise aiohttp.ClientResponseError(
         request_info=aiohttp.client.RequestInfo(
             url="http://example.com",
             method="GET",
             headers={},
             real_url="http://example.com",
         ),
         code=400,
         history=(),
     )
 def maybe_fail(self, method, path, headers):
     if self.should_fail():
         raise aiohttp.ClientResponseError(
             status=503,
             message=
             'Service Unavailable from FailureInjectingClientSession',
             request_info=aiohttp.RequestInfo(url=path,
                                              method=method,
                                              headers=headers,
                                              real_url=path),
             history=())
Пример #18
0
def raise_not_200(response: aiohttp.ClientResponse) -> None:
    if response.status != 200:
        assert response.reason is not None
        response.release()
        raise aiohttp.ClientResponseError(
            response.request_info,
            response.history,
            status=response.status,
            message=response.reason,
            headers=response.headers,
        )
Пример #19
0
def aiohttp_raise_for_status(response: aiohttp.ClientResponse):
    # workaround aiohttp bug, can remove after fixed in aiohttp
    # issue: https://github.com/aio-libs/aiohttp/issues/3906
    if response.status >= 400:
        response.release()
        raise aiohttp.ClientResponseError(
            response.request_info,
            response.history,
            status=response.status,
            message=response.reason,
            headers=response.headers,
        )
async def test_download_from_github_bad_response_is_caught(mocker):
    # Setup
    mock_get_response: MagicMock = mocker.patch.object(aiohttp.ClientSession, 'get')
    mock_get_response.return_value.__aenter__.return_value.raise_for_status = MagicMock(
        side_effect=aiohttp.ClientResponseError(None, None))

    # Run
    returned_value = await github_releases_data._download_from_github()

    # Assert
    mock_get_response.assert_called()
    mock_get_response.return_value.__aenter__.return_value.raise_for_status.assert_called()
    assert returned_value is None
Пример #21
0
async def raise_for_status_body(resp):
    if resp.status >= 400:
        message = resp.reason
        body = await resp.text()
        if body:
            message += ": " + body
        raise aiohttp.ClientResponseError(
            resp.request_info,
            resp.history,
            status=resp.status,
            message=message,
            headers=resp.headers,
        )
Пример #22
0
 async def _check_op(self, op: aiohttp.ClientResponse):
     if op.status >= 200 and op.status <= 299:
         return
     e = aiohttp.ClientResponseError(op.request_info,
                                     op.history,
                                     code=op.status,
                                     message=op.reason,
                                     headers=op.headers)
     try:
         e.body = await op.json()
     except:
         pass
     raise e
Пример #23
0
async def command_corgi(client, message):
    """Fetch a corgi picture from Tumblr and post it."""
    blog_url = BLOG_URL_FORMAT.format(random.choice(CORGI_BLOGS), TUMBLR_KEY)

    # First, attempt to retrieve a random corgi image URL from a blog.
    try:
        session = aiohttp.ClientSession()

        async with session.get(blog_url) as url_response:
            if url_response.status == 200:
                image_url = get_random_image_url(await url_response.json())
            else:
                raise aiohttp.ClientResponseError(
                    "URL response status is not 200.")

        # Next, attempt to download that random image.
        async with session.get(image_url) as image_response:
            if image_response.status == 200:

                # Format the temporary file name with the file type
                # from the image URL.
                temporary_image = TEMPORARY_FILE_FORMAT.format(
                    image_url.rpartition(".")[2])

                with open(temporary_image, "wb") as writable_file:
                    writable_file.write(await image_response.read())
            else:
                raise aiohttp.ClientResponseError(
                    "Image response status is not 200.")

        # Finally, send the image to the client.
        await client.send_file(message.channel, temporary_image)

    # If something goes wrong, send a default corgi to the client.
    except Exception as exception:
        await client.send_file(message.channel, DEFAULT_IMAGE)

    finally:
        session.close()
Пример #24
0
    async def _check_json(self, op):
        """Check an operation for errors and convert errors to exceptions. Graph can
        return an HTTP failure code, or (rarely) a JSON error message and a 200 success."""
        await self._check_op(op)

        res = await op.json()
        if "error" in res:
            e = aiohttp.ClientResponseError(op.request_info,
                                            op.history,
                                            code=op.status,
                                            message=op.reason,
                                            headers=op.headers)
            e.body = res
            raise e
        return res
Пример #25
0
async def raise_for_status(response):
    if 400 <= response.status:
        e = aiohttp.ClientResponseError(response.request_info,
                                        response.history,
                                        code=response.status,
                                        headers=response.headers)

        if 'json' in response.headers.get('CONTENT-TYPE', ''):
            data = await response.json()
            e.message = str(data)
            raise UplinkResponseException(data.get('errorCode'), data) from e

        else:
            data = await response.text()
            raise UplinkException(data) from e
Пример #26
0
async def srcRequest(query):
    """Request info from speedrun.com.

    Use backoff to retry request when the request fails the first time
    (Unless the error is 404 or 420 also ignore 200 because its not error)
    """
    async with aiohttp.ClientSession() as session:
        async with session.get(
                "https://www.speedrun.com/api/v1{}".format(query)) as res:
            if res.status not in (200, 420, 404):
                print("D:")
                raise aiohttp.ClientResponseError(res.request_info,
                                                  res.history)
            _json = await res.json()
            return _json
Пример #27
0
async def test_setup_failed_invalid_auth(
    hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
    """Test integration failed due to invalid auth."""

    entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG_DATA)
    entry.add_to_hass(hass)
    aioclient_mock.get(
        "http://1.1.1.1:8080/status.json?show_avail=1",
        exc=aiohttp.ClientResponseError(Mock(), (), status=401),
    )

    await hass.config_entries.async_setup(entry.entry_id)

    assert entry.state == ConfigEntryState.SETUP_RETRY
 async def _request(self, method, url, **kwargs):
     if self._max_wait > 0:
         kwargs.setdefault('timeout', self._max_wait)
     async with self._session.request(method, url, **kwargs) as res:
         if 'application/json' in res.headers['CONTENT-TYPE'].lower():
             data = await res.json()
         else:
             data = await res.text()
         # this is almost like raise_for_status, except it reads response from server and attaches it to exception
         if 400 <= res.status:
             raise aiohttp.ClientResponseError(res.request_info,
                                               res.history,
                                               status=res.status,
                                               message=data,
                                               headers=res.headers)
         return data
async def callback(callback_url, data, is_successful):
    logger.debug('notifying external service via callback url = %s',
                 callback_url)
    translation_data = {
        'payload': data,
        'validation_successful': is_successful
    }
    async with aiohttp.ClientSession() as session:
        async with session.post(callback_url,
                                data=json.dumps(translation_data)) as resp:
            try:
                if resp.status >= 400:
                    raise aiohttp.ClientResponseError(resp.status)
            except aiohttp.ClientResponseError:
                logger.exception(
                    'Could not notify external service via callback: %s',
                    callback_url)
Пример #30
0
 def data_received(self, data):
     self.transport.close()
     response = data.decode()  # type: str
     lines = response.split('\r\n', 1)
     if not lines:
         return
     parts = lines[0].split(' ')
     if (len(parts) != 3):
         return
     if int(parts[1]) != 200:
         on_complete.set_exception(
             aiohttp.ClientResponseError(None,
                                         None,
                                         status=int(parts[1]),
                                         message=parts[2]))
     else:
         on_complete.set_result(True)