use reqwest::Method;
use url::Url;
#[cfg(feature = "derive")]
pub use fav_derive::Api;
#[allow(missing_docs)]
pub enum DefaultApiKind {
Login,
QrLogin,
QrCheck,
Logout,
FetchResSet,
FetchRes,
PullRes,
}
pub trait ApiProvider {
type ApiKind;
fn api(&self, api_kind: Self::ApiKind) -> &dyn Api;
}
pub trait Api {
fn endpoint(&self) -> &'static str;
fn params(&self) -> &[&str];
fn cookie_keys(&self) -> &[&str] {
&[]
}
fn url(&self, params: Vec<String>) -> Url {
Url::parse_with_params(self.endpoint(), self.params().iter().zip(params)).unwrap()
}
fn method(&self) -> Method {
Method::GET
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Remote;
impl ApiProvider for Remote {
type ApiKind = DefaultApiKind;
fn api(&self, api_kind: DefaultApiKind) -> &dyn Api {
match api_kind {
DefaultApiKind::Login => todo!(),
_ => todo!(),
}
}
}
#[allow(dead_code)]
struct LoginApi;
impl Api for LoginApi {
fn endpoint(&self) -> &'static str {
"http://abc.com"
}
fn params(&self) -> &[&str] {
&["id", "pwd"]
}
}
}