Python aiohttp.ContentTypeError() Examples
The following are 21
code examples of aiohttp.ContentTypeError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
aiohttp
, or try the search function
.
Example #1
Source File: ddb.py From avrae with GNU General Public License v3.0 | 6 votes |
def _fetch_token(self, claim: str): """ Requests a short-term token from the DDB Auth Service given a Discord user claim in JWT form. :param str claim: The JWT representing the Discord user. :returns: A tuple representing the short-term token for the user and its TTL, or (None, None). :rtype: tuple[str or None,int or None] """ body = {"Token": claim} try: async with self.http.post(AUTH_DISCORD, json=body) as resp: if not 199 < resp.status < 300: raise AuthException(f"Auth Service returned {resp.status}: {await resp.text()}") try: data = await resp.json() except (aiohttp.ContentTypeError, ValueError, TypeError): raise AuthException(f"Could not deserialize Auth Service response: {await resp.text()}") except aiohttp.ServerTimeoutError: raise AuthException("Timed out connecting to Auth Service") return data['token'], data.get('ttl')
Example #2
Source File: network.py From pyrobud with MIT License | 6 votes |
def cmd_dog(self, ctx: command.Context) -> str: input_text = ctx.input status, text = await util.tg.get_text_input(ctx, input_text) if not status: if isinstance(text, str): return text return "__Unknown error.__" await ctx.respond("Uploading text to [Dogbin](https://del.dog/)...") async with self.bot.http.post("https://del.dog/documents", data=text) as resp: try: resp_data = await resp.json() except aiohttp.ContentTypeError: return "__Dogbin is currently experiencing issues. Try again later.__" return f'https://del.dog/{resp_data["key"]}'
Example #3
Source File: networking.py From pubgate with BSD 3-Clause "New" or "Revised" License | 6 votes |
def fetch(url, pass_through=False): async with aiohttp.ClientSession() as session: async with session.get(url, headers={"accept": 'application/activity+json', "user-agent": f"PubGate v:{__version__}"} ) as resp: status_code = resp.status logger.info(f"Fetch {url}, status: {resp.status}, {resp.reason}") try: result = await resp.json(encoding='utf-8') failed = False except aiohttp.ContentTypeError as e: result = {'fetch_error': await resp.text()} status_code = 500 failed = e if pass_through: return status_code, result elif failed: raise failed return result
Example #4
Source File: esi.py From Firetail with MIT License | 5 votes |
def get_data(self, url): """Base data retrieval method.""" async with self.session.get(url) as r: try: data = await r.json() except aiohttp.ContentTypeError: await asyncio.sleep(1) try: data = await r.json() except aiohttp.ContentTypeError: return None return data
Example #5
Source File: boorucore.py From Jintaku-Cogs-V3 with GNU Affero General Public License v3.0 | 5 votes |
def fetch_from_booru(self, urlstr, provider): # Handles provider data and fetcher responses content = "" async with self.session.get(urlstr, headers={'User-Agent': "Booru (https://github.com/Jintaku/Jintaku-Cogs-V3)"}) as resp: try: content = await resp.json(content_type=None) except (ValueError, aiohttp.ContentTypeError) as ex: log.debug("Pruned by exception, error below:") log.debug(ex) content = [] if not content or content == [] or content is None or (type(content) is dict and "success" in content.keys() and content["success"] == False): content = [] return content else: for item in content: if provider == "Konachan": item["post_link"] = "https://konachan.com/post/show/" + str(item["id"]) elif provider == "Gelbooru": item["post_link"] = "https://gelbooru.com/index.php?page=post&s=view&id=" + str(item["id"]) item["author"] = item["owner"] elif provider == "Rule34": item["post_link"] = "https://rule34.xxx/index.php?page=post&s=view&id=" + str(item["id"]) item["file_url"] = "https://us.rule34.xxx//images/" + item["directory"] + "/" + item["image"] item["author"] = item["owner"] elif provider == "Yandere": item["post_link"] = "https://yande.re/post/show/" + str(item["id"]) elif provider == "Danbooru": item["post_link"] = "https://danbooru.donmai.us/posts/" + str(item["id"]) item["tags"] = item["tag_string"] item["author"] = "Not Available" elif provider == "Safebooru": item["post_link"] = "https://safebooru.com/index.php?page=post&s=view&id=" + str(item["id"]) item["file_url"] = "https://safebooru.org//images/" + item["directory"] + "/" + item["image"] item["author"] = item["owner"] elif provider == "e621": item["post_link"] = "https://e621.net/post/show/" + str(item["id"]) item["provider"] = provider return content
Example #6
Source File: boorucore.py From Jintaku-Cogs-V3 with GNU Affero General Public License v3.0 | 5 votes |
def fetch_from_o(self, urlstr, rating, provider): # Handles provider data and fetcher responses content = "" async with self.session.get(urlstr, headers={'User-Agent': "Booru (https://github.com/Jintaku/Jintaku-Cogs-V3)"}) as resp: try: content = await resp.json(content_type=None) except (ValueError, aiohttp.ContentTypeError) as ex: log.debug("Pruned by exception, error below:") log.debug(ex) content = [] if not content or content == [] or content is None or (type(content) is dict and "success" in content.keys() and content["success"] == False): content = [] return content else: # Assign stuff to be used by booru_show for item in content: if provider == "Oboobs": item["post_link"] = "http://media.oboobs.ru/" + item["preview"] item["file_url"] = "http://media.oboobs.ru/" + item["preview"] elif provider == "Obutts": item["post_link"] = "http://media.obutts.ru/" + item["preview"] item["file_url"] = "http://media.obutts.ru/" + item["preview"] item["provider"] = provider item["author"] = "N/A" item["rating"] = rating item["score"] = "N/A" item["tags"] = "N/A" return content
Example #7
Source File: boorucore.py From Jintaku-Cogs-V3 with GNU Affero General Public License v3.0 | 5 votes |
def fetch_from_nekos(self, urlstr, rating, provider): # Handles provider data and fetcher responses content = "" async with self.session.get(urlstr, headers={'User-Agent': "Booru (https://github.com/Jintaku/Jintaku-Cogs-V3)"}) as resp: try: content = await resp.json(content_type=None) except (ValueError, aiohttp.ContentTypeError) as ex: log.debug("Pruned by exception, error below:") log.debug(ex) content = [] if not content or content == [] or content is None or (type(content) is dict and "success" in content.keys() and content["success"] == False): content = [] return content else: # Assign stuff to be used by booru_show nekos_content = [] item = {} for url in content["data"]["response"]["urls"]: item["post_link"] = url item["file_url"] = url item["provider"] = provider item["author"] = "N/A" item["rating"] = rating item["score"] = "N/A" item["tags"] = "N/A" nekos_content.append(item) item = {} return nekos_content
Example #8
Source File: roleplay.py From Jintaku-Cogs-V3 with GNU Affero General Public License v3.0 | 5 votes |
def fetch_nekos_life(self, ctx, rp_action): async with aiohttp.ClientSession() as session: async with session.get(f"https://api.nekos.dev/api/v3/images/sfw/gif/{rp_action}/?count=20") as resp: try: content = await resp.json(content_type=None) except (ValueError, aiohttp.ContentTypeError) as ex: log.debug("Pruned by exception, error below:") log.debug(ex) return [] if content["data"]["status"]["code"] == 200: return content["data"]["response"]["urls"]
Example #9
Source File: zero_ex_coordinator_v3.py From hummingbot with Apache License 2.0 | 5 votes |
def _post_request(self, url, data, timeout=10): async with aiohttp.ClientSession() as client: async with client.request('POST', url=url, timeout=timeout, json=data, headers={'Content-Type': 'application/json; charset=utf-8'}) as response: try: await response.json() return response except aiohttp.ContentTypeError as ex: error = await response.text() raise ValueError(error)
Example #10
Source File: http.py From galaxy-integrations-python-api with MIT License | 5 votes |
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 as error: raise UnknownBackendResponse(error.message) except aiohttp.ClientResponseError as error: if error.status == HTTPStatus.UNAUTHORIZED: raise AuthenticationRequired(error.message) if error.status == HTTPStatus.FORBIDDEN: raise AccessDenied(error.message) if error.status == HTTPStatus.SERVICE_UNAVAILABLE: raise BackendNotAvailable(error.message) if error.status == HTTPStatus.TOO_MANY_REQUESTS: raise TooManyRequests(error.message) if error.status >= 500: raise BackendError(error.message) if error.status >= 400: logger.warning( "Got status %d while performing %s request for %s", error.status, error.request_info.method, str(error.request_info.url) ) raise UnknownError(error.message) except aiohttp.ClientError as e: logger.exception("Caught exception while performing request") raise UnknownError(repr(e))
Example #11
Source File: esi.py From Firetail with MIT License | 5 votes |
def verify_token(self, access_token): header = {'Authorization': 'Bearer {}'.format(access_token)} async with self.session.get(OAUTH_URL, headers=header) as r: try: data = await r.json() except aiohttp.ContentTypeError: return None return data # Token Restricted
Example #12
Source File: esi.py From Firetail with MIT License | 5 votes |
def refresh_access_token(self, refresh_token, auth): header = {'Authorization': 'Basic {}'.format(auth)} params = {'grant_type': 'refresh_token', 'refresh_token': refresh_token} sess = self.session async with sess.get(OAUTH_URL, params=params, headers=header) as r: try: data = await r.json() except aiohttp.ContentTypeError: return None return data
Example #13
Source File: spacex.py From SpaceXLaunchBot with MIT License | 5 votes |
def get_launch_dict(launch_number: int = 0) -> Dict: """Get a launch information dictionary for the given launch. If launch_number <= 0 (the default), get the "next" launch. """ route = launch_number if launch_number > 0 else "next" spacex_api_url = f"https://api.spacexdata.com/v3/launches/{route}" try: async with aiohttp.ClientSession() as session: async with session.get(spacex_api_url) as response: if response.status != 200: logging.error(f"Response status: {response.status}") return {} return await response.json() except aiohttp.client_exceptions.ClientConnectorError: logging.error("Cannot connect to api.spacexdata.com") return {} except aiohttp.ContentTypeError: logging.error("JSON decode failed") return {}
Example #14
Source File: http.py From Galaxy_Plugin_Bethesda with MIT License | 5 votes |
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()
Example #15
Source File: base_client.py From python-slackclient with MIT License | 5 votes |
def _request(self, *, http_verb, api_url, req_args): """Submit the HTTP request with the running session or a new session. Returns: A dictionary of the response data. """ session = None use_running_session = self.session and not self.session.closed if use_running_session: session = self.session else: session = aiohttp.ClientSession( timeout=aiohttp.ClientTimeout(total=self.timeout), auth=req_args.pop("auth", None), ) response = None try: async with session.request(http_verb, api_url, **req_args) as res: data = {} try: data = await res.json() except aiohttp.ContentTypeError: self._logger.debug( f"No response data returned from the following API call: {api_url}." ) response = { "data": data, "headers": res.headers, "status_code": res.status, } finally: if not use_running_session: await session.close() return response # ================================================================= # urllib based WebClient # =================================================================
Example #16
Source File: bestiary.py From avrae with GNU General Public License v3.0 | 5 votes |
def parse_critterdb_response(resp, sha256_hash): try: raw_creatures = await resp.json() sha256_hash.update(await resp.read()) except (ValueError, aiohttp.ContentTypeError): raise ExternalImportError("Error importing bestiary: bad data. Are you sure the link is right?") return raw_creatures # critterdb -> bestiary helpers
Example #17
Source File: bestiary.py From avrae with GNU General Public License v3.0 | 5 votes |
def from_critterdb(cls, ctx, url, published=True): log.info(f"Getting bestiary ID {url}...") api_base = "https://critterdb.com:443/api/publishedbestiaries" if published \ else "https://critterdb.com:443/api/bestiaries" sha256_hash = hashlib.sha256() sha256_hash.update(BESTIARY_SCHEMA_VERSION) async with aiohttp.ClientSession() as session: if published: creatures = await get_published_bestiary_creatures(url, session, api_base, sha256_hash) else: creatures = await get_link_shared_bestiary_creatures(url, session, api_base, sha256_hash) async with session.get(f"{api_base}/{url}") as resp: try: raw = await resp.json() except (ValueError, aiohttp.ContentTypeError): raise ExternalImportError("Error importing bestiary metadata. Are you sure the link is right?") name = raw['name'] desc = raw['description'] sha256_hash.update(name.encode() + desc.encode()) # try and find a bestiary by looking up upstream|hash # if it exists, return it # otherwise commit a new one to the db and return that sha256 = sha256_hash.hexdigest() log.debug(f"Bestiary hash: {sha256}") existing_bestiary = await ctx.bot.mdb.bestiaries.find_one({"upstream": url, "sha256": sha256}) if existing_bestiary: log.info("This bestiary already exists, subscribing") existing_bestiary = Bestiary.from_dict(existing_bestiary) await existing_bestiary.subscribe(ctx) return existing_bestiary parsed_creatures = [_monster_factory(c, name) for c in creatures] b = cls(None, sha256, url, published, name, parsed_creatures, desc) await b.write_to_db(ctx) await b.subscribe(ctx) return b
Example #18
Source File: api.py From bot with MIT License | 5 votes |
def maybe_raise_for_status(self, response: aiohttp.ClientResponse, should_raise: bool) -> None: """Raise ResponseCodeError for non-OK response if an exception should be raised.""" if should_raise and response.status >= 400: try: response_json = await response.json() raise ResponseCodeError(response=response, response_json=response_json) except aiohttp.ContentTypeError: response_text = await response.text() raise ResponseCodeError(response=response, response_text=response_text)
Example #19
Source File: async_reader.py From rssant with BSD 3-Clause "New" or "Revised" License | 4 votes |
def read(self, url, *args, use_proxy=False, **kwargs) -> FeedResponse: headers = content = None try: if use_proxy: headers, content, url, status = await self._read_by_proxy(url, *args, **kwargs) else: headers, content, url, status = await self._read(url, *args, **kwargs) except (socket.gaierror, aiodns.error.DNSError): status = FeedResponseStatus.DNS_ERROR.value except (socket.timeout, TimeoutError, aiohttp.ServerTimeoutError, asyncio.TimeoutError, concurrent.futures.TimeoutError): status = FeedResponseStatus.CONNECTION_TIMEOUT.value except (ssl.SSLError, ssl.CertificateError, aiohttp.ServerFingerprintMismatch, aiohttp.ClientSSLError, aiohttp.ClientConnectorSSLError, aiohttp.ClientConnectorCertificateError): status = FeedResponseStatus.SSL_ERROR.value except (aiohttp.ClientProxyConnectionError, aiohttp.ClientHttpProxyError): status = FeedResponseStatus.PROXY_ERROR.value except (ConnectionError, aiohttp.ServerDisconnectedError, aiohttp.ServerConnectionError, aiohttp.ClientConnectionError, aiohttp.ClientConnectorError): status = FeedResponseStatus.CONNECTION_RESET.value except (aiohttp.WSServerHandshakeError, aiohttp.ClientOSError): status = FeedResponseStatus.CONNECTION_ERROR.value except aiohttp.ClientPayloadError: status = FeedResponseStatus.CHUNKED_ENCODING_ERROR.value except UnicodeDecodeError: status = FeedResponseStatus.CONTENT_DECODING_ERROR.value except FeedReaderError as ex: status = ex.status LOG.warning(type(ex).__name__ + " url=%s %s", url, ex) except (aiohttp.ClientResponseError, aiohttp.ContentTypeError) as ex: status = ex.status except (aiohttp.ClientError, aiohttp.InvalidURL): status = FeedResponseStatus.UNKNOWN_ERROR.value builder = FeedResponseBuilder(use_proxy=use_proxy) builder.url(url) builder.status(status) builder.content(content) builder.headers(headers) return builder.build()
Example #20
Source File: boorucore.py From Jintaku-Cogs-V3 with GNU Affero General Public License v3.0 | 4 votes |
def fetch_from_reddit(self, urlstr, rating, provider): # Handles provider data and fetcher responses content = "" async with self.session.get(urlstr, headers={'User-Agent': "Booru (https://github.com/Jintaku/Jintaku-Cogs-V3)"}) as resp: try: content = await resp.json(content_type=None) except (ValueError, aiohttp.ContentTypeError) as ex: log.debug("Pruned by exception, error below:") log.debug(ex) content = [] if not content or content == [] or content is None or (type(content) is dict and "success" in content.keys() and content["success"] == False): content = [] return content else: # Clean up to kill bad pictures and crap good_content = [] for item in content["data"]["children"]: IMGUR_LINKS = "https://imgur.com/", "https://i.imgur.com/", "http://i.imgur.com/", "http://imgur.com", "https://m.imgur.com" GOOD_EXTENSIONS = ".png", ".jpg", ".jpeg", ".gif" url = item["data"]["url"] if url.startswith(IMGUR_LINKS): if url.endswith(".mp4"): item["file_url"] = url[:-3] + "gif" elif url.endswith(".gifv"): item["file_url"] = url[:-1] elif url.endswith(GOOD_EXTENSIONS): item["file_url"] = url else: item["file_url"] = url + ".png" elif url.startswith("https://gfycat.com/"): url_cut = url[19:] if url_cut.islower(): continue item["file_url"] = "https://thumbs.gfycat.com/" + url_cut + "-size_restricted.gif" elif url.endswith(GOOD_EXTENSIONS): item["file_url"] = url else: continue good_content.append(item) content = good_content # Assign stuff to be used by booru_show for item in content: item["provider"] = provider item["rating"] = rating item["post_link"] = "https://reddit.com" + item["data"]["permalink"] item["score"] = item["data"]["score"] item["tags"] = item["data"]["title"] item["author"] = item["data"]["author"] return content
Example #21
Source File: slack_client.py From botbuilder-python with MIT License | 4 votes |
def post_message_to_slack(self, message: SlackMessage) -> SlackResponse: if not message: return None request_content = { "token": self.options.slack_bot_token, "channel": message.channel, "text": message.text, } if message.thread_ts: request_content["thread_ts"] = message.thread_ts if message.blocks: request_content["blocks"] = json.dumps(message.blocks) session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30),) http_verb = "POST" api_url = POST_EPHEMERAL_MESSAGE_URL if message.ephemeral else POST_MESSAGE_URL req_args = {"data": request_content} async with session.request(http_verb, api_url, **req_args) as res: response_content = {} try: response_content = await res.json() except aiohttp.ContentTypeError: pass response_data = { "data": response_content, "headers": res.headers, "status_code": res.status, } data = { "client": self, "http_verb": http_verb, "api_url": api_url, "req_args": req_args, } response = SlackResponse(**{**data, **response_data}).validate() await session.close() return response