Python源码示例:aiohttp.ClientResponseError()

示例1
def add_safe(self, name, url, author_id):
		"""Try to add an emote. Returns a string that should be sent to the user."""
		if not re.fullmatch(r'\w{2,32}', name, re.ASCII):
			return _(
				'{name} is not a valid emote name; use 2–32 English letters, numbers and underscores.'
			).format(name=discord.utils.escape_mentions(name))
		try:
			emote = await self.add_from_url(name, url, author_id)
		except discord.HTTPException as ex:
			return (
				_('An error occurred while creating the emote:\n')
				+ utils.format_http_exception(ex))
		except ValueError:
			return _('Error: Invalid URL.')
		except aiohttp.ServerDisconnectedError:
			return _('Error: The connection was closed early by the remote host.')
		except aiohttp.ClientResponseError as exc:
			raise errors.HTTPException(exc.status)
		else:
			return _('Emote {emote} successfully created.').format(emote=emote) 
示例2
def get_text_from_url(self, url: str) -> str:
        """Fetch content at **url** and return as text

        - On non-permanent errors (429, 502, 503, 504), the GET is retried 10 times with
          increasing wait times according to fibonacci series.
        - Permanent errors raise a ClientResponseError
        """
        if self.cache and url in self.cache["url_text"]:
            return self.cache["url_text"][url]

        async with self.session.get(url) as resp:
            resp.raise_for_status()
            res = await resp.text()

        if self.cache:
            self.cache["url_text"][url] = res

        return res 
示例3
def refresh_token(self, refresh_token):
        """
        :param refresh_token: an openid refresh-token from a previous token request
        """
        async with self._client_session() as client:
            well_known = await self._get_well_known(client)

            try:
                return await self._post(
                    client,
                    well_known['token_endpoint'],
                    data={
                        'grant_type': GRANT_TYPE_REFRESH_TOKEN,
                        'refresh_token': refresh_token,
                    }
                )
            except aiohttp.ClientResponseError:
                raise ConfigException('oidc: failed to refresh access token') 
示例4
def test_single_proxy(self, proxy):
        """
        text one proxy, if valid, put them to usable_proxies.
        """
        try:
            async with aiohttp.ClientSession() as session:
                try:
                    if isinstance(proxy, bytes):
                        proxy = proxy.decode('utf-8')
                    real_proxy = 'http://' + proxy
                    print('Testing', proxy)
                    async with session.get(self.test_api, proxy=real_proxy, timeout=get_proxy_timeout) as response:
                        if response.status == 200:
                            self._conn.put(proxy)
                            print('Valid proxy', proxy)
                except (ProxyConnectionError, TimeoutError, ValueError):
                    print('Invalid proxy', proxy)
        except (ServerDisconnectedError, ClientResponseError,ClientConnectorError) as s:
            print(s)
            pass 
示例5
def _raise_response_exceptions(response):
    try:
        response.raise_for_status()
    except ClientResponseError as err:
        if err.status == 422:
            raise AugustApiAIOHTTPError(
                "The operation failed because the bridge (connect) is offline.",
            ) from err
        if err.status == 423:
            raise AugustApiAIOHTTPError(
                "The operation failed because the bridge (connect) is in use.",
            ) from err
        if err.status == 408:
            raise AugustApiAIOHTTPError(
                "The operation timed out because the bridge (connect) failed to respond.",
            ) from err
        raise err 
示例6
def get_picture_urls(dates, verbose=False):
    semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)
    tasks = [get_picture_url(date, semaphore) for date in dates]
    urls = []
    count = 0
    # get results as jobs are done
    for job in asyncio.as_completed(tasks, timeout=GLOBAL_TIMEOUT):
        try:
            url = yield from job
        except NoPictureForDate as exc:
            if verbose:
                print('*** {!r} ***'.format(exc))
            continue
        except aiohttp.ClientResponseError as exc:
            print('****** {!r} ******'.format(exc))
            continue
        count += 1
        if verbose:
            print(format(count, '3d'), end=' ')
            print(url.split('/')[-1])
        else:
            print(url)
        urls.append(url)
    return urls 
