Class: TerminalShop::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/terminal_shop/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
ENVIRONMENTS =

rubocop:disable Style/MutableConstant

{production: "https://api.terminal.shop", dev: "https://api.dev.terminal.shop"}

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(bearer_token: ENV["TERMINAL_BEARER_TOKEN"], app_id: nil, environment: nil, base_url: ENV["TERMINAL_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.

Each environment maps to a different base URL:

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

Parameters:

  • bearer_token (String, nil) (defaults to: ENV["TERMINAL_BEARER_TOKEN"])

    Defaults to ‘ENV`

  • app_id (String, nil) (defaults to: nil)
  • environment (:production, :dev, nil) (defaults to: nil)

    Specifies the environment to use for the API.

  • base_url (String, nil) (defaults to: ENV["TERMINAL_BASE_URL"])

    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)


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
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/terminal_shop/client.rb', line 94

def initialize(
  bearer_token: ENV["TERMINAL_BEARER_TOKEN"],
  app_id: nil,
  environment: nil,
  base_url: ENV["TERMINAL_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 ||= TerminalShop::Client::ENVIRONMENTS.fetch(environment&.to_sym || :production) do
    message = "environment must be one of #{TerminalShop::Client::ENVIRONMENTS.keys}, got #{environment}"
    raise ArgumentError.new(message)
  end

  if bearer_token.nil?
    raise ArgumentError.new("bearer_token is required, and can be set via environ: \"TERMINAL_BEARER_TOKEN\"")
  end

  headers = {
    "x-terminal-app-id" => (@app_id = app_id&.to_s)
  }

  @bearer_token = bearer_token.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,
    headers: headers
  )

  @product = TerminalShop::Resources::Product.new(client: self)
  @profile = TerminalShop::Resources::Profile.new(client: self)
  @address = TerminalShop::Resources::Address.new(client: self)
  @card = TerminalShop::Resources::Card.new(client: self)
  @cart = TerminalShop::Resources::Cart.new(client: self)
  @order = TerminalShop::Resources::Order.new(client: self)
  @subscription = TerminalShop::Resources::Subscription.new(client: self)
  @token = TerminalShop::Resources::Token.new(client: self)
  @app = TerminalShop::Resources::App.new(client: self)
  @email = TerminalShop::Resources::Email.new(client: self)
  @view = TerminalShop::Resources::View.new(client: self)
end

Instance Attribute Details

#addressTerminalShop::Resources::Address (readonly)



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

def address
  @address
end

#appTerminalShop::Resources::App (readonly)



54
55
56
# File 'lib/terminal_shop/client.rb', line 54

def app
  @app
end

#app_idString? (readonly)

Returns:

  • (String, nil)


27
28
29
# File 'lib/terminal_shop/client.rb', line 27

def app_id
  @app_id
end

#bearer_tokenString (readonly)

Returns:

  • (String)


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

def bearer_token
  @bearer_token
end

#cardTerminalShop::Resources::Card (readonly)



39
40
41
# File 'lib/terminal_shop/client.rb', line 39

def card
  @card
end

#cartTerminalShop::Resources::Cart (readonly)



42
43
44
# File 'lib/terminal_shop/client.rb', line 42

def cart
  @cart
end

#emailTerminalShop::Resources::Email (readonly)



57
58
59
# File 'lib/terminal_shop/client.rb', line 57

def email
  @email
end

#orderTerminalShop::Resources::Order (readonly)



45
46
47
# File 'lib/terminal_shop/client.rb', line 45

def order
  @order
end

#productTerminalShop::Resources::Product (readonly)



30
31
32
# File 'lib/terminal_shop/client.rb', line 30

def product
  @product
end

#profileTerminalShop::Resources::Profile (readonly)



33
34
35
# File 'lib/terminal_shop/client.rb', line 33

def profile
  @profile
end

#subscriptionTerminalShop::Resources::Subscription (readonly)



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

def subscription
  @subscription
end

#tokenTerminalShop::Resources::Token (readonly)



51
52
53
# File 'lib/terminal_shop/client.rb', line 51

def token
  @token
end

#viewTerminalShop::Resources::View (readonly)



60
61
62
# File 'lib/terminal_shop/client.rb', line 60

def view
  @view
end