use serde::Serialize;
use crate::{request::{LabraResponse, LabraRequest}, session::{SessionStore, SimpleStorage}, LabradorResult, RequestMethod, RequestType, Method};
#[derive(Debug, Clone)]
pub struct APIClient<T: SessionStore> {
pub app_key: String,
pub secret: String,
pub api_path: String,
pub session: T,
}
#[allow(unused)]
impl<T: SessionStore> APIClient<T> {
#[inline]
pub fn new<Q: Into<String>, S: Into<String>, R: Into<String>>(app_key: Q, secret: R, api_path: S) -> APIClient<SimpleStorage> {
APIClient {
app_key: app_key.into(),
secret: secret.into(),
api_path: api_path.into(),
session: SimpleStorage::new()
}
}
#[inline]
pub fn from_session<Q: Into<String>, S: Into<String>, R: Into<String>>(app_key: Q, secret: R, api_path: S, session: T) -> APIClient<T> {
APIClient {
app_key: app_key.into(),
secret: secret.into(),
api_path: api_path.into(),
session: session,
}
}
pub fn session(&self) -> &T {
&self.session
}
#[inline]
pub async fn request<D: Serialize>(&self, mut req: LabraRequest<D>) -> LabradorResult<LabraResponse> {
let mut api_path = self.api_path.to_owned();
let LabraRequest { url, ..} = req;
if url.starts_with("http") {
req.url = url;
} else {
req.url = api_path + &url;
}
req.request().await
}
pub async fn post<D: Serialize, R: RequestMethod>(&self, method: R, mut querys: Vec<(String, String)>, data: D, request_type: RequestType) -> LabradorResult<LabraResponse> {
let req = LabraRequest::new().url(method.get_method()).params(querys).method(Method::Post).json(data).req_type(request_type);
self.request(req).await
}
pub async fn get<R: RequestMethod>(&self, method: R, params: Vec<(String, String)>, request_type: RequestType) -> LabradorResult<LabraResponse> {
let req = LabraRequest::<String>::new().url(method.get_method()).params(params).method(Method::Get).req_type(request_type);
self.request(req).await
}
}