use {
super::{Error, configuration},
crate::{
apis::{ContentType, ResponseContent},
models,
},
async_trait::async_trait,
reqwest,
serde::{Deserialize, Serialize, de::Error as _},
std::sync::Arc,
};
#[async_trait]
pub trait TradingBetaApi: Send + Sync {
async fn create_order(
&self,
params: CreateOrderParams,
) -> Result<models::OrderDetails, Error<CreateOrderError>>;
async fn create_quote(
&self,
params: CreateQuoteParams,
) -> Result<models::QuotesResponse, Error<CreateQuoteError>>;
async fn get_order(
&self,
params: GetOrderParams,
) -> Result<models::OrderDetails, Error<GetOrderError>>;
async fn get_orders(
&self,
params: GetOrdersParams,
) -> Result<models::GetOrdersResponse, Error<GetOrdersError>>;
async fn get_trading_providers(
&self,
params: GetTradingProvidersParams,
) -> Result<models::ProvidersListResponse, Error<GetTradingProvidersError>>;
}
pub struct TradingBetaApiClient {
configuration: Arc<configuration::Configuration>,
}
impl TradingBetaApiClient {
pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
Self { configuration }
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct CreateOrderParams {
pub create_order_request: models::CreateOrderRequest,
pub idempotency_key: Option<String>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct CreateQuoteParams {
pub create_quote: models::CreateQuote,
pub idempotency_key: Option<String>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct GetOrderParams {
pub order_id: String,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct GetOrdersParams {
pub page_size: u8,
pub page_cursor: Option<String>,
pub order: Option<String>,
pub account_id: Option<Vec<String>>,
pub provider_id: Option<Vec<String>>,
pub statuses: Option<Vec<models::OrderStatus>>,
pub start_time: Option<u32>,
pub end_time: Option<u32>,
pub asset_conversion_type: Option<String>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct GetTradingProvidersParams {
pub page_size: Option<u8>,
pub page_cursor: Option<String>,
}
#[async_trait]
impl TradingBetaApi for TradingBetaApiClient {
async fn create_order(
&self,
params: CreateOrderParams,
) -> Result<models::OrderDetails, Error<CreateOrderError>> {
let CreateOrderParams {
create_order_request,
idempotency_key,
} = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!("{}/trading/orders", local_var_configuration.base_path);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(local_var_param_value) = idempotency_key {
local_var_req_builder =
local_var_req_builder.header("Idempotency-Key", local_var_param_value.to_string());
}
local_var_req_builder = local_var_req_builder.json(&create_order_request);
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_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::OrderDetails`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `models::OrderDetails`"
))));
}
}
} else {
let local_var_entity: Option<CreateOrderError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
async fn create_quote(
&self,
params: CreateQuoteParams,
) -> Result<models::QuotesResponse, Error<CreateQuoteError>> {
let CreateQuoteParams {
create_quote,
idempotency_key,
} = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!("{}/trading/quotes", local_var_configuration.base_path);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(local_var_param_value) = idempotency_key {
local_var_req_builder =
local_var_req_builder.header("Idempotency-Key", local_var_param_value.to_string());
}
local_var_req_builder = local_var_req_builder.json(&create_quote);
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_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::QuotesResponse`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `models::QuotesResponse`"
))));
}
}
} else {
let local_var_entity: Option<CreateQuoteError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
async fn get_order(
&self,
params: GetOrderParams,
) -> Result<models::OrderDetails, Error<GetOrderError>> {
let GetOrderParams { order_id } = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!(
"{}/trading/orders/{orderId}",
local_var_configuration.base_path,
orderId = crate::apis::urlencode(order_id)
);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_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::OrderDetails`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `models::OrderDetails`"
))));
}
}
} else {
let local_var_entity: Option<GetOrderError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
async fn get_orders(
&self,
params: GetOrdersParams,
) -> Result<models::GetOrdersResponse, Error<GetOrdersError>> {
let GetOrdersParams {
page_size,
page_cursor,
order,
account_id,
provider_id,
statuses,
start_time,
end_time,
asset_conversion_type,
} = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!("{}/trading/orders", local_var_configuration.base_path);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
local_var_req_builder =
local_var_req_builder.query(&[("pageSize", &page_size.to_string())]);
if let Some(ref param_value) = page_cursor {
local_var_req_builder =
local_var_req_builder.query(&[("pageCursor", ¶m_value.to_string())]);
}
if let Some(ref param_value) = order {
local_var_req_builder =
local_var_req_builder.query(&[("order", ¶m_value.to_string())]);
}
if let Some(ref param_value) = account_id {
local_var_req_builder = match "multi" {
"multi" => local_var_req_builder.query(
¶m_value
.into_iter()
.map(|p| ("accountId".to_owned(), p.to_string()))
.collect::<Vec<(std::string::String, std::string::String)>>(),
),
_ => local_var_req_builder.query(&[(
"accountId",
¶m_value
.into_iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(",")
.to_string(),
)]),
};
}
if let Some(ref param_value) = provider_id {
local_var_req_builder = match "multi" {
"multi" => local_var_req_builder.query(
¶m_value
.into_iter()
.map(|p| ("providerId".to_owned(), p.to_string()))
.collect::<Vec<(std::string::String, std::string::String)>>(),
),
_ => local_var_req_builder.query(&[(
"providerId",
¶m_value
.into_iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(",")
.to_string(),
)]),
};
}
if let Some(ref param_value) = statuses {
local_var_req_builder = match "multi" {
"multi" => local_var_req_builder.query(
¶m_value
.into_iter()
.map(|p| ("statuses".to_owned(), p.to_string()))
.collect::<Vec<(std::string::String, std::string::String)>>(),
),
_ => local_var_req_builder.query(&[(
"statuses",
¶m_value
.into_iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(",")
.to_string(),
)]),
};
}
if let Some(ref param_value) = start_time {
local_var_req_builder =
local_var_req_builder.query(&[("startTime", ¶m_value.to_string())]);
}
if let Some(ref param_value) = end_time {
local_var_req_builder =
local_var_req_builder.query(&[("endTime", ¶m_value.to_string())]);
}
if let Some(ref param_value) = asset_conversion_type {
local_var_req_builder =
local_var_req_builder.query(&[("assetConversionType", ¶m_value.to_string())]);
}
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_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::GetOrdersResponse`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `models::GetOrdersResponse`"
))));
}
}
} else {
let local_var_entity: Option<GetOrdersError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
async fn get_trading_providers(
&self,
params: GetTradingProvidersParams,
) -> Result<models::ProvidersListResponse, Error<GetTradingProvidersError>> {
let GetTradingProvidersParams {
page_size,
page_cursor,
} = params;
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!("{}/trading/providers", local_var_configuration.base_path);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
if let Some(ref param_value) = page_size {
local_var_req_builder =
local_var_req_builder.query(&[("pageSize", ¶m_value.to_string())]);
}
if let Some(ref param_value) = page_cursor {
local_var_req_builder =
local_var_req_builder.query(&[("pageCursor", ¶m_value.to_string())]);
}
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => {
crate::deserialize_wrapper(&local_var_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::ProvidersListResponse`",
)));
}
ContentType::Unsupported(local_var_unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{local_var_unknown_type}` content type response that cannot be \
converted to `models::ProvidersListResponse`"
))));
}
}
} else {
let local_var_entity: Option<GetTradingProvidersError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateOrderError {
Status404(models::TradingErrorResponse),
Status401(models::TradingErrorResponse),
Status5XX(models::TradingErrorResponse),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateQuoteError {
Status404(models::TradingErrorResponse),
Status401(models::TradingErrorResponse),
Status5XX(models::TradingErrorResponse),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetOrderError {
Status404(models::TradingErrorResponse),
Status401(models::TradingErrorResponse),
Status5XX(models::TradingErrorResponse),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetOrdersError {
Status404(models::TradingErrorResponse),
Status401(models::TradingErrorResponse),
Status5XX(models::TradingErrorResponse),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetTradingProvidersError {
Status401(models::TradingErrorResponse),
Status5XX(models::TradingErrorResponse),
UnknownValue(serde_json::Value),
}