dexrs/dexcom/
request.rs

1use reqwest::StatusCode;
2
3use super::client::DexcomClient;
4
5impl DexcomClient {
6    pub(crate) fn post(
7        &self,
8        endpoint: &str,
9        json: serde_json::Value,
10        params: Vec<(&str, &str)>,
11    ) -> Result<reqwest::blocking::Response, Box<dyn std::error::Error>> {
12        let resp = self.reqwest_client
13            .post(format!("{}/{}", self.base_url, endpoint))
14            .header("Accept-Encoding", "application/json")
15            .json(&json)
16            .query(&params)
17            .send();
18
19        match resp {
20            Ok(r) => {
21                if r.status() != StatusCode::OK {
22                    // oh no, an error occured! (server-side)
23                    // for our purposes, we can panic here
24                    // but it would be best to use different error types
25                    println!("Error: {:?}", r.text().unwrap());
26                    return Err("server-side dexcom error".into());
27                }
28
29                Ok(r)
30            },
31            Err(e) => Err(Box::new(e)),
32        }
33    }
34}