|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from typing_extensions import override |
| 5 | + |
| 6 | +import httpx |
| 7 | + |
| 8 | +from openai import _legacy_response |
| 9 | +from openai._types import Body, Omit, Query, Headers, NotGiven, omit, not_given |
| 10 | +from openai._utils import maybe_transform, async_maybe_transform |
| 11 | +from openai._base_client import make_request_options |
| 12 | +from openai.resources.realtime.calls import Calls, AsyncCalls |
| 13 | +from openai.types.realtime.realtime_session_create_request_param import RealtimeSessionCreateRequestParam |
| 14 | + |
| 15 | +__all__ = ["_Calls", "_AsyncCalls"] |
| 16 | + |
| 17 | + |
| 18 | +# Custom code to override the `create` method to have correct behavior with |
| 19 | +# application/sdp and multipart/form-data. |
| 20 | +# Ideally we can cutover to the generated code this overrides eventually and remove this. |
| 21 | +class _Calls(Calls): |
| 22 | + @override |
| 23 | + def create( |
| 24 | + self, |
| 25 | + *, |
| 26 | + sdp: str, |
| 27 | + session: RealtimeSessionCreateRequestParam | Omit = omit, |
| 28 | + extra_headers: Headers | None = None, |
| 29 | + extra_query: Query | None = None, |
| 30 | + extra_body: Body | None = None, |
| 31 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 32 | + ) -> _legacy_response.HttpxBinaryResponseContent: |
| 33 | + if session is omit: |
| 34 | + extra_headers = {"Accept": "application/sdp", "Content-Type": "application/sdp", **(extra_headers or {})} |
| 35 | + return self._post( |
| 36 | + "/realtime/calls", |
| 37 | + body=sdp.encode("utf-8"), |
| 38 | + options=make_request_options(extra_headers=extra_headers, extra_query=extra_query, timeout=timeout), |
| 39 | + cast_to=_legacy_response.HttpxBinaryResponseContent, |
| 40 | + ) |
| 41 | + |
| 42 | + extra_headers = {"Accept": "application/sdp", "Content-Type": "multipart/form-data", **(extra_headers or {})} |
| 43 | + session_payload = maybe_transform(session, RealtimeSessionCreateRequestParam) |
| 44 | + files = [ |
| 45 | + ("sdp", (None, sdp.encode("utf-8"), "application/sdp")), |
| 46 | + ("session", (None, json.dumps(session_payload).encode("utf-8"), "application/json")), |
| 47 | + ] |
| 48 | + return self._post( |
| 49 | + "/realtime/calls", |
| 50 | + files=files, |
| 51 | + options=make_request_options( |
| 52 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 53 | + ), |
| 54 | + cast_to=_legacy_response.HttpxBinaryResponseContent, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +class _AsyncCalls(AsyncCalls): |
| 59 | + @override |
| 60 | + async def create( |
| 61 | + self, |
| 62 | + *, |
| 63 | + sdp: str, |
| 64 | + session: RealtimeSessionCreateRequestParam | Omit = omit, |
| 65 | + extra_headers: Headers | None = None, |
| 66 | + extra_query: Query | None = None, |
| 67 | + extra_body: Body | None = None, |
| 68 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 69 | + ) -> _legacy_response.HttpxBinaryResponseContent: |
| 70 | + if session is omit: |
| 71 | + extra_headers = {"Accept": "application/sdp", "Content-Type": "application/sdp", **(extra_headers or {})} |
| 72 | + return await self._post( |
| 73 | + "/realtime/calls", |
| 74 | + body=sdp.encode("utf-8"), |
| 75 | + options=make_request_options(extra_headers=extra_headers, extra_query=extra_query, timeout=timeout), |
| 76 | + cast_to=_legacy_response.HttpxBinaryResponseContent, |
| 77 | + ) |
| 78 | + |
| 79 | + extra_headers = {"Accept": "application/sdp", "Content-Type": "multipart/form-data", **(extra_headers or {})} |
| 80 | + session_payload = await async_maybe_transform(session, RealtimeSessionCreateRequestParam) |
| 81 | + files = [ |
| 82 | + ("sdp", (None, sdp.encode("utf-8"), "application/sdp")), |
| 83 | + ("session", (None, json.dumps(session_payload).encode("utf-8"), "application/json")), |
| 84 | + ] |
| 85 | + return await self._post( |
| 86 | + "/realtime/calls", |
| 87 | + files=files, |
| 88 | + options=make_request_options( |
| 89 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 90 | + ), |
| 91 | + cast_to=_legacy_response.HttpxBinaryResponseContent, |
| 92 | + ) |
0 commit comments