1use reqwest::{header, Client, Method, StatusCode};
2
3use crate::{Error, Result};
4
5pub struct Info {
6 pub account_id: String,
7 pub user_id: String,
8 pub token: String,
9}
10
11pub async fn auth(url: &str, value: &str) -> Result<Info> {
12 let response = Client::new()
13 .request(Method::POST, url)
14 .header(header::AUTHORIZATION, value)
15 .send()
16 .await
17 .map_err(Error::any)?;
18 match response.status() {
19 StatusCode::OK => {}
20 StatusCode::FORBIDDEN => {
21 return Err(Error::Forbidden(response.text().await.map_err(Error::any)?))
22 }
23 StatusCode::INTERNAL_SERVER_ERROR => {
24 return Err(Error::Any(anyhow::anyhow!(response
25 .text()
26 .await
27 .map_err(Error::any)?)))
28 }
29 _ => return Err(Error::NotFound("not right status code".to_string())),
30 };
31 let headers = response.headers();
32 let account_id = headers
33 .get("X-Account-Id")
34 .ok_or_else(|| Error::NotFound("not found X-Account-Id".to_string()))?
35 .to_str()
36 .map_err(Error::any)?;
37 let user_id = headers
38 .get("X-User-Id")
39 .ok_or_else(|| Error::NotFound("not found X-User-Id".to_string()))?
40 .to_str()
41 .map_err(Error::any)?;
42 let token = match headers.get("X-Auth-Token") {
43 Some(v) => v.to_str().map_err(Error::any)?,
44 None => "",
45 };
46
47 Ok(Info {
48 account_id: account_id.to_string(),
49 user_id: user_id.to_string(),
50 token: token.to_string(),
51 })
52}