Class: Smartbill::Sdk::Client
- Inherits:
-
Object
- Object
- Smartbill::Sdk::Client
- Defined in:
- lib/smartbill/sdk/client.rb
Overview
Synchronous client for the SmartBill Cloud REST API.
Usage:
client = Smartbill::Sdk::Client.new(username: "you@example.com", token: "...")
resp = client.invoices.create(invoice)
puts resp.series, resp.number
client.close
Or with a block (closes automatically):
Smartbill::Sdk::Client.new(username: "...", token: "...").with_client do |client|
client.invoices.create(invoice)
end
Instance Attribute Summary collapse
-
#auth_header ⇒ Object
readonly
Returns the value of attribute auth_header.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#estimates ⇒ Object
readonly
Returns the value of attribute estimates.
-
#invoices ⇒ Object
readonly
Returns the value of attribute invoices.
-
#payments ⇒ Object
readonly
Returns the value of attribute payments.
-
#series ⇒ Object
readonly
Returns the value of attribute series.
-
#stocks ⇒ Object
readonly
Returns the value of attribute stocks.
-
#taxes ⇒ Object
readonly
Returns the value of attribute taxes.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#close ⇒ Object
Close the underlying HTTP adapter.
-
#execute(request, binary: false) ⇒ Object
Send a Transport::Request and return the parsed payload.
-
#initialize(username:, token:, base_url: Transport::DEFAULT_BASE_URL, timeout: Transport::DEFAULT_TIMEOUT, enforce_rate_limit: false, http: nil) ⇒ Client
constructor
A new instance of Client.
-
#with_client ⇒ Object
Yield self to a block and ensure
#closeis called afterwards.
Constructor Details
#initialize(username:, token:, base_url: Transport::DEFAULT_BASE_URL, timeout: Transport::DEFAULT_TIMEOUT, enforce_rate_limit: false, http: nil) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/smartbill/sdk/client.rb', line 23 def initialize(username:, token:, base_url: Transport::DEFAULT_BASE_URL, timeout: Transport::DEFAULT_TIMEOUT, enforce_rate_limit: false, http: nil) @username = username @token = token @base_url = base_url @auth_header = Transport.build_auth(username, token) @rate_limiter = enforce_rate_limit ? Transport::RateLimiter.new : nil @http = http || NetHttpAdapter.new(timeout: timeout) @invoices = Services::InvoicesService.new(self) @estimates = Services::EstimatesService.new(self) @payments = Services::PaymentsService.new(self) @email = Services::EmailService.new(self) @taxes = Services::ConfigurationService.new(self) @series = @taxes # convenience alias: taxes + series share one service @stocks = Services::StocksService.new(self) end |
Instance Attribute Details
#auth_header ⇒ Object (readonly)
Returns the value of attribute auth_header.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def auth_header @auth_header end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def base_url @base_url end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def email @email end |
#estimates ⇒ Object (readonly)
Returns the value of attribute estimates.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def estimates @estimates end |
#invoices ⇒ Object (readonly)
Returns the value of attribute invoices.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def invoices @invoices end |
#payments ⇒ Object (readonly)
Returns the value of attribute payments.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def payments @payments end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def series @series end |
#stocks ⇒ Object (readonly)
Returns the value of attribute stocks.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def stocks @stocks end |
#taxes ⇒ Object (readonly)
Returns the value of attribute taxes.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def taxes @taxes end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def token @token end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
20 21 22 |
# File 'lib/smartbill/sdk/client.rb', line 20 def username @username end |
Instance Method Details
#close ⇒ Object
Close the underlying HTTP adapter. A no-op for the default Net::HTTP adapter (which opens a fresh connection per request).
58 |
# File 'lib/smartbill/sdk/client.rb', line 58 def close; end |
#execute(request, binary: false) ⇒ Object
Send a Transport::Request and return the parsed payload. When binary is true, the raw body String is returned.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/smartbill/sdk/client.rb', line 43 def execute(request, binary: false) @rate_limiter&.acquire response = begin @http.call(request) rescue Error, AuthError, APIError, RateLimitError, ValidationError => e raise e rescue StandardError => e raise TransportError, "Transport error: #{e.}" end @rate_limiter&.notify_403 if response.status == 403 && @rate_limiter Transport.handle_response(response, binary: binary) end |
#with_client ⇒ Object
Yield self to a block and ensure #close is called afterwards.
61 62 63 64 65 |
# File 'lib/smartbill/sdk/client.rb', line 61 def with_client yield self ensure close end |