Class: ApiBrasil::Client
- Inherits:
-
Object
- Object
- ApiBrasil::Client
- Defined in:
- lib/api_brasil/client.rb
Overview
Cliente oficial da plataforma APIBrasil.
api = ApiBrasil.new(
bearer_token: ENV["APIBRASIL_BEARER_TOKEN"],
device_token: ENV["APIBRASIL_DEVICE_TOKEN"]
)
api.whatsapp.send_text("number" => "5511999999999", "text" => "Ola!")
empresa = api.consulta.cnpj("cnpj" => "00000000000000")
Credenciais nao informadas sao lidas das variaveis de ambiente
APIBRASIL_BEARER_TOKEN, APIBRASIL_DEVICE_TOKEN,
APIBRASIL_SECRET_KEY e APIBRASIL_BASE_URL.
Instance Attribute Summary collapse
-
#account ⇒ Services::Platform::AccountService
readonly
Saldo, faturas, notificacoes, tickets.
-
#auth ⇒ Services::Platform::AuthService
readonly
Login, 2FA, cadastro, senha e perfil.
-
#bearer_rate_limit ⇒ Services::Platform::BearerRateLimitService
readonly
Rate limit por Bearer Token.
-
#bulk ⇒ Services::Data::BulkService
readonly
Execucao em lote.
-
#catalog ⇒ Services::Platform::CatalogService
readonly
Catalogo de APIs, planos, docs e servidores.
-
#cep ⇒ Services::Data::CepService
readonly
CEP + geolocalizacao.
-
#chip_virtual ⇒ Services::Data::ChipVirtualService
readonly
Chip virtual.
-
#consulta ⇒ Services::Data::ConsultaService
readonly
Consultas por credito.
-
#correios ⇒ Services::Data::CorreiosService
readonly
Correios.
-
#dados ⇒ Services::Data::DadosService
readonly
Dados cadastrais device-based.
-
#database_ip ⇒ Services::Data::DatabaseIpService
readonly
GeoIP (
/database/ip). -
#ddd ⇒ Services::Data::DddService
readonly
DDD.
-
#devices ⇒ Services::Platform::DevicesService
readonly
Gestao de devices.
-
#evolution ⇒ Services::Messaging::EvolutionService
readonly
Evolution API (
/evolution/{controller}/{action}). -
#fipe ⇒ Services::Data::FipeService
readonly
Tabela FIPE.
-
#geolocation ⇒ Services::Data::GeolocationService
readonly
Geolocalizacao.
-
#geomatrix ⇒ Services::Data::GeomatrixService
readonly
Matriz de distancias.
-
#holidays ⇒ Services::Data::HolidaysService
readonly
Feriados.
-
#http ⇒ Core::HttpClient
readonly
Cliente HTTP interno (headers, retry, hooks, erros).
-
#ip_whitelist ⇒ Services::Platform::IpWhitelistService
readonly
IP whitelist da conta.
-
#loterias ⇒ Services::Data::LoteriasService
readonly
Loterias.
-
#payments ⇒ Services::Platform::PaymentsService
readonly
Recargas e pagamentos (PIX, boleto, cartao).
-
#recognize ⇒ Services::Data::RecognizeService
readonly
OCR / Google Vision.
-
#reports ⇒ Services::Platform::ReportsService
readonly
Relatorios e dashboard de consumo.
-
#sms ⇒ Services::Messaging::SmsService
readonly
SMS (
/sms/{action}e/sms/send/credits). -
#translate ⇒ Services::Data::TranslateService
readonly
Traducao.
-
#ura ⇒ Services::Data::UraService
readonly
URA reversa / ligacoes.
-
#vehicles ⇒ Services::Data::VehiclesService
readonly
Veiculos por placa.
-
#weather ⇒ Services::Data::WeatherService
readonly
Clima.
-
#whatsapp ⇒ Services::Messaging::WhatsAppService
readonly
WhatsApp device-based (
/whatsapp/{action}). -
#whatsmeow ⇒ Services::Messaging::WhatsMeowService
readonly
WhatsMeow (
/whatsmeow/{action}).
Class Method Summary collapse
-
.login(credentials, config = {}) ⇒ Hash
Faz login e retorna um cliente ja autenticado.
Instance Method Summary collapse
- #bearer_token ⇒ String?
- #bearer_token=(token) ⇒ Object
- #device_token ⇒ String?
- #device_token=(token) ⇒ Object
-
#initialize(config = {}) ⇒ Client
constructor
A new instance of Client.
-
#request(method, path, body = nil, options = {}) ⇒ Object
Porta de saida generica: chama qualquer endpoint do gateway com os headers de autenticacao ja configurados.
-
#set_bearer_token(token) ⇒ self
Define/atualiza o Bearer Token do cliente.
-
#set_device_token(token) ⇒ self
Define/atualiza o DeviceToken do cliente.
-
#with_device(device_token) ⇒ Client
Retorna um novo cliente com as mesmas credenciais, mas apontando para outro device - util para gerenciar varios numeros/instancias.
Constructor Details
#initialize(config = {}) ⇒ Client
Returns a new instance of Client.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/api_brasil/client.rb', line 139 def initialize(config = {}) @http = Core::HttpClient.new(config) @auth = Services::Platform::AuthService.new(@http) @devices = Services::Platform::DevicesService.new(@http) @whatsapp = Services::Messaging::WhatsAppService.new(@http) @evolution = Services::Messaging::EvolutionService.new(@http) @whatsmeow = Services::Messaging::WhatsMeowService.new(@http) @sms = Services::Messaging::SmsService.new(@http) @dados = Services::Data::DadosService.new(@http) @vehicles = Services::Data::VehiclesService.new(@http) @fipe = Services::Data::FipeService.new(@http) @correios = Services::Data::CorreiosService.new(@http) @cep = Services::Data::CepService.new(@http) @geolocation = Services::Data::GeolocationService.new(@http) @geomatrix = Services::Data::GeomatrixService.new(@http) @recognize = Services::Data::RecognizeService.new(@http) @ddd = Services::Data::DddService.new(@http) @holidays = Services::Data::HolidaysService.new(@http) @translate = Services::Data::TranslateService.new(@http) @weather = Services::Data::WeatherService.new(@http) @loterias = Services::Data::LoteriasService.new(@http) @database_ip = Services::Data::DatabaseIpService.new(@http) @consulta = Services::Data::ConsultaService.new(@http) @ura = Services::Data::UraService.new(@http) @chip_virtual = Services::Data::ChipVirtualService.new(@http) @bulk = Services::Data::BulkService.new(@http) @catalog = Services::Platform::CatalogService.new(@http) @account = Services::Platform::AccountService.new(@http) @payments = Services::Platform::PaymentsService.new(@http) @ip_whitelist = Services::Platform::IpWhitelistService.new(@http) @bearer_rate_limit = Services::Platform::BearerRateLimitService.new(@http) @reports = Services::Platform::ReportsService.new(@http) end |
Instance Attribute Details
#account ⇒ Services::Platform::AccountService (readonly)
Returns saldo, faturas, notificacoes, tickets.
124 125 126 |
# File 'lib/api_brasil/client.rb', line 124 def account @account end |
#auth ⇒ Services::Platform::AuthService (readonly)
Returns login, 2FA, cadastro, senha e perfil.
49 50 51 |
# File 'lib/api_brasil/client.rb', line 49 def auth @auth end |
#bearer_rate_limit ⇒ Services::Platform::BearerRateLimitService (readonly)
Returns rate limit por Bearer Token.
133 134 135 |
# File 'lib/api_brasil/client.rb', line 133 def bearer_rate_limit @bearer_rate_limit end |
#bulk ⇒ Services::Data::BulkService (readonly)
Returns execucao em lote.
118 119 120 |
# File 'lib/api_brasil/client.rb', line 118 def bulk @bulk end |
#catalog ⇒ Services::Platform::CatalogService (readonly)
Returns catalogo de APIs, planos, docs e servidores.
121 122 123 |
# File 'lib/api_brasil/client.rb', line 121 def catalog @catalog end |
#cep ⇒ Services::Data::CepService (readonly)
Returns CEP + geolocalizacao.
79 80 81 |
# File 'lib/api_brasil/client.rb', line 79 def cep @cep end |
#chip_virtual ⇒ Services::Data::ChipVirtualService (readonly)
Returns chip virtual.
115 116 117 |
# File 'lib/api_brasil/client.rb', line 115 def chip_virtual @chip_virtual end |
#consulta ⇒ Services::Data::ConsultaService (readonly)
Returns consultas por credito.
109 110 111 |
# File 'lib/api_brasil/client.rb', line 109 def consulta @consulta end |
#correios ⇒ Services::Data::CorreiosService (readonly)
Returns Correios.
76 77 78 |
# File 'lib/api_brasil/client.rb', line 76 def correios @correios end |
#dados ⇒ Services::Data::DadosService (readonly)
Returns dados cadastrais device-based.
67 68 69 |
# File 'lib/api_brasil/client.rb', line 67 def dados @dados end |
#database_ip ⇒ Services::Data::DatabaseIpService (readonly)
Returns GeoIP (/database/ip).
106 107 108 |
# File 'lib/api_brasil/client.rb', line 106 def database_ip @database_ip end |
#ddd ⇒ Services::Data::DddService (readonly)
Returns DDD.
91 92 93 |
# File 'lib/api_brasil/client.rb', line 91 def ddd @ddd end |
#devices ⇒ Services::Platform::DevicesService (readonly)
Returns gestao de devices.
52 53 54 |
# File 'lib/api_brasil/client.rb', line 52 def devices @devices end |
#evolution ⇒ Services::Messaging::EvolutionService (readonly)
Returns Evolution API (/evolution/{controller}/{action}).
58 59 60 |
# File 'lib/api_brasil/client.rb', line 58 def evolution @evolution end |
#fipe ⇒ Services::Data::FipeService (readonly)
Returns tabela FIPE.
73 74 75 |
# File 'lib/api_brasil/client.rb', line 73 def fipe @fipe end |
#geolocation ⇒ Services::Data::GeolocationService (readonly)
Returns geolocalizacao.
82 83 84 |
# File 'lib/api_brasil/client.rb', line 82 def geolocation @geolocation end |
#geomatrix ⇒ Services::Data::GeomatrixService (readonly)
Returns matriz de distancias.
85 86 87 |
# File 'lib/api_brasil/client.rb', line 85 def geomatrix @geomatrix end |
#holidays ⇒ Services::Data::HolidaysService (readonly)
Returns feriados.
94 95 96 |
# File 'lib/api_brasil/client.rb', line 94 def holidays @holidays end |
#http ⇒ Core::HttpClient (readonly)
Returns cliente HTTP interno (headers, retry, hooks, erros).
46 47 48 |
# File 'lib/api_brasil/client.rb', line 46 def http @http end |
#ip_whitelist ⇒ Services::Platform::IpWhitelistService (readonly)
Returns IP whitelist da conta.
130 131 132 |
# File 'lib/api_brasil/client.rb', line 130 def ip_whitelist @ip_whitelist end |
#loterias ⇒ Services::Data::LoteriasService (readonly)
Returns loterias.
103 104 105 |
# File 'lib/api_brasil/client.rb', line 103 def loterias @loterias end |
#payments ⇒ Services::Platform::PaymentsService (readonly)
Returns recargas e pagamentos (PIX, boleto, cartao).
127 128 129 |
# File 'lib/api_brasil/client.rb', line 127 def payments @payments end |
#recognize ⇒ Services::Data::RecognizeService (readonly)
Returns OCR / Google Vision.
88 89 90 |
# File 'lib/api_brasil/client.rb', line 88 def recognize @recognize end |
#reports ⇒ Services::Platform::ReportsService (readonly)
Returns relatorios e dashboard de consumo.
136 137 138 |
# File 'lib/api_brasil/client.rb', line 136 def reports @reports end |
#sms ⇒ Services::Messaging::SmsService (readonly)
Returns SMS (/sms/{action} e /sms/send/credits).
64 65 66 |
# File 'lib/api_brasil/client.rb', line 64 def sms @sms end |
#translate ⇒ Services::Data::TranslateService (readonly)
Returns traducao.
97 98 99 |
# File 'lib/api_brasil/client.rb', line 97 def translate @translate end |
#ura ⇒ Services::Data::UraService (readonly)
Returns URA reversa / ligacoes.
112 113 114 |
# File 'lib/api_brasil/client.rb', line 112 def ura @ura end |
#vehicles ⇒ Services::Data::VehiclesService (readonly)
Returns veiculos por placa.
70 71 72 |
# File 'lib/api_brasil/client.rb', line 70 def vehicles @vehicles end |
#weather ⇒ Services::Data::WeatherService (readonly)
Returns clima.
100 101 102 |
# File 'lib/api_brasil/client.rb', line 100 def weather @weather end |
#whatsapp ⇒ Services::Messaging::WhatsAppService (readonly)
Returns WhatsApp device-based (/whatsapp/{action}).
55 56 57 |
# File 'lib/api_brasil/client.rb', line 55 def whatsapp @whatsapp end |
#whatsmeow ⇒ Services::Messaging::WhatsMeowService (readonly)
Returns WhatsMeow (/whatsmeow/{action}).
61 62 63 |
# File 'lib/api_brasil/client.rb', line 61 def whatsmeow @whatsmeow end |
Class Method Details
.login(credentials, config = {}) ⇒ Hash
Faz login e retorna um cliente ja autenticado.
Levanta ApiBrasilError se a conta exigir 2FA - nesse caso crie o
cliente manualmente e use auth.login + auth.verify_2fa.
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/api_brasil/client.rb', line 185 def self.login(credentials, config = {}) client = new(config) session = client.auth.login(credentials) if session.is_a?(Hash) && session["requires_2fa"] raise Core::Errors::ApiBrasilError.new( "Esta conta exige autenticacao em dois fatores. Use auth.login + auth.verify_2fa.", response: session ) end { client: client, session: session } end |
Instance Method Details
#bearer_token ⇒ String?
216 217 218 |
# File 'lib/api_brasil/client.rb', line 216 def bearer_token http.bearer_token end |
#bearer_token=(token) ⇒ Object
220 221 222 |
# File 'lib/api_brasil/client.rb', line 220 def bearer_token=(token) http.bearer_token = token end |
#device_token ⇒ String?
225 226 227 |
# File 'lib/api_brasil/client.rb', line 225 def device_token http.device_token end |
#device_token=(token) ⇒ Object
229 230 231 |
# File 'lib/api_brasil/client.rb', line 229 def device_token=(token) http.device_token = token end |
#request(method, path, body = nil, options = {}) ⇒ Object
Porta de saida generica: chama qualquer endpoint do gateway com os headers de autenticacao ja configurados. Use para rotas que ainda nao tem metodo dedicado na SDK.
api.request("POST", "/consulta/cpf/credits", "cpf" => "...")
248 249 250 |
# File 'lib/api_brasil/client.rb', line 248 def request(method, path, body = nil, = {}) http.request(method, path, body, ) end |
#set_bearer_token(token) ⇒ self
Define/atualiza o Bearer Token do cliente.
202 203 204 205 |
# File 'lib/api_brasil/client.rb', line 202 def set_bearer_token(token) http.bearer_token = token self end |
#set_device_token(token) ⇒ self
Define/atualiza o DeviceToken do cliente.
210 211 212 213 |
# File 'lib/api_brasil/client.rb', line 210 def set_device_token(token) http.device_token = token self end |
#with_device(device_token) ⇒ Client
Retorna um novo cliente com as mesmas credenciais, mas apontando para outro device - util para gerenciar varios numeros/instancias.
237 238 239 |
# File 'lib/api_brasil/client.rb', line 237 def with_device(device_token) self.class.new(http.to_config.merge(device_token: device_token)) end |