mirror of
https://github.com/Noratrieb/terustform.git
synced 2026-01-16 01:15:10 +01:00
stuff
This commit is contained in:
parent
854f7bb2bc
commit
85d10ed893
15 changed files with 328 additions and 160 deletions
|
|
@ -1,3 +1,51 @@
|
|||
pub struct _CorsClient {
|
||||
client: reqwest::Client
|
||||
use eyre::{Context, OptionExt, Result};
|
||||
use reqwest::header::{HeaderMap, HeaderValue};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CorsClient {
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
const URL: &str = "https://api.cors-school.nilstrieb.dev/api";
|
||||
|
||||
impl CorsClient {
|
||||
pub async fn new(email: String, password: String) -> Result<Self> {
|
||||
let client = reqwest::Client::new();
|
||||
let login = dto::UserLogin { email, password };
|
||||
let token = client
|
||||
.post(format!("{URL}/login"))
|
||||
.json(&login)
|
||||
.send()
|
||||
.await
|
||||
.wrap_err("failed to send login request")?;
|
||||
let token = token.error_for_status().wrap_err("failed to login")?;
|
||||
let token = token
|
||||
.headers()
|
||||
.get("Token")
|
||||
.ok_or_eyre("does not have Token header in login response")?
|
||||
.to_str()
|
||||
.wrap_err("Token is invalid utf8")?;
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Authorization",
|
||||
HeaderValue::from_str(&format!("Bearer {}", token,)).unwrap(),
|
||||
);
|
||||
let client = reqwest::Client::builder()
|
||||
.default_headers(headers)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
Ok(Self { client })
|
||||
}
|
||||
|
||||
pub async fn get_hugo(&self) -> Result<String> {
|
||||
Ok(self
|
||||
.client
|
||||
.get(format!("{URL}/hugo"))
|
||||
.send()
|
||||
.await?
|
||||
.text()
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue