reqkey 0.1.0

Official Rust SDK for ReqKey API key validation, credit metering, and analytics
Documentation
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// Stable SDK-level categories for a key-validation decision.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum VerificationReason {
    /// The consumer key is valid and the requested credits were deducted.
    Valid,
    /// ReqKey returned a normal invalid-key decision.
    InvalidKey,
    /// The key does not have enough credits.
    InsufficientCredits,
    /// The key cannot access the requested API or resource.
    Forbidden,
    /// A configured rate limit was exceeded.
    RateLimited,
    /// Another explicit access decision denied the request.
    Denied,
}

/// The normalized result of `/key/validate`.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct VerificationResult {
    /// Whether the request is allowed.
    pub valid: bool,
    /// Stable reason category derived from status and response fields.
    pub reason: VerificationReason,
    /// ReqKey HTTP status code.
    pub status_code: u16,
    /// Correlation ID used by analytics.
    pub request_id: Option<String>,
    /// Human-readable decision message.
    pub message: Option<String>,
    /// Resolved ReqKey API ID.
    pub api_id: Option<String>,
    /// Resolved ReqKey API name.
    pub api_name: Option<String>,
    /// Resolved resource path.
    pub resource: Option<String>,
    /// Credits left after this decision.
    pub credits_remaining: Option<i64>,
    /// Configured credit limit.
    pub credits_limit: Option<i64>,
    /// APIs available to the key.
    pub allowed_apis: Vec<String>,
    /// Number of seconds before retrying a rate-limited request.
    pub retry_after: Option<f64>,
    /// Provider rate-limit details, when returned.
    pub rate_limit: Option<Map<String, Value>>,
    /// Complete parsed response for forward compatibility.
    pub raw: Map<String, Value>,
}

impl VerificationResult {
    /// Alias that reads naturally inside authorization code.
    pub const fn allowed(&self) -> bool {
        self.valid
    }
}