rust_api_calling 1.0.0

A clean, idiomatic Rust HTTP client library with a single core request function and convenient GET/POST wrappers. Features builder pattern configuration, typed responses via serde generics, automatic session/cookie/XSRF management, and structured logging.
Documentation
/// All possible errors that can occur during an API call.
///
/// Instead of magic error codes (like NSError in Swift), each variant
/// clearly describes what went wrong and carries the relevant context.
#[derive(Debug, thiserror::Error)]
pub enum ApiError {
    /// The URL string could not be parsed into a valid URL.
    #[error("Invalid URL: {0}")]
    InvalidUrl(String),

    /// A network-level error occurred (connection refused, DNS failure, etc.).
    #[error("Network error: {0}")]
    NetworkError(#[from] reqwest::Error),

    /// Failed to serialize the request body to JSON.
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),

    /// The server returned a non-2xx status code.
    #[error("HTTP {status}: {body}")]
    HttpError {
        status: u16,
        body: String,
    },

    /// The server returned an empty response body.
    #[error("Empty response from server")]
    EmptyResponse,

    /// The request timed out before receiving a response.
    #[error("Request timed out")]
    Timeout,
}

impl ApiError {
    /// Returns `true` if this is a network-level error (connection, DNS, etc.).
    pub fn is_network_error(&self) -> bool {
        matches!(self, ApiError::NetworkError(_))
    }

    /// Returns `true` if this is a timeout error.
    pub fn is_timeout(&self) -> bool {
        matches!(self, ApiError::Timeout)
    }

    /// Returns the HTTP status code if this is an `HttpError`, or `None` otherwise.
    pub fn status_code(&self) -> Option<u16> {
        match self {
            ApiError::HttpError { status, .. } => Some(*status),
            _ => None,
        }
    }
}