#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[macro_use]
mod macros;
pub mod core;
pub mod generated;
pub mod legacy;
pub mod services;
#[cfg(feature = "blocking")]
#[cfg_attr(docsrs, doc(cfg(feature = "blocking")))]
pub mod blocking;
use std::sync::Arc;
pub use self::core::{
Config, Consulta, CreditResponse, DeviceResponse, Error, ErrorKind, FnHooks, Hooks, HttpClient,
IntoBody, Json, Method, RequestHookInfo, RequestOptions, ReqwestTransport, ResponseData,
ResponseHookInfo, ResponseType, Result, RetryConfig, RetryHookInfo, Transport,
TransportRequest, TransportResponse, DEFAULT_BASE_URL, DEFAULT_TIMEOUT, USER_AGENT,
};
pub use self::services::data::{
BulkService, CepService, ChipVirtualService, ConsultaService, CorreiosService, DadosService,
DatabaseIpService, DddService, FipeService, GeolocationService, GeomatrixService,
HolidaysService, LoteriasService, RecognizeService, TranslateService, UraService,
VehiclesService, WeatherService,
};
pub use self::services::messaging::{
EvolutionService, SmsService, WhatsAppService, WhatsMeowService,
};
pub use self::services::platform::{
extract_token, requires_2fa, AccountService, AuthService, BearerRateLimitService,
CatalogService, DevicesService, IpWhitelistService, PaymentsService, ReportsService,
};
pub use self::services::{BaseService, CreditService, DeviceProxyService};
#[derive(Clone, Debug)]
pub struct ApiBrasil {
http: Arc<HttpClient>,
options: RequestOptions,
}
pub type Client = ApiBrasil;
impl ApiBrasil {
pub fn new(config: Config) -> Self {
Self::with_http(Arc::new(HttpClient::new(config)))
}
pub fn from_env() -> Self {
Self::new(Config::default())
}
pub fn with_http(http: Arc<HttpClient>) -> Self {
Self {
http,
options: RequestOptions::default(),
}
}
pub fn http(&self) -> &Arc<HttpClient> {
&self.http
}
pub fn options(&self) -> &RequestOptions {
&self.options
}
pub fn with_options(&self, options: RequestOptions) -> Self {
Self {
http: self.http.clone(),
options,
}
}
pub fn config(&self) -> Config {
self.http.config()
}
pub fn set_bearer_token(&self, token: impl Into<String>) -> &Self {
self.http.set_bearer_token(Some(token.into()));
self
}
pub fn clear_bearer_token(&self) -> &Self {
self.http.set_bearer_token(None);
self
}
pub fn set_device_token(&self, token: impl Into<String>) -> &Self {
self.http.set_device_token(Some(token.into()));
self
}
pub fn clear_device_token(&self) -> &Self {
self.http.set_device_token(None);
self
}
pub fn with_device(&self, device_token: impl Into<String>) -> Self {
let mut config = self.http.config();
config.device_token = Some(device_token.into());
Self::new(config)
}
pub async fn request(&self, method: Method, path: &str, body: impl IntoBody) -> Result<Json> {
self.http
.request_json(method, path, body.into_body(), &self.options)
.await
}
pub async fn execute(
&self,
method: Method,
path: &str,
body: impl IntoBody,
) -> Result<ResponseData> {
self.http
.execute(method, path, body.into_body(), &self.options)
.await
}
pub async fn download(&self, path: &str) -> Result<Vec<u8>> {
self.http
.bytes(Method::Get, path, None, &self.options)
.await
}
}
macro_rules! client_services {
(
$(
$(#[$meta:meta])*
$method:ident -> $service:ident
),* $(,)?
) => {
impl ApiBrasil {
$(
$(#[$meta])*
pub fn $method(&self) -> $service {
$service::new(self.http.clone()).with_options(self.options.clone())
}
)*
}
};
}
client_services! {
auth -> AuthService,
devices -> DevicesService,
whatsapp -> WhatsAppService,
evolution -> EvolutionService,
whatsmeow -> WhatsMeowService,
sms -> SmsService,
dados -> DadosService,
vehicles -> VehiclesService,
fipe -> FipeService,
correios -> CorreiosService,
cep -> CepService,
geolocation -> GeolocationService,
geomatrix -> GeomatrixService,
recognize -> RecognizeService,
ddd -> DddService,
holidays -> HolidaysService,
translate -> TranslateService,
weather -> WeatherService,
loterias -> LoteriasService,
database_ip -> DatabaseIpService,
consulta -> ConsultaService,
ura -> UraService,
chip_virtual -> ChipVirtualService,
bulk -> BulkService,
catalog -> CatalogService,
account -> AccountService,
payments -> PaymentsService,
ip_whitelist -> IpWhitelistService,
bearer_rate_limit -> BearerRateLimitService,
reports -> ReportsService,
}
pub async fn login(credentials: impl IntoBody, config: Config) -> Result<(ApiBrasil, Json)> {
let api = ApiBrasil::new(config);
let session = api.auth().login(credentials).await?;
if requires_2fa(&session) {
return Err(Error::authentication(
"Esta conta exige autenticação em dois fatores. Use auth().login() + auth().verify_2fa().",
)
.with_response(session));
}
Ok((api, session))
}