use crate::client::Client;
use crate::client::client::BASE_URL;
use crate::error::ApiError;
use reqwest::Method as HttpMethod;
use serde::Serialize;
pub enum Method {
Get,
Post,
Put,
Delete,
}
fn transform_method(method: Method) -> HttpMethod {
match method {
Method::Get => HttpMethod::GET,
Method::Post => HttpMethod::POST,
Method::Put => HttpMethod::PUT,
Method::Delete => HttpMethod::DELETE,
}
}
impl<State> Client<State> {
pub(crate) async fn call_api<T: serde::de::DeserializeOwned>(
&mut self,
method: Method,
path: &str,
body: Option<&impl Serialize>,
) -> Result<T, ApiError> {
let builder = self
.http
.request(transform_method(method), BASE_URL.to_owned() + path);
let builder = if let Some(body) = body {
builder.json(body)
} else {
builder
};
match builder.send().await {
Ok(resp) => {
let body = resp
.text()
.await
.map_err(|_| ApiError::Unknown("Error".to_string()))?;
let data = serde_json::from_str::<T>(&body);
match data {
Ok(data) => Ok(data),
Err(err) => {
Err(ApiError::ParsingError(format!("Error Parsing: {:?}", err).to_string()))
},
}
}
Err(_) => Err(ApiError::RequestError),
}
}
}