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