Class: Vatsense::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/vatsense/client.rb

Constant Summary collapse

DEFAULT_MAX_RETRIES =

Default max number of retries to attempt after a failed retryable request.

2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

60.0
DEFAULT_INITIAL_RETRY_DELAY =

Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.

0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

8.0

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(username: , password: , base_url: , max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with the API.

Defaults to ‘ENV`

Defaults to ‘ENV`

‘“api.example.com/v2/”`. Defaults to `ENV`

Parameters:

  • username (String, nil) (defaults to: )

    Use HTTP Basic Auth with username ‘user` and your API key as the password.

  • password (String, nil) (defaults to: )

    Use HTTP Basic Auth with username ‘user` and your API key as the password.

  • base_url (String, nil) (defaults to: )

    Override the default base URL for the API, e.g.,

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vatsense/client.rb', line 91

def initialize(
  username: ENV["VAT_SENSE_USERNAME"],
  password: ENV["VAT_SENSE_PASSWORD"],
  base_url: ENV["VAT_SENSE_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  base_url ||= "https://api.vatsense.com/1.0"

  if username.nil?
    raise ArgumentError.new("username is required, and can be set via environ: \"VAT_SENSE_USERNAME\"")
  end
  if password.nil?
    raise ArgumentError.new("password is required, and can be set via environ: \"VAT_SENSE_PASSWORD\"")
  end

  @username = username.to_s
  @password = password.to_s

  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay
  )

  @rates = Vatsense::Resources::Rates.new(client: self)
  @countries = Vatsense::Resources::Countries.new(client: self)
  @validate = Vatsense::Resources::Validate.new(client: self)
  @currency = Vatsense::Resources::Currency.new(client: self)
  @invoice = Vatsense::Resources::Invoice.new(client: self)
  @usage = Vatsense::Resources::Usage.new(client: self)
  @sandbox = Vatsense::Resources::Sandbox.new(client: self)
end

Instance Attribute Details

#countriesVatsense::Resources::Countries (readonly)

Country and province information



32
33
34
# File 'lib/vatsense/client.rb', line 32

def countries
  @countries
end

#currencyVatsense::Resources::Currency (readonly)

Currency exchange rates and conversion



40
41
42
# File 'lib/vatsense/client.rb', line 40

def currency
  @currency
end

#invoiceVatsense::Resources::Invoice (readonly)

VAT-compliant invoice management



44
45
46
# File 'lib/vatsense/client.rb', line 44

def invoice
  @invoice
end

#passwordString (readonly)

Use HTTP Basic Auth with username ‘user` and your API key as the password.

Returns:

  • (String)


24
25
26
# File 'lib/vatsense/client.rb', line 24

def password
  @password
end

#ratesVatsense::Resources::Rates (readonly)

VAT/GST rate lookups for countries worldwide



28
29
30
# File 'lib/vatsense/client.rb', line 28

def rates
  @rates
end

#sandboxVatsense::Resources::Sandbox (readonly)

Temporary sandbox API keys for testing



52
53
54
# File 'lib/vatsense/client.rb', line 52

def sandbox
  @sandbox
end

#usageVatsense::Resources::Usage (readonly)

API usage statistics



48
49
50
# File 'lib/vatsense/client.rb', line 48

def usage
  @usage
end

#usernameString (readonly)

Use HTTP Basic Auth with username ‘user` and your API key as the password.

Returns:

  • (String)


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

def username
  @username
end

#validateVatsense::Resources::Validate (readonly)

VAT and EORI number validation



36
37
38
# File 'lib/vatsense/client.rb', line 36

def validate
  @validate
end