use crate::{
config::Config,
error::OpenAIError,
types::realtime::{
RealtimeCallAcceptRequest, RealtimeCallCreateRequest, RealtimeCallCreateResponse,
RealtimeCallReferRequest, RealtimeCallRejectRequest, RealtimeCreateClientSecretRequest,
RealtimeCreateClientSecretResponse,
},
Client, RequestOptions,
};
pub struct Realtime<'c, C: Config> {
client: &'c Client<C>,
pub(crate) request_options: RequestOptions,
}
impl<'c, C: Config> Realtime<'c, C> {
pub fn new(client: &'c Client<C>) -> Self {
Self {
client,
request_options: RequestOptions::new(),
}
}
pub async fn create_call(
&self,
request: RealtimeCallCreateRequest,
) -> Result<RealtimeCallCreateResponse, OpenAIError> {
let (bytes, headers) = self
.client
.post_form_raw("/realtime/calls", request, &self.request_options)
.await?;
let location = headers
.get("location")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
if location.is_none() {
tracing::warn!("Location header not found in Realtime call creation response");
}
let sdp = String::from_utf8_lossy(&bytes).into_owned();
Ok(RealtimeCallCreateResponse { sdp, location })
}
#[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn accept_call(
&self,
call_id: &str,
request: RealtimeCallAcceptRequest,
) -> Result<(), OpenAIError> {
self.client
.post(
&format!("/realtime/calls/{}/accept", call_id),
request,
&self.request_options,
)
.await
}
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn hangup_call(&self, call_id: &str) -> Result<(), OpenAIError> {
self.client
.post(
&format!("/realtime/calls/{}/hangup", call_id),
(),
&self.request_options,
)
.await
}
#[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn refer_call(
&self,
call_id: &str,
request: RealtimeCallReferRequest,
) -> Result<(), OpenAIError> {
self.client
.post(
&format!("/realtime/calls/{}/refer", call_id),
request,
&self.request_options,
)
.await
}
#[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn reject_call(
&self,
call_id: &str,
request: RealtimeCallRejectRequest,
) -> Result<(), OpenAIError> {
self.client
.post(
&format!("/realtime/calls/{}/reject", call_id),
request,
&self.request_options,
)
.await
}
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn create_client_secret(
&self,
request: RealtimeCreateClientSecretRequest,
) -> Result<RealtimeCreateClientSecretResponse, OpenAIError> {
self.client
.post("/realtime/client_secrets", request, &self.request_options)
.await
}
}