|
| 1 | +from urllib.parse import quote, urlparse |
| 2 | + |
| 3 | +from botocore.auth import SigV4Auth |
| 4 | +from botocore.awsrequest import AWSRequest |
| 5 | +import botocore.session |
| 6 | + |
| 7 | +import httpx |
| 8 | + |
| 9 | + |
| 10 | +class AWSSignV4Auth(httpx.Auth): |
| 11 | + def __init__(self, aws_region="eu-west-1") -> None: |
| 12 | + self.aws_region = aws_region |
| 13 | + |
| 14 | + def auth_flow(self, request): |
| 15 | + request = self.sign_request(request) |
| 16 | + yield request |
| 17 | + |
| 18 | + def sign_request(self, request: httpx.Request) -> httpx.Request: |
| 19 | + """Signs an httpx request with AWS Signature Version 4.""" |
| 20 | + |
| 21 | + session = botocore.session.get_session() |
| 22 | + credentials = session.get_credentials() |
| 23 | + aws_request = AWSRequest( |
| 24 | + method=request.method.upper(), url=str(request.url), data=request.content |
| 25 | + ) |
| 26 | + region = self.aws_region |
| 27 | + service = "execute-api" |
| 28 | + |
| 29 | + # Sign the request |
| 30 | + SigV4Auth(credentials, service, region).add_auth(aws_request) |
| 31 | + |
| 32 | + # Update the httpx request headers with the signed headers |
| 33 | + request.headers.update(dict(aws_request.headers)) |
| 34 | + |
| 35 | + return request |
| 36 | + |
| 37 | + |
| 38 | +class ApiKeyAuth(httpx.Auth): |
| 39 | + def __init__(self, api_key: str) -> None: |
| 40 | + self.api_key = api_key |
| 41 | + |
| 42 | + def auth_flow(self, request): |
| 43 | + request.headers["x-api-key"] = self.api_key |
| 44 | + yield request |
| 45 | + |
| 46 | + |
| 47 | +class HttpClient: |
| 48 | + def __init__(self, base_url: str, auth: httpx.Auth, aws_region="eu-west-1") -> None: |
| 49 | + self.aws_region = aws_region |
| 50 | + self.base_url = base_url |
| 51 | + self.auth = auth |
| 52 | + self.client = httpx.Client() |
| 53 | + self.timeout = 30 |
| 54 | + |
| 55 | + def _replace_path_params(self, url: str, path_params: dict): |
| 56 | + for param, value in path_params.items(): |
| 57 | + if not value: |
| 58 | + raise ValueError( |
| 59 | + f"Parameter {param} is required, cannot be empty or blank." |
| 60 | + ) |
| 61 | + url = url.replace(f"{{{param}}}", quote(str(value))) |
| 62 | + return url |
| 63 | + |
| 64 | + def _normalize_url(self, url: str): |
| 65 | + parsed_url = urlparse(url) |
| 66 | + if not parsed_url.netloc: |
| 67 | + return f"{self.base_url}{url}" |
| 68 | + return url |
| 69 | + |
| 70 | + def get( |
| 71 | + self, |
| 72 | + url: str, |
| 73 | + path_params: dict = {}, |
| 74 | + params: dict = {}, |
| 75 | + headers: dict = {}, |
| 76 | + ): |
| 77 | + url = self._replace_path_params( |
| 78 | + url=self._normalize_url(url), path_params=path_params |
| 79 | + ) |
| 80 | + response = self.client.get( |
| 81 | + url=url, |
| 82 | + params=params, |
| 83 | + headers=headers, |
| 84 | + auth=self.auth, |
| 85 | + timeout=self.timeout, |
| 86 | + ) |
| 87 | + response.raise_for_status() |
| 88 | + return response |
| 89 | + |
| 90 | + def post( |
| 91 | + self, url: str, path_params: dict, params: dict, headers: dict, json: dict |
| 92 | + ): |
| 93 | + url = self._replace_path_params( |
| 94 | + url=self._normalize_url(url), path_params=path_params |
| 95 | + ) |
| 96 | + response = self.client.post( |
| 97 | + url=url, |
| 98 | + params=params, |
| 99 | + headers=headers, |
| 100 | + json=json, |
| 101 | + auth=self.auth, |
| 102 | + timeout=self.timeout, |
| 103 | + ) |
| 104 | + response.raise_for_status() |
| 105 | + return response |
| 106 | + |
| 107 | + def put(self, url: str, path_params: dict, params: dict, headers: dict, json: dict): |
| 108 | + url = self._replace_path_params( |
| 109 | + url=self._normalize_url(url), path_params=path_params |
| 110 | + ) |
| 111 | + response = self.client.put( |
| 112 | + url=url, |
| 113 | + params=params, |
| 114 | + headers=headers, |
| 115 | + json=json, |
| 116 | + auth=self.auth, |
| 117 | + timeout=self.timeout, |
| 118 | + ) |
| 119 | + response.raise_for_status() |
| 120 | + return response |
| 121 | + |
| 122 | + def patch( |
| 123 | + self, url: str, path_params: dict, params: dict, headers: dict, json: dict |
| 124 | + ): |
| 125 | + url = self._replace_path_params( |
| 126 | + url=self._normalize_url(url), path_params=path_params |
| 127 | + ) |
| 128 | + response = self.client.patch( |
| 129 | + url=url, |
| 130 | + params=params, |
| 131 | + headers=headers, |
| 132 | + json=json, |
| 133 | + auth=self.auth, |
| 134 | + timeout=self.timeout, |
| 135 | + ) |
| 136 | + response.raise_for_status() |
| 137 | + return response |
| 138 | + |
| 139 | + def delete(self, url: str, path_params: dict, params: dict, headers: dict): |
| 140 | + url = self._replace_path_params( |
| 141 | + url=self._normalize_url(url), path_params=path_params |
| 142 | + ) |
| 143 | + response = self.client.delete( |
| 144 | + url=url, |
| 145 | + params=params, |
| 146 | + headers=headers, |
| 147 | + auth=self.auth, |
| 148 | + timeout=self.timeout, |
| 149 | + ) |
| 150 | + response.raise_for_status() |
| 151 | + return response |
0 commit comments