use reqwest::header::{self, HeaderMap, HeaderValue};
use std::time::Duration;
#[cfg(doctest)]
mod _disabled {}
#[cfg(not(doctest))]
mod generated {
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
}
#[cfg(not(doctest))]
pub use generated::*;
#[cfg(doctest)]
pub struct Client;
#[cfg(doctest)]
impl Client {
pub fn from_token(_token: &str) -> Self {
Self
}
pub fn with_client(_token: &str, _client: reqwest::Client) -> Self {
Self
}
pub fn baseurl(&self) -> &str {
"https://api.digitalocean.com"
}
}
#[cfg(not(doctest))]
impl Client {
pub fn from_token(token: &str) -> Self {
let mut headers = HeaderMap::new();
let auth_value = HeaderValue::from_str(&format!("Bearer {}", token))
.expect("Failed to create authorization header");
headers.insert(header::AUTHORIZATION, auth_value);
let http_client = reqwest::ClientBuilder::new()
.connect_timeout(Duration::from_secs(15))
.timeout(Duration::from_secs(30))
.default_headers(headers)
.user_agent("rsdo/0.1.0")
.build()
.expect("Failed to build HTTP client");
Self::new_with_client("https://api.digitalocean.com", http_client)
}
pub fn with_client(_token: &str, http_client: reqwest::Client) -> Self {
Self::new_with_client("https://api.digitalocean.com", http_client)
}
}
#[cfg(not(doctest))]
pub use types::*;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_client_creation() {
let client = Client::from_token("test-token");
assert_eq!(client.baseurl(), "https://api.digitalocean.com");
}
#[tokio::test]
async fn test_client_user_agent() {
let client = Client::from_token("test-token");
assert_eq!(client.baseurl(), "https://api.digitalocean.com");
}
}