def make_request(method_name: str, data: dict) -> dict:
    """
    Makes HTTP request to Telegram Bot API.

    - see `api/request.py` documentation for more.

    :param method_name: Name of API method in URL.
    :param data: JSON data to send.

    :raises TelegramBotApiException:
    See `telegram/exceptions.py` documentation for more.
    """
    url = create_bot_url(method_name)
    timeout = current_app.config["TELEGRAM_API_TIMEOUT"]
    result = request(content_type="json",
                     method="POST",
                     url=url,
                     json=data,
                     timeout=timeout,
                     allow_redirects=False,
                     verify=True)

    ok = result["content"]["ok"]

    if not ok:
        raise RequestFailed(create_error_text(result.content))

    # https://core.telegram.org/bots/api/#making-requests
    result["content"] = result["content"]["result"]

    return result
Example #2
0
def make_link_request(data: dict, user_token: str):
    """
    https://yandex.ru/dev/disk/api/reference/response-objects-docpage/#link

    - it will not raise in case of error HTTP code.
    - see `api/request.py` documentation for more.

    :param data: Data of link to handle.
    :param user_token: User OAuth token to access the API.

    :raises NotImplementedError: If link requires templating.
    """
    if (data["templated"]):
        raise NotImplementedError("Templating not implemented")

    url = data["href"]
    method = data["method"].upper()
    timeout = current_app.config["YANDEX_DISK_API_TIMEOUT"]

    return request(raise_for_status=False,
                   content_type="json",
                   method=method,
                   url=url,
                   timeout=timeout,
                   auth=HTTPOAuthAuth(user_token),
                   allow_redirects=False,
                   verify=True)
Example #3
0
def make_disk_request(http_method: str, api_method: str, data: dict,
                      user_token: str):
    """
    Makes HTTP request to Yandex.Disk.

    - it will not raise in case of error HTTP code.
    - see `api/request.py` documentation for more.

    :param http_method: Name of HTTP method for request.
    :param api_method: Name of API method in URL.
    :param data: JSON data to send.
    :param user_token: User OAuth token to access the API.
    """
    url = create_disk_url(api_method)
    timeout = current_app.config["YANDEX_DISK_API_TIMEOUT"]

    return request(raise_for_status=False,
                   content_type="json",
                   method=http_method.upper(),
                   url=url,
                   params=data,
                   timeout=timeout,
                   auth=HTTPOAuthAuth(user_token),
                   allow_redirects=False,
                   verify=True)
def make_photo_preview_request(photo_url: str, user_token: str):
    """
    Makes request to URL in order to get bytes content of photo.

    Yandex requires user OAuth token in order to get
    access to photo previews, so, it is why you should
    use this method.

    - it will not raise in case of error HTTP code.
    - see `api/request.py` documentation for more.

    :param photo_url:
    URL of photo.
    :param user_token:
    User OAuth token to access this URL.

    :returns:
    See `api/request.py`.
    In case of `ok = True` under `content` will be bytes content
    of requested photo.
    """
    timeout = current_app.config["YANDEX_DISK_API_TIMEOUT"]

    return request(raise_for_status=False,
                   content_type="bytes",
                   method="GET",
                   url=photo_url,
                   timeout=timeout,
                   auth=HTTPOAuthAuth(user_token),
                   allow_redirects=False,
                   verify=True)
Example #5
0
def make_oauth_request(method_name: str, data: dict):
    """
    Makes HTTP request to Yandex OAuth.

    - see `api/request.py` documentation for more.

    :param method_name: Name of API method in URL.
    :param data: Data to send.
    """
    url = create_bot_oauth_url(method_name)
    timeout = current_app.config["YANDEX_OAUTH_API_TIMEOUT"]
    id = environ["YANDEX_OAUTH_API_APP_ID"]
    password = environ["YANDEX_OAUTH_API_APP_PASSWORD"]

    return request(content_type="json",
                   method="POST",
                   url=url,
                   data=data,
                   timeout=timeout,
                   auth=HTTPBasicAuth(id, password),
                   allow_redirects=False,
                   verify=True)