|
| 1 | +"""Implementation of an httpx.Client that forwards traffic to the Azure SDK test-proxy. |
| 2 | +
|
| 3 | +.. note:: |
| 4 | +
|
| 5 | + This module has side-effects! |
| 6 | +
|
| 7 | + Importing this module will replace the default httpx.Client used |
| 8 | + by the openai package with one that can redirect it's traffic |
| 9 | + to the Azure SDK test-proxy on demand. |
| 10 | +
|
| 11 | +""" |
| 12 | +from contextlib import contextmanager |
| 13 | +from typing import Iterable, Literal, Optional |
| 14 | + |
| 15 | +import httpx |
| 16 | +import openai._base_client |
| 17 | +from typing_extensions import override |
| 18 | +from dataclasses import dataclass |
| 19 | + |
| 20 | + |
| 21 | +@dataclass |
| 22 | +class TestProxyConfig: |
| 23 | + recording_id: str |
| 24 | + """The ID for the ongoing test recording.""" |
| 25 | + |
| 26 | + recording_mode: Literal["playback", "record"] |
| 27 | + """The current recording mode.""" |
| 28 | + |
| 29 | + proxy_url: str |
| 30 | + """The url for the Azure SDK test proxy.""" |
| 31 | + |
| 32 | + |
| 33 | +class TestProxyHttpxClient(openai._base_client.SyncHttpxClientWrapper): |
| 34 | + recording_config: Optional[TestProxyConfig] = None |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def is_recording(cls) -> bool: |
| 38 | + """Whether we are forwarding requests to the test proxy |
| 39 | +
|
| 40 | + :return: True if forwarding, False otherwise |
| 41 | + :rtype: bool |
| 42 | + """ |
| 43 | + return cls.recording_config is not None |
| 44 | + |
| 45 | + @classmethod |
| 46 | + @contextmanager |
| 47 | + def record_with_proxy(cls, config: TestProxyConfig) -> Iterable[None]: |
| 48 | + """Forward all requests made within the scope of context manager to test-proxy. |
| 49 | +
|
| 50 | + :param TestProxyConfig config: The test proxy configuration |
| 51 | + """ |
| 52 | + cls.recording_config = config |
| 53 | + |
| 54 | + yield |
| 55 | + |
| 56 | + cls.recording_config = None |
| 57 | + |
| 58 | + @override |
| 59 | + def send(self, request: httpx.Request, **kwargs) -> httpx.Response: |
| 60 | + if self.is_recording(): |
| 61 | + return self._send_to_proxy(request, **kwargs) |
| 62 | + else: |
| 63 | + return super().send(request, **kwargs) |
| 64 | + |
| 65 | + def _send_to_proxy(self, request: httpx.Request, **kwargs) -> httpx.Response: |
| 66 | + """Forwards a network request to the test proxy |
| 67 | +
|
| 68 | + :param httpx.Request request: The request to send |
| 69 | + :keyword **kwargs: The kwargs accepted by httpx.Client.send |
| 70 | + :return: The request's response |
| 71 | + :rtype: httpx.Response |
| 72 | + """ |
| 73 | + assert self.is_recording(), f"{self._send_to_proxy.__qualname__} should only be called while recording" |
| 74 | + config = self.recording_config |
| 75 | + original_url = request.url |
| 76 | + |
| 77 | + request_path = original_url.copy_with(scheme="", netloc=b"") |
| 78 | + request.url = httpx.URL(config.proxy_url).join(request_path) |
| 79 | + |
| 80 | + headers = request.headers |
| 81 | + if headers.get("x-recording-upstream-base-uri", None) is None: |
| 82 | + headers["x-recording-upstream-base-uri"] = str( |
| 83 | + httpx.URL(scheme=original_url.scheme, netloc=original_url.netloc) |
| 84 | + ) |
| 85 | + headers["x-recording-id"] = config.recording_id |
| 86 | + headers["x-recording-mode"] = config.recording_mode |
| 87 | + |
| 88 | + response = super().send(request, **kwargs) |
| 89 | + |
| 90 | + response.request.url = original_url |
| 91 | + return response |
| 92 | + |
| 93 | + |
| 94 | +# openai._base_client.SyncHttpxClientWrapper is default httpx.Client instantiated by openai |
| 95 | +openai._base_client.SyncHttpxClientWrapper = TestProxyHttpxClient |
0 commit comments