Skip to main content

personio_rs/shared/
https.rs

1use crate::Result;
2use reqwest::{header, Body, Client};
3use std::time::Duration;
4
5// #[derive(Clone)]
6pub struct HttpClient {
7    client: Client,
8}
9
10impl HttpClient {
11    pub fn new(partner_id: &str, app_id: &str) -> Result<HttpClient> {
12        let mut headers = header::HeaderMap::new();
13        let partner_id = header::HeaderValue::from_str(partner_id)?;
14        let app_id = header::HeaderValue::from_str(app_id)?;
15        let content_type = header::HeaderValue::from_str("application/json")?;
16        let accept = header::HeaderValue::from_str("application/json")?;
17
18        headers.insert("X-Personio-Partner-ID", partner_id);
19        headers.insert("X-Personio-App-ID", app_id);
20        headers.insert("Content-Type", content_type);
21        headers.insert("Accept", accept);
22
23        let client = Client::builder()
24            .timeout(Duration::from_secs(15))
25            .default_headers(headers)
26            .build()?;
27
28        Ok(HttpClient { client })
29    }
30
31    // pub async fn get(&self, url: &str) -> Result<String> {
32    //     let response = self.client.get(url).send().await?;
33    //     let body = response.text().await?;
34    //     Ok(body)
35    // }
36
37    pub async fn post<T: Into<Body>>(&self, url: &str, body: T) -> Result<String> {
38        let response = self.client.post(url).body(body).send().await?;
39        let body = response.text().await?;
40        Ok(body)
41    }
42}