/*
* OpenAI API
*
* The OpenAI REST API. Please see https://platform.openai.com/docs/api-reference for more details.
*
* The version of the OpenAPI document: 2.3.0
*
* Generated by: https://openapi-generator.tech
*/
#![allow(clippy::needless_return, clippy::into_iter_on_ref)]
use super::{configuration, multipart_helper, ContentType, Error};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{de::Error as _, Deserialize, Serialize};
/// struct for typed errors of method [`accept_realtime_call`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AcceptRealtimeCallError {
Status404(models::ErrorResponse),
Status409(models::ErrorResponse),
Status500(String),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`create_realtime_call`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateRealtimeCallError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`create_realtime_client_secret`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateRealtimeClientSecretError {
Status400(models::ErrorResponse),
Status404(models::ErrorResponse),
Status500(models::ErrorResponse),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`create_realtime_session`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateRealtimeSessionError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`create_realtime_transcription_session`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateRealtimeTranscriptionSessionError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`create_realtime_translation_client_secret`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateRealtimeTranslationClientSecretError {
Status400(models::ErrorResponse),
Status404(models::ErrorResponse),
Status500(models::ErrorResponse),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`hangup_realtime_call`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum HangupRealtimeCallError {
Status404(models::ErrorResponse),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`refer_realtime_call`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ReferRealtimeCallError {
Status404(models::ErrorResponse),
Status500(String),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`reject_realtime_call`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RejectRealtimeCallError {
Status404(models::ErrorResponse),
Status409(models::ErrorResponse),
Status500(String),
UnknownValue(serde_json::Value),
}
/// Accept an incoming SIP call and configure the realtime session that will handle it.
#[bon::builder]
pub async fn accept_realtime_call(
configuration: &configuration::Configuration,
call_id: &str,
realtime_session_create_request_ga: models::RealtimeSessionCreateRequestGa,
) -> Result<(), Error<AcceptRealtimeCallError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_path_call_id = call_id;
let p_body_realtime_session_create_request_ga = realtime_session_create_request_ga;
let uri_str = format!(
"{}/realtime/calls/{call_id}/accept",
configuration.base_path,
call_id = crate::apis::urlencode(p_path_call_id)
);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_realtime_session_create_request_ga);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<AcceptRealtimeCallError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}
/// Create a new Realtime API call over WebRTC and receive the SDP answer needed to complete the peer connection.
#[bon::builder]
pub async fn create_realtime_call(
configuration: &configuration::Configuration,
sdp: &str,
session: Option<models::RealtimeSessionCreateRequestGa>,
) -> Result<String, Error<CreateRealtimeCallError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_form_sdp = sdp;
let p_form_session = session;
let uri_str = format!("{}/realtime/calls", configuration.base_path);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let mut multipart_form = reqwest::multipart::Form::new();
multipart_form = multipart_helper::add_schema_value_to_form(
multipart_form,
"sdp",
serde_json::Value::String(p_form_sdp.to_string()),
Some("application/sdp"),
)?;
if let Some(param_value) = p_form_session {
multipart_form = multipart_helper::add_schema_value_to_form(
multipart_form,
"session",
serde_json::to_value(param_value)?,
Some("application/json"),
)?;
}
req_builder = req_builder.multipart(multipart_form);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `String`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `String`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<CreateRealtimeCallError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}
/// Create a Realtime client secret with an associated session configuration. Client secrets are short-lived tokens that can be passed to a client app, such as a web frontend or mobile client, which grants access to the Realtime API without leaking your main API key. You can configure a custom TTL for each client secret. You can also attach session configuration options to the client secret, which will be applied to any sessions created using that client secret, but these can also be overridden by the client connection. [Learn more about authentication with client secrets over WebRTC](https://developers.openai.com/api/docs/guides/realtime-webrtc). Returns the created client secret and the effective session object. The client secret is a string that looks like `ek_1234`.
#[bon::builder]
pub async fn create_realtime_client_secret(
configuration: &configuration::Configuration,
realtime_create_client_secret_request: models::RealtimeCreateClientSecretRequest,
) -> Result<models::RealtimeCreateClientSecretResponse, Error<CreateRealtimeClientSecretError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_body_realtime_create_client_secret_request = realtime_create_client_secret_request;
let uri_str = format!("{}/realtime/client_secrets", configuration.base_path);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_realtime_create_client_secret_request);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RealtimeCreateClientSecretResponse`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::RealtimeCreateClientSecretResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<CreateRealtimeClientSecretError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}
/// Create an ephemeral API token for use in client-side applications with the Realtime API. Can be configured with the same session parameters as the `session.update` client event. It responds with a session object, plus a `client_secret` key which contains a usable ephemeral API token that can be used to authenticate browser clients for the Realtime API. Returns the created Realtime session object, plus an ephemeral key.
#[bon::builder]
pub async fn create_realtime_session(
configuration: &configuration::Configuration,
realtime_session_create_request: models::RealtimeSessionCreateRequest,
) -> Result<models::RealtimeSessionCreateResponse, Error<CreateRealtimeSessionError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_body_realtime_session_create_request = realtime_session_create_request;
let uri_str = format!("{}/realtime/sessions", configuration.base_path);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_realtime_session_create_request);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RealtimeSessionCreateResponse`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::RealtimeSessionCreateResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<CreateRealtimeSessionError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}
/// Create an ephemeral API token for use in client-side applications with the Realtime API specifically for realtime transcriptions. Can be configured with the same session parameters as the `transcription_session.update` client event. It responds with a session object, plus a `client_secret` key which contains a usable ephemeral API token that can be used to authenticate browser clients for the Realtime API. Returns the created Realtime transcription session object, plus an ephemeral key.
#[bon::builder]
pub async fn create_realtime_transcription_session(
configuration: &configuration::Configuration,
realtime_transcription_session_create_request: models::RealtimeTranscriptionSessionCreateRequest,
) -> Result<
models::RealtimeTranscriptionSessionCreateResponse,
Error<CreateRealtimeTranscriptionSessionError>,
> {
// add a prefix to parameters to efficiently prevent name collisions
let p_body_realtime_transcription_session_create_request =
realtime_transcription_session_create_request;
let uri_str = format!(
"{}/realtime/transcription_sessions",
configuration.base_path
);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_realtime_transcription_session_create_request);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RealtimeTranscriptionSessionCreateResponse`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::RealtimeTranscriptionSessionCreateResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<CreateRealtimeTranscriptionSessionError> =
serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}
/// Create a Realtime translation client secret with an associated translation session configuration. Client secrets are short-lived tokens that can be passed to a client app, such as a web frontend or mobile client, which grants access to the Realtime Translation API without leaking your main API key. You can configure a custom TTL for each client secret. Returns the created client secret and the effective translation session object. The client secret is a string that looks like `ek_1234`.
#[bon::builder]
pub async fn create_realtime_translation_client_secret(
configuration: &configuration::Configuration,
realtime_translation_client_secret_create_request: models::RealtimeTranslationClientSecretCreateRequest,
) -> Result<
models::RealtimeTranslationClientSecretCreateResponse,
Error<CreateRealtimeTranslationClientSecretError>,
> {
// add a prefix to parameters to efficiently prevent name collisions
let p_body_realtime_translation_client_secret_create_request =
realtime_translation_client_secret_create_request;
let uri_str = format!(
"{}/realtime/translations/client_secrets",
configuration.base_path
);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_realtime_translation_client_secret_create_request);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RealtimeTranslationClientSecretCreateResponse`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::RealtimeTranslationClientSecretCreateResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<CreateRealtimeTranslationClientSecretError> =
serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}
/// End an active Realtime API call, whether it was initiated over SIP or WebRTC.
#[bon::builder]
pub async fn hangup_realtime_call(
configuration: &configuration::Configuration,
call_id: &str,
) -> Result<(), Error<HangupRealtimeCallError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_path_call_id = call_id;
let uri_str = format!(
"{}/realtime/calls/{call_id}/hangup",
configuration.base_path,
call_id = crate::apis::urlencode(p_path_call_id)
);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<HangupRealtimeCallError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}
/// Transfer an active SIP call to a new destination using the SIP REFER verb.
#[bon::builder]
pub async fn refer_realtime_call(
configuration: &configuration::Configuration,
call_id: &str,
realtime_call_refer_request: models::RealtimeCallReferRequest,
) -> Result<(), Error<ReferRealtimeCallError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_path_call_id = call_id;
let p_body_realtime_call_refer_request = realtime_call_refer_request;
let uri_str = format!(
"{}/realtime/calls/{call_id}/refer",
configuration.base_path,
call_id = crate::apis::urlencode(p_path_call_id)
);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_realtime_call_refer_request);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<ReferRealtimeCallError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}
/// Decline an incoming SIP call by returning a SIP status code to the caller.
#[bon::builder]
pub async fn reject_realtime_call(
configuration: &configuration::Configuration,
call_id: &str,
realtime_call_reject_request: Option<models::RealtimeCallRejectRequest>,
) -> Result<(), Error<RejectRealtimeCallError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_path_call_id = call_id;
let p_body_realtime_call_reject_request = realtime_call_reject_request;
let uri_str = format!(
"{}/realtime/calls/{call_id}/reject",
configuration.base_path,
call_id = crate::apis::urlencode(p_path_call_id)
);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_realtime_call_reject_request);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<RejectRealtimeCallError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(Box::new(ResponseContent {
status,
content,
entity,
})))
}
}