|
1 | 1 | import base64 |
2 | 2 | from abc import ABC, abstractmethod |
3 | | -from typing import Any, Dict |
4 | | - |
| 3 | +from typing import Any |
5 | 4 |
|
6 | 5 | class AuthProvider(ABC): |
7 | 6 | @abstractmethod |
8 | | - def get_auth_headers(self) -> Dict[str, str]: |
| 7 | + def get_auth_headers(self) -> dict[str, str]: |
9 | 8 | pass |
10 | 9 |
|
11 | 10 | @abstractmethod |
12 | | - def get_auth_params(self) -> Dict[str, Any]: |
| 11 | + def get_auth_params(self) -> dict[str, Any]: |
13 | 12 | pass |
14 | 13 |
|
15 | | - |
16 | 14 | class APIKeyAuth(AuthProvider): |
17 | 15 | def __init__(self, api_key: str, header_name: str = "Authorization"): |
18 | 16 | self.api_key = api_key |
19 | 17 | self.header_name = header_name |
20 | 18 |
|
21 | | - def get_auth_headers(self) -> Dict[str, str]: |
| 19 | + def get_auth_headers(self) -> dict[str, str]: |
22 | 20 | return {self.header_name: f"Bearer {self.api_key}"} |
23 | 21 |
|
24 | | - def get_auth_params(self) -> Dict[str, Any]: |
| 22 | + def get_auth_params(self) -> dict[str, Any]: |
25 | 23 | return {} |
26 | 24 |
|
27 | | - |
28 | 25 | class BasicAuth(AuthProvider): |
29 | 26 | def __init__(self, username: str, password: str): |
30 | 27 | self.username = username |
31 | 28 | self.password = password |
32 | 29 |
|
33 | | - def get_auth_headers(self) -> Dict[str, str]: |
| 30 | + def get_auth_headers(self) -> dict[str, str]: |
34 | 31 | credentials = f"{self.username}:{self.password}" |
35 | 32 | encoded = base64.b64encode(credentials.encode()).decode() |
36 | 33 | return {"Authorization": f"Basic {encoded}"} |
37 | 34 |
|
38 | | - def get_auth_params(self) -> Dict[str, Any]: |
| 35 | + def get_auth_params(self) -> dict[str, Any]: |
39 | 36 | return {} |
40 | 37 |
|
41 | | - |
42 | 38 | class OAuthTokenAuth(AuthProvider): |
43 | 39 | def __init__(self, token: str, token_type: str = "Bearer"): |
44 | 40 | self.token = token |
45 | 41 | self.token_type = token_type |
46 | 42 |
|
47 | | - def get_auth_headers(self) -> Dict[str, str]: |
| 43 | + def get_auth_headers(self) -> dict[str, str]: |
48 | 44 | return {"Authorization": f"{self.token_type} {self.token}"} |
49 | 45 |
|
50 | | - def get_auth_params(self) -> Dict[str, Any]: |
| 46 | + def get_auth_params(self) -> dict[str, Any]: |
51 | 47 | return {} |
52 | 48 |
|
53 | | - |
54 | 49 | class CustomHeaderAuth(AuthProvider): |
55 | | - def __init__(self, headers: Dict[str, str]): |
| 50 | + def __init__(self, headers: dict[str, str]): |
56 | 51 | self.headers = dict(headers) |
57 | 52 |
|
58 | | - def get_auth_headers(self) -> Dict[str, str]: |
| 53 | + def get_auth_headers(self) -> dict[str, str]: |
59 | 54 | return dict(self.headers) |
60 | 55 |
|
61 | | - def get_auth_params(self) -> Dict[str, Any]: |
| 56 | + def get_auth_params(self) -> dict[str, Any]: |
62 | 57 | return {} |
63 | 58 |
|
64 | | - |
65 | 59 | def create_api_key_auth(api_key: str, header_name: str = "Authorization") -> APIKeyAuth: |
66 | 60 | return APIKeyAuth(api_key, header_name) |
67 | 61 |
|
68 | | - |
69 | 62 | def create_basic_auth(username: str, password: str) -> BasicAuth: |
70 | 63 | return BasicAuth(username, password) |
71 | 64 |
|
72 | | - |
73 | 65 | def create_oauth_auth(token: str, token_type: str = "Bearer") -> OAuthTokenAuth: |
74 | 66 | return OAuthTokenAuth(token, token_type) |
75 | 67 |
|
76 | | - |
77 | | -def create_custom_header_auth(headers: Dict[str, str]) -> CustomHeaderAuth: |
| 68 | +def create_custom_header_auth(headers: dict[str, str]) -> CustomHeaderAuth: |
78 | 69 | return CustomHeaderAuth(headers) |
0 commit comments