示例7
def _get(self, url, data=None, headers=None, method='GET'):
        page = ''
        try:
            timeout = aiohttp.ClientTimeout(total=self._timeout)
            async with self._sem_provider, self._session.request(
                method, url, data=data, headers=headers, timeout=timeout
            ) as resp:
                page = await resp.text()
                if resp.status != 200:
                    log.debug(
                        'url: %s\nheaders: %s\ncookies: %s\npage:\n%s'
                        % (url, resp.headers, resp.cookies, page)
                    )
                    raise BadStatusError('Status: %s' % resp.status)
        except (
            UnicodeDecodeError,
            BadStatusError,
            asyncio.TimeoutError,
            aiohttp.ClientOSError,
            aiohttp.ClientResponseError,
            aiohttp.ServerDisconnectedError,
        ) as e:
            page = ''
            log.debug('%s is failed. Error: %r;' % (url, e))
        return page 
示例8
def fetch(self, session, url):
        """Fetch URL.
        
        :param session: aiohttp.ClientSession
        :param url: URL
        :return: Response in JSON
        """
        print(url)
        try:
            with async_timeout.timeout(Settings.timeout):
                async with session.get(url) as response:
                    return await response.json()
        except asyncio.TimeoutError:
            return None
        except aiohttp.ClientResponseError:
            return None 
示例9
def __init__(self, exception):
        if isinstance(exception, HTTPError):
            super().__init__(
                "Error by connection with Instagram to '%s' with response code '%s'" % (
                    exception.request.url,
                    exception.response.status_code,
                ),
            )
            self.request = exception.request
            self.response = exception.response
        elif isinstance(exception, ClientResponseError):
            super().__init__(
                "Error by connection with Instagram to '%s' with response code '%s'" % (
                    exception.request_info.real_url,
                    exception.status,
                ),
            ) 
示例10
def hastebin(self, ctx, *, message):
        """Upload text to hastebin"""
        haste_url = os.environ.get("HASTE_URL", "https://hasteb.in")

        try:
            async with self.bot.session.post(
                haste_url + "/documents", data=message
            ) as resp:
                key = (await resp.json())["key"]
                embed = Embed(
                    title="Your uploaded file",
                    color=self.bot.main_color,
                    description=f"{haste_url}/" + key,
                )
        except (JSONDecodeError, ClientResponseError, IndexError):
            embed = Embed(
                color=self.bot.main_color,
                description="Something went wrong. "
                "We're unable to upload your text to hastebin.",
            )
            embed.set_footer(text="Hastebin Plugin")
        await ctx.send(embed=embed) 
示例11
def request(self, method, url, data, headers, **kwargs):
        try:
            # print(url)
            with async_timeout.timeout(TIMEOUT):
                async with self._session.request(
                    method, url, headers=headers, data=data
                ) as response:
                    # print(response)
                    if response.status == 200 or response.status == 202:
                        return await response.json(loads=json_loads)
                    else:
                        raise ClientResponseError(
                            response.request_info,
                            response.history,
                            status=response.status,
                            message=response.reason,
                        )
        except TimeoutError:
            raise TimeoutError("Timeout error")
        except Exception:
            raise 
示例12
def download(self, session, update):
        if self.path is None:
            return
        if update and not await self.needsUpdate():
            return

        self.path.parent.mkdir(parents=True, exist_ok=True)

        async with session.get(self.url) as response:
            try:
                response.raise_for_status()
            except aiohttp.ClientResponseError as e:
                # I don't like returning Exceptions, but I can't find a better way to pass a single error in an async loop
                return (self, e)
            with open(self.path, "wb") as out_file:
                while True:
                    chunk = await response.content.read(8192)
                    if not chunk:
                        break
                    out_file.write(chunk)

        url_timestamp = getTimestamp(self.lastModified)
        os.utime(self.path, (url_timestamp, url_timestamp))
        return (self, None) 
