| 15 | |
| 16 | |
| 17 | def make_snapshot_request( |
| 18 | func: Callable[[OpenAI], _T], |
| 19 | *, |
| 20 | content_snapshot: Any, |
| 21 | respx_mock: MockRouter, |
| 22 | mock_client: OpenAI, |
| 23 | path: str, |
| 24 | ) -> _T: |
| 25 | live = os.environ.get("OPENAI_LIVE") == "1" |
| 26 | if live: |
| 27 | |
| 28 | def _on_response(response: httpx.Response) -> None: |
| 29 | # update the content snapshot |
| 30 | assert json.dumps(json.loads(response.read())) == content_snapshot |
| 31 | |
| 32 | respx_mock.stop() |
| 33 | |
| 34 | client = OpenAI( |
| 35 | http_client=httpx.Client( |
| 36 | event_hooks={ |
| 37 | "response": [_on_response], |
| 38 | } |
| 39 | ) |
| 40 | ) |
| 41 | else: |
| 42 | respx_mock.post(path).mock( |
| 43 | return_value=httpx.Response( |
| 44 | 200, |
| 45 | content=get_snapshot_value(content_snapshot), |
| 46 | headers={"content-type": "application/json"}, |
| 47 | ) |
| 48 | ) |
| 49 | |
| 50 | client = mock_client |
| 51 | |
| 52 | result = func(client) |
| 53 | |
| 54 | if live: |
| 55 | client.close() |
| 56 | |
| 57 | return result |
| 58 | |
| 59 | |
| 60 | async def make_async_snapshot_request( |