use crate::framework::response::ApiResult;
use crate::framework::Environment;
use serde::Serialize;
use std::borrow::Cow;
use url::Url;
pub use http::Method;
#[cfg(feature = "endpoint-spec")]
pub use spec::EndpointSpec;
#[cfg(not(feature = "endpoint-spec"))]
pub(crate) use spec::EndpointSpec;
mod spec {
use super::*;
pub trait EndpointSpec<ResultType>
where
ResultType: ApiResult,
{
fn method(&self) -> http::Method;
fn path(&self) -> String;
#[inline]
fn query(&self) -> Option<String> {
None
}
#[inline]
fn body(&self) -> Option<String> {
None
}
fn url(&self, environment: &Environment) -> Url {
let mut url = Url::from(environment).join(&self.path()).unwrap();
url.set_query(self.query().as_deref());
url
}
fn content_type(&self) -> Cow<'static, str> {
Cow::Borrowed("application/json")
}
}
}
impl<T: ApiResult, U: EndpointSpec<T>> Endpoint<T> for U {}
pub trait Endpoint<ResultType: ApiResult>: spec::EndpointSpec<ResultType> {}
#[inline]
pub(crate) fn serialize_query<Q: Serialize>(q: &Q) -> Option<String> {
serde_urlencoded::to_string(q).ok()
}