taskr/src/gitlab.rs

102 lines
3 KiB
Rust
Raw Normal View History

2024-03-18 23:05:51 -05:00
use color_eyre::owo_colors::OwoColorize;
2024-03-18 22:46:41 -05:00
use reqwest::{
header::{HeaderMap, HeaderValue},
Client, Url,
};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use reqwest_tracing::TracingMiddleware;
use serde::Deserialize;
use tracing::trace;
pub struct GitLab {
url: Url,
client: ClientWithMiddleware,
}
// ---
/* "avatar_url": "http://localhost:3000/uploads/user/avatar/1/index.jpg",
// "web_url": "http://localhost:3000/john_smith",
// "created_at": "2012-05-23T08:00:58Z",
// "bio": "",
// "location": null,
// "skype": "",
// "linkedin": "",
// "twitter": "",
// "discord": "",
// "website_url": "",
// "organization": "",
// "job_title": "",
// "pronouns": "he/him",
// "bot": false,
// "work_information": null,
// "followers": 0,
// "following": 0,
// "local_time": "3:38 PM",
// "last_sign_in_at": "2012-06-01T11:41:01Z",
// "confirmed_at": "2012-05-23T09:05:22Z",
// "theme_id": 1,
// "last_activity_on": "2012-05-23",
// "color_scheme_id": 2,
// "projects_limit": 100,
// "current_sign_in_at": "2012-06-02T06:36:55Z",
// "identities": [
// {"provider": "github", "extern_uid": "2435223452345"},
// {"provider": "bitbucket", "extern_uid": "john_smith"},
// {"provider": "google_oauth2", "extern_uid": "8776128412476123468721346"}
// ],
// "can_create_group": true,
// "can_create_project": true,
// "two_factor_enabled": true,
// "external": false,
// "private_profile": false,
// "commit_email": "admin@example.com",
// }
*/
#[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 {
pub fn try_new(url: &str, token: &str) -> Result<Self, anyhow::Error> {
let url = Url::parse(url)?;
let mut headers = HeaderMap::new();
headers.insert("PRIVATE-TOKEN", HeaderValue::from_str(token)?);
2024-03-18 23:05:51 -05:00
let proxy = reqwest::Proxy::all("socks5h://localhost:9982")?;
2024-03-18 22:46:41 -05:00
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let base_client = Client::builder()
.default_headers(headers)
.proxy(proxy)
.build()?;
let client = ClientBuilder::new(base_client)
.with(TracingMiddleware::default())
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();
Ok(Self { url, client })
}
pub fn url(&self, path: &str) -> Result<Url, anyhow::Error> {
Ok(self.url.join(path)?)
}
pub async fn me(&self) -> Result<User, anyhow::Error> {
let res = self.client.get(self.url("/user")?).send().await?;
trace!("{res:?}");
let body = res.text().await?;
trace!("{body:?}");
Ok(serde_json::from_str::<User>(&body)?)
// Ok(res.json::<User>().await?)
}
}