Class: Smartbill::Sdk::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_headerObject (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_urlObject (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

#emailObject (readonly)

Returns the value of attribute email.



20
21
22
# File 'lib/smartbill/sdk/client.rb', line 20

def email
  @email
end

#estimatesObject (readonly)

Returns the value of attribute estimates.



20
21
22
# File 'lib/smartbill/sdk/client.rb', line 20

def estimates
  @estimates
end

#invoicesObject (readonly)

Returns the value of attribute invoices.



20
21
22
# File 'lib/smartbill/sdk/client.rb', line 20

def invoices
  @invoices
end

#paymentsObject (readonly)

Returns the value of attribute payments.



20
21
22
# File 'lib/smartbill/sdk/client.rb', line 20

def payments
  @payments
end

#seriesObject (readonly)

Returns the value of attribute series.



20
21
22
# File 'lib/smartbill/sdk/client.rb', line 20

def series
  @series
end

#stocksObject (readonly)

Returns the value of attribute stocks.



20
21
22
# File 'lib/smartbill/sdk/client.rb', line 20

def stocks
  @stocks
end

#taxesObject (readonly)

Returns the value of attribute taxes.



20
21
22
# File 'lib/smartbill/sdk/client.rb', line 20

def taxes
  @taxes
end

#tokenObject (readonly)

Returns the value of attribute token.



20
21
22
# File 'lib/smartbill/sdk/client.rb', line 20

def token
  @token
end

#usernameObject (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

#closeObject

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.message}"
  end
  @rate_limiter&.notify_403 if response.status == 403 && @rate_limiter
  Transport.handle_response(response, binary: binary)
end

#with_clientObject

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