|
26 | 26 |
|
27 | 27 | from http import HTTPStatus # Provides constants for HTTP status codes |
28 | 28 |
|
| 29 | +import ssl |
29 | 30 | import httpx |
30 | 31 |
|
31 | 32 | from . import proxy as proxy_address |
@@ -232,6 +233,23 @@ async def get_config(cls) -> EndpointConfig: |
232 | 233 | provide additional configuration options. |
233 | 234 | """ |
234 | 235 | raise NotImplementedError("get_config method not implemented") |
| 236 | + |
| 237 | + @classmethod |
| 238 | + def _ssl_context(cls): |
| 239 | + """Returns the SSL context for the endpoint |
| 240 | +
|
| 241 | + This method can be overridden by the child class to |
| 242 | + provide additional SSL context options. |
| 243 | + """ |
| 244 | + from . import file_tls_certificate, file_private_key |
| 245 | + |
| 246 | + if not file_tls_certificate: |
| 247 | + """TLS certificate is required for SSL context""" |
| 248 | + return None |
| 249 | + |
| 250 | + context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH) |
| 251 | + context.load_cert_chain(file_tls_certificate, file_private_key) |
| 252 | + return context |
235 | 253 |
|
236 | 254 | @classmethod |
237 | 255 | async def _discover(cls): |
@@ -269,7 +287,10 @@ async def _discover(cls): |
269 | 287 | # be called as part of the bootstrapping process |
270 | 288 | from . import api_base |
271 | 289 |
|
272 | | - async with httpx.AsyncClient(proxy=proxy_address) as _httpx_async: |
| 290 | + async with httpx.AsyncClient( |
| 291 | + proxy=proxy_address, |
| 292 | + verify=cls._ssl_context(), |
| 293 | + ) as _httpx_async: |
273 | 294 | # Don't use the _get wrapper here, we need to get the raw response |
274 | 295 | response = await _httpx_async.get( |
275 | 296 | api_base, |
@@ -490,7 +511,11 @@ async def follow( |
490 | 511 | # Initial url is set to endpoint_follow |
491 | 512 | url = f"{cls.__config__.endpoint_follow.href}" |
492 | 513 |
|
493 | | - async with httpx.AsyncClient(proxy=proxy_address) as _httpx_async: |
| 514 | + async with httpx.AsyncClient( |
| 515 | + proxy=proxy_address, |
| 516 | + verify=cls._ssl_context(), |
| 517 | + ) as _httpx_async: |
| 518 | + |
494 | 519 | while event.is_set(): |
495 | 520 | try: |
496 | 521 | response = await _httpx_async.get( |
@@ -546,7 +571,10 @@ async def _get( |
546 | 571 | :param str url: URL to fetch the data from |
547 | 572 | :param AppBaseModel response_class: DTO to be used for list requests |
548 | 573 | """ |
549 | | - async with httpx.AsyncClient(proxy=proxy_address) as _httpx_async: |
| 574 | + async with httpx.AsyncClient( |
| 575 | + proxy=proxy_address, |
| 576 | + verify=cls._ssl_context(), |
| 577 | + ) as _httpx_async: |
550 | 578 |
|
551 | 579 | try: |
552 | 580 |
|
@@ -594,7 +622,10 @@ async def _post( |
594 | 622 | The behaviour is very similar to the _get method, except |
595 | 623 | parsing and sending out a body as part of the request. |
596 | 624 | """ |
597 | | - async with httpx.AsyncClient(proxy=proxy_address) as _httpx_async: |
| 625 | + async with httpx.AsyncClient( |
| 626 | + proxy=proxy_address, |
| 627 | + verify=cls._ssl_context(), |
| 628 | + ) as _httpx_async: |
598 | 629 |
|
599 | 630 | try: |
600 | 631 |
|
|
0 commit comments