use std::marker::PhantomData;
use serde::Serialize;
use serde_json::Value;
use crate::api::operations::TypedOperation;
use crate::client::JorttClient;
use crate::error::JorttError;
pub mod endpoints;
pub mod operations;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PathParam {
pub name: String,
pub value: String,
}
impl PathParam {
pub fn new(name: impl Into<String>, value: impl ToString) -> Self {
Self {
name: name.into(),
value: value.to_string(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct QueryParam {
pub name: String,
pub value: String,
}
impl QueryParam {
pub fn new(name: impl Into<String>, value: impl ToString) -> Self {
Self {
name: name.into(),
value: value.to_string(),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct OperationRequest {
pub path_params: Vec<PathParam>,
pub query_params: Vec<QueryParam>,
pub body: Option<Value>,
pub accept: Option<String>,
}
impl OperationRequest {
pub fn builder() -> RequestBuilder {
RequestBuilder::new()
}
pub fn new() -> Self {
Self::default()
}
pub fn with_path_param(mut self, name: impl Into<String>, value: impl ToString) -> Self {
self.path_params.push(PathParam::new(name, value));
self
}
pub fn with_query_param(mut self, name: impl Into<String>, value: impl ToString) -> Self {
self.query_params.push(QueryParam::new(name, value));
self
}
pub fn with_json_body<T: Serialize>(mut self, value: &T) -> Result<Self, JorttError> {
self.body = Some(serde_json::to_value(value).map_err(JorttError::from_serialize)?);
Ok(self)
}
pub fn with_body_value(mut self, value: Value) -> Self {
self.body = Some(value);
self
}
pub fn with_accept(mut self, accept: impl Into<String>) -> Self {
self.accept = Some(accept.into());
self
}
}
#[derive(Debug, Clone, Default)]
pub struct RequestBuilder {
request: OperationRequest,
}
impl RequestBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn path_param(mut self, name: impl Into<String>, value: impl ToString) -> Self {
self.request.path_params.push(PathParam::new(name, value));
self
}
pub fn query_param(mut self, name: impl Into<String>, value: impl ToString) -> Self {
self.request.query_params.push(QueryParam::new(name, value));
self
}
pub fn json_body<T: Serialize>(mut self, value: &T) -> Result<Self, JorttError> {
self.request.body = Some(serde_json::to_value(value).map_err(JorttError::from_serialize)?);
Ok(self)
}
pub fn body_value(mut self, value: Value) -> Self {
self.request.body = Some(value);
self
}
pub fn accept(mut self, accept: impl Into<String>) -> Self {
self.request.accept = Some(accept.into());
self
}
pub fn build(self) -> OperationRequest {
self.request
}
}
#[derive(Clone)]
pub struct DomainApi<O> {
client: JorttClient,
_marker: PhantomData<O>,
}
impl<O> DomainApi<O> {
pub(crate) fn new(client: JorttClient) -> Self {
Self {
client,
_marker: PhantomData,
}
}
}
impl<O: TypedOperation> DomainApi<O> {
pub async fn execute(
&self,
operation: O,
request: OperationRequest,
) -> Result<Value, JorttError> {
self.client.execute_spec(operation.spec(), request).await
}
}