2024-03-19 12:07:37 -05:00
|
|
|
use crate::result::Result;
|
2024-03-18 22:46:41 -05:00
|
|
|
use reqwest::{
|
|
|
|
header::{HeaderMap, HeaderValue},
|
|
|
|
Client, Url,
|
|
|
|
};
|
2024-03-19 12:07:37 -05:00
|
|
|
use reqwest_middleware::ClientWithMiddleware;
|
2024-03-18 22:46:41 -05:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
pub struct GitLab {
|
|
|
|
url: Url,
|
|
|
|
client: ClientWithMiddleware,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
pub struct User {
|
|
|
|
pub id: u64,
|
|
|
|
pub username: String,
|
|
|
|
pub email: String,
|
|
|
|
pub name: String,
|
|
|
|
pub state: String,
|
|
|
|
pub locked: bool,
|
|
|
|
pub created_at: String,
|
|
|
|
pub public_email: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GitLab {
|
2024-03-19 12:07:37 -05:00
|
|
|
pub fn try_new(url: &str, token: &str) -> Result<Self> {
|
|
|
|
let url = Url::parse(&format!("{}{}", url.trim_end_matches('/'), "/"))?;
|
2024-03-18 22:46:41 -05:00
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert("PRIVATE-TOKEN", HeaderValue::from_str(token)?);
|
2024-03-19 12:07:37 -05:00
|
|
|
headers.insert("content-type", HeaderValue::from_str("application/json")?);
|
|
|
|
headers.insert("accepts", HeaderValue::from_str("application/json")?);
|
2024-03-18 22:46:41 -05:00
|
|
|
|
2024-03-19 12:07:37 -05:00
|
|
|
let base_client = Client::builder().default_headers(headers).build()?;
|
|
|
|
let client = crate::client::wrap_with_middleware(base_client);
|
2024-03-18 22:46:41 -05:00
|
|
|
|
|
|
|
Ok(Self { url, client })
|
|
|
|
}
|
|
|
|
|
2024-03-19 12:07:37 -05:00
|
|
|
pub fn url(&self, path: &str) -> Result<Url> {
|
|
|
|
Ok(self.url.join(path.trim_start_matches('/'))?)
|
2024-03-18 22:46:41 -05:00
|
|
|
}
|
|
|
|
|
2024-03-19 12:07:37 -05:00
|
|
|
pub async fn me(&self) -> Result<User> {
|
2024-03-18 22:46:41 -05:00
|
|
|
let res = self.client.get(self.url("/user")?).send().await?;
|
2024-03-19 12:07:37 -05:00
|
|
|
Ok(res.json().await?)
|
2024-03-18 22:46:41 -05:00
|
|
|
}
|
|
|
|
}
|