示例13
def read_crd(
        *,
        resource: resources.Resource,
        default: Union[_T, _UNSET] = _UNSET.token,
        context: Optional[auth.APIContext] = None,  # injected by the decorator
) -> Union[bodies.RawBody, _T]:
    if context is None:
        raise RuntimeError("API instance is not injected by the decorator.")

    try:
        response = await context.session.get(
            url=CRD_CRD.get_url(server=context.server, name=resource.name),
        )
        response.raise_for_status()
        respdata = await response.json()
        return cast(bodies.RawBody, respdata)

    except aiohttp.ClientResponseError as e:
        if e.status in [403, 404] and not isinstance(default, _UNSET):
            return default
        raise 
示例14
def get_picture_urls(dates, verbose=False):
    semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)
    tasks = [get_picture_url(date, semaphore) for date in dates]
    urls = []
    count = 0
    # get results as jobs are done
    for job in asyncio.as_completed(tasks, timeout=GLOBAL_TIMEOUT):
        try:
            url = yield from job
        except NoPictureForDate as exc:
            if verbose:
                print('*** {!r} ***'.format(exc))
            continue
        except aiohttp.ClientResponseError as exc:
            print('****** {!r} ******'.format(exc))
            continue
        count += 1
        if verbose:
            print(format(count, '3d'), end=' ')
            print(url.split('/')[-1])
        else:
            print(url)
        urls.append(url)
    return urls 
示例15
def _parse(response):
    try:
        data = await response.json()
        if data is None:
            raise ValueError()
    except (ValueError, json.JSONDecodeError, aiohttp.ClientResponseError):
        text = await response.text()
        raise exception.BadHTTPResponse(response.status, text, response)

    if data['ok']:
        return data['result']
    else:
        description, error_code = data['description'], data['error_code']

        # Look for specific error ...
        for e in exception.TelegramError.__subclasses__():
            n = len(e.DESCRIPTION_PATTERNS)
            if any(map(re.search, e.DESCRIPTION_PATTERNS, n*[description], n*[re.IGNORECASE])):
                raise e(description, error_code, data)

        # ... or raise generic error
        raise exception.TelegramError(description, error_code, data) 
示例16
def _check_url_async(url: str, session: ClientSession) -> UrlResult:
    """
    Connect to URL and return response status.

    Parameters
    ----------
    url : str
        URL to check
    session : ClientSession
        aiohttp client session

    Returns
    -------
    UrlResult
        Tuple of status code, redirect history, requested url,
        status/error message.

    """
    try:
        async with session.get(url) as resp:
            try:
                await resp.read()
                if resp.history:
                    result = UrlResult(
                        resp.status,
                        resp.history,
                        url,
                        "No error. Redirect to " + str(resp.url),
                    )
                elif resp.status == 200:
                    result = UrlResult(
                        resp.status, resp.history, url, "No error. No redirect."
                    )
                else:
                    result = UrlResult(resp.status, resp.history, url, "Error?")
            except ClientResponseError as client_err:
                return UrlResult(client_err.status, [], url, client_err)
    except ClientConnectionError as err:
        result = UrlResult(404, [], url, err)
    return result 
示例17
def get_versions(self, recipe: Recipe, source: Mapping[Any, Any],
                           source_idx: int):
        """Select hosters and retrieve versions for this source"""
        urls = source.get("url")
        if not urls:
            raise self.NoUrlInSource(recipe, source_idx+1)
        if isinstance(urls, str):
            urls = [urls]

        version_map: Dict[str, Dict[str, Any]] = defaultdict(dict)
        for url in urls:
            config = self.get_config(recipe)
            hoster = self.hoster_factory(url, config.get("override", {}))
            if not hoster:
                self.unparsed_urls += [url]
                continue
            logger.debug("Scanning with %s", hoster.__class__.__name__)
            try:
                versions = await hoster.get_versions(self.pipeline.req, recipe.orig.version)
                for match in versions:
                    match['hoster'] = hoster
                    version_map[match["version"]][url] = match
            except ClientResponseError as exc:
                logger.debug("HTTP %s when getting %s", exc, url)

        if not version_map:
            raise self.NoRecognizedSourceUrl(recipe, source_idx+1)

        return version_map 
示例18
def leave_room(self, user: User, room: Room) -> bool:
        """Remove **user** from **room**"""
        try:
            await self._make_request('DELETE', self._ROOM_USERS,
                                     {'roomId': room.id, 'userId': user.id})
        except aiohttp.ClientResponseError as exc:
            if exc.code in (404,):
                return False
        return True 
示例19
def fetch(self, uri):
        sleep_times = self.sleeps(uri)
        self._exns = set()
        self._state = state.green
        for s in sleep_times:
            headers = {"Accept": "application/json"}
            params = {"embed": "body"}
            try:
                async with self.session.get(uri, params=params, headers=headers) as response:
                    # 200 OK? Return the Page
                    if(response.status == 200):
                        self._log.debug("Fetched %s", uri)
                        js = await response.json()
                        return Page(js, self._log)
            # Wonky URI? Raise to caller
            except ValueError as e:
                raise UrlError(e)
            # Error from the HTTP response?
            except aiohttp.ClientResponseError as e:
                self.log(e, uri)
                # For a 404, raise HttpNotFound
                if e.code == 404:
                    raise HttpNotFoundError(uri, 404)
                # For a client error other than a timeout, raise HttpClientError
                # Timeouts should log and sleep
                if e.code < 500 and e.code != 408:
                    raise HttpClientError(uri, e.code)
            # Other connection errors and malformed payloads just log and sleep
            except (aiohttp.ClientError) as e:
                self.log(e, uri)
            # Http timeout? Log and sleep
            except asyncio.TimeoutError as e:
                self.log(e, uri)

            await self.sleep(s) 
示例20
def fail_with_client_response_error():
    raise aiohttp.ClientResponseError(None, "Darn it, something went bad") 
示例21
def _get_well_known(self, client):
        if self._well_known is None:
            try:
                self._well_known = await self._get(
                    client,
                    '{}/.well-known/openid-configuration'.format(self._issuer_url.rstrip('/'))
                )
            except aiohttp.ClientResponseError:
                raise ConfigException('oidc: failed to query well-known metadata endpoint')

        return self._well_known 
示例22
def get(self, path):
        try:
            return await self.client.get(path)
        except exceptions.HTTPClientException as e:
            from aiohttp import ClientResponseError
            cause = e.__cause__
            if isinstance(cause, ClientResponseError):
                if cause.code in self.throttling_error_codes:
                    Logger.third_party.warning('throttling %s' % self.__class__.__name__)
                else:
                    Logger.third_party.exception('Error on %s: %s' % (self.__class__.__name__, e.__cause__))
                self._increase_errors() 
示例23
def debug_hastebin(self, ctx):
        """Posts application-logs to Hastebin."""

        haste_url = os.environ.get("HASTE_URL", "https://hasteb.in")
        log_file_name = self.bot.token.split(".")[0]

        with open(
            os.path.join(
                os.path.dirname(os.path.abspath(__file__)), f"../temp/{log_file_name}.log"
            ),
            "rb+",
        ) as f:
            logs = BytesIO(f.read().strip())

        try:
            async with self.bot.session.post(haste_url + "/documents", data=logs) as resp:
                data = await resp.json()
                try:
                    key = data["key"]
                except KeyError:
                    logger.error(data["message"])
                    raise
                embed = discord.Embed(
                    title="Debug Logs",
                    color=self.bot.main_color,
                    description=f"{haste_url}/" + key,
                )
        except (JSONDecodeError, ClientResponseError, IndexError, KeyError):
            embed = discord.Embed(
                title="Debug Logs",
                color=self.bot.main_color,
                description="Something's wrong. We're unable to upload your logs to hastebin.",
            )
            embed.set_footer(text="Go to Heroku to see your logs.")
        await ctx.send(embed=embed) 
