mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-16 12:07:13 +00:00
Reformat API module.
This commit is contained in:
parent
88c5b3a606
commit
01dc74a118
2 changed files with 46 additions and 52 deletions
|
|
@ -39,4 +39,4 @@ __all__ = [
|
||||||
]
|
]
|
||||||
|
|
||||||
__version__ = '2.0.dev1'
|
__version__ = '2.0.dev1'
|
||||||
__api_version__ = '3.6'
|
__api_version__ = '4.1'
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,8 @@
|
||||||
import abc
|
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import ssl
|
|
||||||
from asyncio import AbstractEventLoop
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import Optional, Tuple
|
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import certifi
|
|
||||||
|
|
||||||
from .. import types
|
from .. import types
|
||||||
from ..utils import exceptions
|
from ..utils import exceptions
|
||||||
|
|
@ -41,55 +35,55 @@ def check_token(token: str) -> bool:
|
||||||
|
|
||||||
|
|
||||||
async def check_result(method_name: str, content_type: str, status_code: int, body: str):
|
async def check_result(method_name: str, content_type: str, status_code: int, body: str):
|
||||||
"""
|
"""
|
||||||
Checks whether `result` is a valid API response.
|
Checks whether `result` is a valid API response.
|
||||||
A result is considered invalid if:
|
A result is considered invalid if:
|
||||||
- The server returned an HTTP response code other than 200
|
- The server returned an HTTP response code other than 200
|
||||||
- The content of the result is invalid JSON.
|
- The content of the result is invalid JSON.
|
||||||
- The method call was unsuccessful (The JSON 'ok' field equals False)
|
- The method call was unsuccessful (The JSON 'ok' field equals False)
|
||||||
|
|
||||||
:param method_name: The name of the method called
|
:param method_name: The name of the method called
|
||||||
:param status_code: status code
|
:param status_code: status code
|
||||||
:param content_type: content type of result
|
:param content_type: content type of result
|
||||||
:param body: result body
|
:param body: result body
|
||||||
:return: The result parsed to a JSON dictionary
|
:return: The result parsed to a JSON dictionary
|
||||||
:raises ApiException: if one of the above listed cases is applicable
|
:raises ApiException: if one of the above listed cases is applicable
|
||||||
"""
|
"""
|
||||||
log.debug('Response for %s: [%d] "%r"', method_name, status_code, body)
|
log.debug('Response for %s: [%d] "%r"', method_name, status_code, body)
|
||||||
|
|
||||||
if content_type != 'application/json':
|
if content_type != 'application/json':
|
||||||
raise exceptions.NetworkError(f"Invalid response with content type {content_type}: \"{body}\"")
|
raise exceptions.NetworkError(f"Invalid response with content type {content_type}: \"{body}\"")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result_json = json.loads(body)
|
result_json = json.loads(body)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
result_json = {}
|
result_json = {}
|
||||||
|
|
||||||
description = result_json.get('description') or body
|
description = result_json.get('description') or body
|
||||||
parameters = types.ResponseParameters(**result_json.get('parameters', {}) or {})
|
parameters = types.ResponseParameters(**result_json.get('parameters', {}) or {})
|
||||||
|
|
||||||
if HTTPStatus.OK <= status_code <= HTTPStatus.IM_USED:
|
if HTTPStatus.OK <= status_code <= HTTPStatus.IM_USED:
|
||||||
return result_json.get('result')
|
return result_json.get('result')
|
||||||
elif parameters.retry_after:
|
elif parameters.retry_after:
|
||||||
raise exceptions.RetryAfter(parameters.retry_after)
|
raise exceptions.RetryAfter(parameters.retry_after)
|
||||||
elif parameters.migrate_to_chat_id:
|
elif parameters.migrate_to_chat_id:
|
||||||
raise exceptions.MigrateToChat(parameters.migrate_to_chat_id)
|
raise exceptions.MigrateToChat(parameters.migrate_to_chat_id)
|
||||||
elif status_code == HTTPStatus.BAD_REQUEST:
|
elif status_code == HTTPStatus.BAD_REQUEST:
|
||||||
exceptions.BadRequest.detect(description)
|
exceptions.BadRequest.detect(description)
|
||||||
elif status_code == HTTPStatus.NOT_FOUND:
|
elif status_code == HTTPStatus.NOT_FOUND:
|
||||||
exceptions.NotFound.detect(description)
|
exceptions.NotFound.detect(description)
|
||||||
elif status_code == HTTPStatus.CONFLICT:
|
elif status_code == HTTPStatus.CONFLICT:
|
||||||
exceptions.ConflictError.detect(description)
|
exceptions.ConflictError.detect(description)
|
||||||
elif status_code in [HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN]:
|
elif status_code in [HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN]:
|
||||||
exceptions.Unauthorized.detect(description)
|
exceptions.Unauthorized.detect(description)
|
||||||
elif status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE:
|
elif status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE:
|
||||||
raise exceptions.NetworkError('File too large for uploading. '
|
raise exceptions.NetworkError('File too large for uploading. '
|
||||||
'Check telegram api limits https://core.telegram.org/bots/api#senddocument')
|
'Check telegram api limits https://core.telegram.org/bots/api#senddocument')
|
||||||
elif status_code >= HTTPStatus.INTERNAL_SERVER_ERROR:
|
elif status_code >= HTTPStatus.INTERNAL_SERVER_ERROR:
|
||||||
if 'restart' in description:
|
if 'restart' in description:
|
||||||
raise exceptions.RestartingTelegram()
|
raise exceptions.RestartingTelegram()
|
||||||
raise exceptions.TelegramAPIError(description)
|
raise exceptions.TelegramAPIError(description)
|
||||||
raise exceptions.TelegramAPIError(f"{description} [{status_code}]")
|
raise exceptions.TelegramAPIError(f"{description} [{status_code}]")
|
||||||
|
|
||||||
|
|
||||||
async def make_request(session, token, method, data=None, files=None, **kwargs):
|
async def make_request(session, token, method, data=None, files=None, **kwargs):
|
||||||
|
|
@ -153,7 +147,7 @@ class Methods(Helper):
|
||||||
"""
|
"""
|
||||||
Helper for Telegram API Methods listed on https://core.telegram.org/bots/api
|
Helper for Telegram API Methods listed on https://core.telegram.org/bots/api
|
||||||
|
|
||||||
List is updated to Bot API 3.6
|
List is updated to Bot API 4.1
|
||||||
"""
|
"""
|
||||||
mode = HelperMode.lowerCamelCase
|
mode = HelperMode.lowerCamelCase
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue