File: util.py

package info (click to toggle)
python-asyncprawcore 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,328 kB
  • sloc: python: 2,224; makefile: 4
file content (30 lines) | stat: -rw-r--r-- 855 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Provide utility for the asyncprawcore package."""

from __future__ import annotations

from typing import TYPE_CHECKING

from .exceptions import Forbidden, InsufficientScope, InvalidToken

if TYPE_CHECKING:
    from aiohttp import ClientResponse

_auth_error_mapping = {
    403: Forbidden,
    "insufficient_scope": InsufficientScope,
    "invalid_token": InvalidToken,
}


def authorization_error_class(
    response: ClientResponse,
) -> InvalidToken | (Forbidden | InsufficientScope):
    """Return an exception instance that maps to the OAuth Error.

    :param response: The HTTP response containing a www-authenticate error.

    """
    message = response.headers.get("www-authenticate")
    error: int | str
    error = message.replace('"', "").rsplit("=", 1)[1] if message else response.status
    return _auth_error_mapping[error](response)