use std::fmt::Debug;
use exchange_interactions_api_generics::{http, ws};
pub trait EndpointUrl {
fn url_mainnet(&self) -> url::Url;
fn url_testnet(&self) -> Option<url::Url>;
}
pub trait WsOption: HandlerOption {
type WsHandler: ws::WsHandler;
fn ws_handler(options: Self::Options) -> Self::WsHandler;
}
pub trait HttpOption<'a, R, B>: HandlerOption {
type RequestHandler: http::RequestHandler<B>;
fn request_handler(options: Self::Options) -> Self::RequestHandler;
}
pub trait HandlerOptions: Default + Clone + Debug {
type OptionItem: HandlerOption<Options = Self>;
fn update(&mut self, option: Self::OptionItem);
fn is_authenticated(&self) -> bool;
}
pub trait HandlerOption: Default {
type Options: HandlerOptions<OptionItem = Self>;
}
#[cfg(any(feature = "bitflyer", feature = "coincheck"))]
pub(crate) fn handle_untyped_response<T: serde::de::DeserializeOwned>(venue: &str, status: http::StatusCode, body: &http::Bytes) -> Result<T, http::HandleError> {
let truncated = || v_utils::utils::truncate_msg(String::from_utf8_lossy(body));
if status.is_success() {
return serde_json::from_slice(body).map_err(|error| http::HandleError::Parse(eyre::eyre!("Failed to parse response: {error}\nResponse body: {}", truncated())));
}
Err(match serde_json::from_slice::<serde_json::Value>(body) {
Ok(parsed) => http::HandleError::Api(http::ApiError::Other(eyre::eyre!("{venue} API error (status {status}): {parsed}"))),
Err(error) => http::HandleError::Parse(eyre::eyre!("Failed to parse error response: {error}\nResponse body: {}", truncated())),
})
}
#[doc(hidden)]
#[macro_export]
macro_rules! endpoint_urls {
($ty:ty { $($variant:ident => $mainnet:literal $(, testnet: $testnet:literal)? ;)+ }) => {
impl $crate::traits::EndpointUrl for $ty {
fn url_mainnet(&self) -> url::Url {
match self {
$(Self::$variant => url::Url::parse($mainnet).expect("literal"),)+
}
}
fn url_testnet(&self) -> Option<url::Url> {
match self {
$(Self::$variant => $crate::endpoint_urls!(@testnet $($testnet)?),)+
}
}
}
};
(@testnet $testnet:literal) => { Some(url::Url::parse($testnet).expect("literal")) };
(@testnet) => { None };
}