82 lines
1.8 KiB
Rust
82 lines
1.8 KiB
Rust
use super::models;
|
|
use crate::api::core::models::Error;
|
|
use crate::core;
|
|
|
|
///
|
|
pub struct Api {
|
|
client: reqwest::Client,
|
|
api_config: core::config::ApiConfig,
|
|
}
|
|
|
|
impl std::fmt::Debug for Api {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
f.debug_struct("Api of api.kgon.identity").finish()
|
|
}
|
|
}
|
|
|
|
impl Api {
|
|
///
|
|
pub fn new(api_config: core::config::ApiConfig) -> Api {
|
|
Api {
|
|
client: reqwest::Client::new(),
|
|
api_config,
|
|
}
|
|
}
|
|
|
|
///
|
|
pub async fn list_vendors(
|
|
&self,
|
|
data: models::ListVendorsRequest,
|
|
) -> Result<models::ListVendorsResponse, Error> {
|
|
let res = match self
|
|
.client
|
|
.post(format!("{}/vendors", self.api_config.k_url))
|
|
.header(reqwest::header::ACCEPT, "application/json")
|
|
.header(
|
|
reqwest::header::CONTENT_TYPE,
|
|
"application/x-www-form-urlencoded",
|
|
)
|
|
.header("k-secret", self.api_config.k_secret.as_str())
|
|
.header("k-username", self.api_config.k_username.as_str())
|
|
.send()
|
|
.await
|
|
{
|
|
Ok(res) => res,
|
|
Err(e) => {
|
|
return Err(Error {
|
|
code: -1,
|
|
msg: Some(e.to_string()),
|
|
});
|
|
}
|
|
};
|
|
|
|
match res.status() {
|
|
reqwest::StatusCode::OK => match res.json::<models::_ListVendorsResponse>().await {
|
|
Ok(r) => {
|
|
if r.code != 0 {
|
|
return Err(Error {
|
|
code: r.code,
|
|
msg: r.msg,
|
|
});
|
|
}
|
|
|
|
let vendors = match r.vendors {
|
|
Some(v) => v,
|
|
None => vec![],
|
|
};
|
|
|
|
Ok(models::ListVendorsResponse { vendors })
|
|
}
|
|
Err(e) => Err(Error {
|
|
code: -1,
|
|
msg: Some(e.to_string()),
|
|
}),
|
|
},
|
|
_ => Err(Error {
|
|
code: -1,
|
|
msg: None,
|
|
}),
|
|
}
|
|
}
|
|
}
|