示例24
def request(
        self,
        url: str,
        method: str = "GET",
        payload: dict = None,
        return_response: bool = False,
        headers: dict = None,
    ) -> Union[ClientResponse, dict, str]:
        """
        Makes a HTTP request.

        Parameters
        ----------
        url : str
            The destination URL of the request.
        method : str
            The HTTP method (POST, GET, PUT, DELETE, FETCH, etc.).
        payload : Dict[str, Any]
            The json payload to be sent along the request.
        return_response : bool
            Whether the `ClientResponse` object should be returned.
        headers : Dict[str, str]
            Additional headers to `headers`.

        Returns
        -------
        ClientResponse or Dict[str, Any] or List[Any] or str
            `ClientResponse` if `return_response` is `True`.
            `dict` if the returned data is a json object.
            `list` if the returned data is a json list.
            `str` if the returned data is not a valid json data,
            the raw response.
        """
        async with self.session.request(method, url, headers=headers, json=payload) as resp:
            if return_response:
                return resp
            try:
                return await resp.json()
            except (JSONDecodeError, ClientResponseError):
                return await resp.text() 
示例25
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,
        ) 
示例26
def handle_exception():
    """
    Context manager translating network related exceptions
    to custom :mod:`~galaxy.api.errors`.
    """
    try:
        yield
    except asyncio.TimeoutError:
        raise BackendTimeout()
    except aiohttp.ServerDisconnectedError:
        raise BackendNotAvailable()
    except aiohttp.ClientConnectionError:
        raise NetworkError()
    except aiohttp.ContentTypeError:
        raise UnknownBackendResponse()
    except aiohttp.ClientResponseError as error:
        if error.status == HTTPStatus.UNAUTHORIZED:
            raise AuthenticationRequired()
        if error.status == HTTPStatus.FORBIDDEN:
            raise AccessDenied()
        if error.status == HTTPStatus.SERVICE_UNAVAILABLE:
            raise BackendNotAvailable()
        if error.status == HTTPStatus.TOO_MANY_REQUESTS:
            raise TooManyRequests()
        if error.status >= 500:
            raise BackendError()
        if error.status >= 400:
            logging.warning(
                "Got status %d while performing %s request for %s",
                error.status, error.request_info.method, str(error.request_info.url)
            )
            raise UnknownError()
    except aiohttp.ClientError:
        logging.exception("Caught exception while performing request")
        raise UnknownError() 
示例27
def test_async_convert_invalid(aioclient_mock, cloud_mock):
    """Test async convert."""
    aioclient_mock.post(CONVERT_URL, status=500, text="")
    with pytest.raises(aiohttp.ClientResponseError) as excinfo:
        await thingtalk.async_convert(cloud_mock, "Hello")

    assert excinfo.value.status == 500 
示例28
def get_package_metadata(self, package_name: str, serial: int = 0) -> Any:
        try:
            metadata_generator = self.get(f"/pypi/{package_name}/json", serial)
            metadata_response = await metadata_generator.asend(None)
            metadata = await metadata_response.json()
            return metadata
        except aiohttp.ClientResponseError as e:
            if e.status == 404:
                raise PackageNotFound(f"{package_name} no longer exists on PyPI")
            raise 
示例29
def _post(self, data):
        async with async_timeout.timeout(self.timeout):
            response = await self._session.post(self.url, data=data, headers=self._headers)

            if response.status == CSRF_ERROR_CODE:
                # Send request again with CSRF header
                self._headers[CSRF_HEADER] = response.headers[CSRF_HEADER]
                log.debug('Setting CSRF header: %s = %s',
                          CSRF_HEADER, response.headers[CSRF_HEADER])
                await response.release()
                return await self._post(data)

            elif response.status == AUTH_ERROR_CODE:
                await response.release()
                log.debug('Authentication failed: %s: user=%r, password=%r',
                          self.url, self.user, self.password)
                raise AuthError(self.url)

            else:
                import aiohttp
                try:
                    answer = await response.json()
                except aiohttp.ClientResponseError:
                    raise RPCError('Server sent malformed JSON: %s' % await response.text())
                else:
                    return answer 
示例30
def get_and_post(paste_url: str):
    """Get the pasted content from a paste service and repaste it to nekobin.
    
    Returns a `str` of the key saved on nekobin or an error code as `int`.
    """
    async with aiohttp.ClientSession() as session:
        async with session.get(paste_url) as response:
            try:
                response.raise_for_status()
            except aiohttp.ClientResponseError as error:
                return error.status
            payload = {"content": await response.text()}
            post = await session.post(ENDPOINT, data=payload, timeout=TIMEOUT)
            return (await post.json())["result"]["key"]