apibrasil 1.0.0

SDK oficial Rust da plataforma APIBrasil: WhatsApp, SMS, consultas de CPF/CNPJ, veiculos, CEP, correios, pagamentos PIX/boleto e mais.
Documentation
use serde_json::json;

use crate::core::{IntoBody, Json, Result};

platform_service! {
    /// IP Whitelist da conta (`/ip-whitelist/*`). Restringe de quais IPs o
    /// Bearer Token pode ser usado.
    IpWhitelistService
}

impl IpWhitelistService {
    /// Configuração atual: `GET /ip-whitelist`.
    pub async fn get(&self) -> Result<Json> {
        self.base.get("ip-whitelist").await
    }

    /// Substitui a lista inteira: `PUT /ip-whitelist`.
    pub async fn set<I, S>(&self, entries: I) -> Result<Json>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let entries: Vec<String> = entries.into_iter().map(Into::into).collect();
        self.base
            .put("ip-whitelist", json!({ "ip_whitelist": entries }))
            .await
    }

    /// Adiciona uma entrada: `POST /ip-whitelist/add`.
    pub async fn add(&self, entry: &str) -> Result<Json> {
        self.base
            .post("ip-whitelist/add", json!({ "entry": entry }))
            .await
    }

    /// Remove uma entrada: `DELETE /ip-whitelist/remove`.
    pub async fn remove(&self, entry: &str) -> Result<Json> {
        self.base
            .delete("ip-whitelist/remove", json!({ "entry": entry }))
            .await
    }

    /// Valida uma entrada: `POST /ip-whitelist/validate`.
    pub async fn validate(&self, entry: &str) -> Result<Json> {
        self.base
            .post("ip-whitelist/validate", json!({ "entry": entry }))
            .await
    }

    /// IP atual visto pelo gateway: `GET /ip-whitelist/current-ip`.
    pub async fn current_ip(&self) -> Result<Json> {
        self.base.get("ip-whitelist/current-ip").await
    }
}

json_empty! {
    IpWhitelistService {
        /// Adiciona o IP atual: `POST /ip-whitelist/add-current`.
        add_current => post "ip-whitelist/add-current",
        /// Libera todos os IPs (wildcard): `POST /ip-whitelist/reset`.
        reset => post "ip-whitelist/reset",
    }
}

platform_service! {
    /// Rate limit por Bearer Token (`/bearer-rate-limit`).
    BearerRateLimitService
}

impl BearerRateLimitService {
    /// Limite atual: `GET /bearer-rate-limit`.
    pub async fn get(&self) -> Result<Json> {
        self.base.get("bearer-rate-limit").await
    }

    /// Define o limite por minuto: `PUT /bearer-rate-limit`.
    pub async fn set(&self, body: impl IntoBody) -> Result<Json> {
        self.base.put("bearer-rate-limit", body).await
    }
}