edgee_api_client/
lib.rs

1pub mod auth;
2pub mod data_collection;
3mod upload;
4
5pub const PROD_BASEURL: &str = "https://api.edgee.app";
6
7progenitor::generate_api! {
8    spec = "openapi.json",
9    interface = Builder,
10    derives = [ schemars::JsonSchema ],
11}
12
13/// This crate's entry point
14///
15/// Use this function to build a client configured to interact with Edgee API using provided
16/// credentials
17#[bon::builder(
18    start_fn = new,
19    finish_fn = connect,
20    on(String, into),
21)]
22pub fn connect(#[builder(default = PROD_BASEURL)] baseurl: String, api_token: String) -> Client {
23    use reqwest::header::{self, HeaderMap};
24
25    let mut default_headers = HeaderMap::new();
26
27    let auth_value = format!("Bearer {api_token}");
28    default_headers.insert(header::AUTHORIZATION, auth_value.try_into().unwrap());
29
30    let client = reqwest::Client::builder()
31        .user_agent(concat!("edgee/", env!("CARGO_PKG_VERSION")))
32        .default_headers(default_headers)
33        .build()
34        .unwrap();
35
36    Client::new_with_client(&baseurl, client)
37}
38
39#[easy_ext::ext(ErrorExt)]
40impl Error<types::ErrorResponse> {
41    pub fn into_message(self) -> String {
42        match self {
43            Error::ErrorResponse(err) => err.error.message.clone(),
44            _ => self.to_string(),
45        }
46    }
47}
48
49#[easy_ext::ext(ResultExt)]
50impl<T> Result<T, Error<types::ErrorResponse>> {
51    pub fn api_context(self, ctx: impl std::fmt::Display) -> anyhow::Result<T> {
52        self.map_err(|err| anyhow::anyhow!("{ctx}: {}", err.into_message()))
53    }
54
55    pub fn api_with_context<F, C>(self, f: F) -> anyhow::Result<T>
56    where
57        F: FnOnce() -> C,
58        C: std::fmt::Display,
59    {
60        self.map_err(|err| {
61            let ctx = f();
62            anyhow::anyhow!("{ctx}: {}", err.into_message())
63        })
64    }
65}