bigdatacloud 1.0.0

Official Rust SDK for BigDataCloud APIs — IP Geolocation, Reverse Geocoding, Phone & Email Verification, Network Engineering
Documentation
use std::collections::HashMap;
use std::time::Duration;
use reqwest::blocking::Client;
use serde::de::DeserializeOwned;
use crate::error::{Error, Result};

const BASE_URL: &str = "https://api-bdc.net/data/";
const TIMEOUT_SECS: u64 = 30;

/// Lightweight reqwest wrapper. Clone-safe — shares the underlying connection pool.
#[derive(Clone)]
pub struct HttpClient {
    api_key: String,
    client: Client,
}

impl HttpClient {
    pub fn new(api_key: String) -> Self {
        let client = Client::builder()
            .timeout(Duration::from_secs(TIMEOUT_SECS))
            .build()
            .expect("Failed to build HTTP client");
        Self { api_key, client }
    }

    pub fn get<T: DeserializeOwned>(&self, endpoint: &str, mut params: HashMap<String, String>) -> Result<T> {
        params.insert("key".to_string(), self.api_key.clone());
        // Remove empty values
        params.retain(|_, v| !v.is_empty());

        let url = format!("{}{}", BASE_URL, endpoint);
        let response = self.client.get(&url).query(&params).send()?;

        let status = response.status();
        if !status.is_success() {
            let body = response.text().unwrap_or_default();
            return Err(Error::Api {
                status_code: status.as_u16(),
                endpoint: endpoint.to_string(),
                body,
            });
        }

        Ok(response.json::<T>()?)
    }
}