1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
from __future__ import annotations
import os
import json
from typing import Any, Callable, Awaitable
from typing_extensions import TypeVar
import httpx
from respx import MockRouter
from openai import OpenAI, AsyncOpenAI
from .utils import get_snapshot_value
_T = TypeVar("_T")
def make_snapshot_request(
func: Callable[[OpenAI], _T],
*,
content_snapshot: Any,
respx_mock: MockRouter,
mock_client: OpenAI,
path: str,
) -> _T:
live = os.environ.get("OPENAI_LIVE") == "1"
if live:
def _on_response(response: httpx.Response) -> None:
# update the content snapshot
assert json.dumps(json.loads(response.read())) == content_snapshot
respx_mock.stop()
client = OpenAI(
http_client=httpx.Client(
event_hooks={
"response": [_on_response],
}
)
)
else:
respx_mock.post(path).mock(
return_value=httpx.Response(
200,
content=get_snapshot_value(content_snapshot),
headers={"content-type": "application/json"},
)
)
client = mock_client
result = func(client)
if live:
client.close()
return result
async def make_async_snapshot_request(
func: Callable[[AsyncOpenAI], Awaitable[_T]],
*,
content_snapshot: Any,
respx_mock: MockRouter,
mock_client: AsyncOpenAI,
path: str,
) -> _T:
live = os.environ.get("OPENAI_LIVE") == "1"
if live:
async def _on_response(response: httpx.Response) -> None:
# update the content snapshot
assert json.dumps(json.loads(await response.aread())) == content_snapshot
respx_mock.stop()
client = AsyncOpenAI(
http_client=httpx.AsyncClient(
event_hooks={
"response": [_on_response],
}
)
)
else:
respx_mock.post(path).mock(
return_value=httpx.Response(
200,
content=get_snapshot_value(content_snapshot),
headers={"content-type": "application/json"},
)
)
client = mock_client
result = await func(client)
if live:
await client.close()
return result
|