Class: SurgeAPI::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/surge_api/client.rb,
sig/surge_api/client.rbs

Constant Summary collapse

DEFAULT_MAX_RETRIES =

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

Returns:

  • (2)
2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

Returns:

  • (Float)
60.0
DEFAULT_INITIAL_RETRY_DELAY =

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

Returns:

  • (Float)
0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

Returns:

  • (Float)
8.0

Constants inherited from Internal::Transport::BaseClient

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

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(api_key: ENV["SURGE_API_KEY"], webhook_signing_secret: ENV["SURGE_WEBHOOK_SIGNING_SECRET"], base_url: ENV["SURGE_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.

"https://api.example.com/v2/". Defaults to ENV["SURGE_BASE_URL"]

Parameters:

  • api_key (String, nil) (defaults to: ENV["SURGE_API_KEY"])

    Defaults to ENV["SURGE_API_KEY"]

  • webhook_signing_secret (String, nil) (defaults to: ENV["SURGE_WEBHOOK_SIGNING_SECRET"])

    Defaults to ENV["SURGE_WEBHOOK_SIGNING_SECRET"]

  • base_url (String, nil) (defaults to: ENV["SURGE_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)


85
86
87
88
89
90
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
128
129
130
131
132
133
134
135
136
137
# File 'lib/surge_api/client.rb', line 85

def initialize(
  api_key: ENV["SURGE_API_KEY"],
  webhook_signing_secret: ENV["SURGE_WEBHOOK_SIGNING_SECRET"],
  base_url: ENV["SURGE_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.surge.app"

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

  headers = {}
  custom_headers_env = ENV["SURGE_CUSTOM_HEADERS"]
  unless custom_headers_env.nil?
    parsed = {}
    custom_headers_env.split("\n").each do |line|
      colon = line.index(":")
      unless colon.nil?
        parsed[line[0...colon].strip] = line[(colon + 1)..].strip
      end
    end
    headers = parsed.merge(headers)
  end

  @api_key = api_key.to_s
  @webhook_signing_secret = webhook_signing_secret&.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
  )

  @accounts = SurgeAPI::Resources::Accounts.new(client: self)
  @attachments = SurgeAPI::Resources::Attachments.new(client: self)
  @audiences = SurgeAPI::Resources::Audiences.new(client: self)
  @blasts = SurgeAPI::Resources::Blasts.new(client: self)
  @campaigns = SurgeAPI::Resources::Campaigns.new(client: self)
  @contacts = SurgeAPI::Resources::Contacts.new(client: self)
  @messages = SurgeAPI::Resources::Messages.new(client: self)
  @phone_numbers = SurgeAPI::Resources::PhoneNumbers.new(client: self)
  @recordings = SurgeAPI::Resources::Recordings.new(client: self)
  @users = SurgeAPI::Resources::Users.new(client: self)
  @verifications = SurgeAPI::Resources::Verifications.new(client: self)
  @webhooks = SurgeAPI::Resources::Webhooks.new(client: self)
end

Instance Attribute Details

#accountsSurgeAPI::Resources::Accounts (readonly)



25
26
27
# File 'lib/surge_api/client.rb', line 25

def accounts
  @accounts
end

#api_keyString (readonly)

Returns:

  • (String)


19
20
21
# File 'lib/surge_api/client.rb', line 19

def api_key
  @api_key
end

#attachmentsSurgeAPI::Resources::Attachments (readonly)



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

def attachments
  @attachments
end

#audiencesSurgeAPI::Resources::Audiences (readonly)



31
32
33
# File 'lib/surge_api/client.rb', line 31

def audiences
  @audiences
end

#blastsSurgeAPI::Resources::Blasts (readonly)



34
35
36
# File 'lib/surge_api/client.rb', line 34

def blasts
  @blasts
end

#campaignsSurgeAPI::Resources::Campaigns (readonly)



37
38
39
# File 'lib/surge_api/client.rb', line 37

def campaigns
  @campaigns
end

#contactsSurgeAPI::Resources::Contacts (readonly)



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

def contacts
  @contacts
end

#messagesSurgeAPI::Resources::Messages (readonly)



43
44
45
# File 'lib/surge_api/client.rb', line 43

def messages
  @messages
end

#phone_numbersSurgeAPI::Resources::PhoneNumbers (readonly)



46
47
48
# File 'lib/surge_api/client.rb', line 46

def phone_numbers
  @phone_numbers
end

#recordingsSurgeAPI::Resources::Recordings (readonly)



49
50
51
# File 'lib/surge_api/client.rb', line 49

def recordings
  @recordings
end

#usersSurgeAPI::Resources::Users (readonly)



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

def users
  @users
end

#verificationsSurgeAPI::Resources::Verifications (readonly)



55
56
57
# File 'lib/surge_api/client.rb', line 55

def verifications
  @verifications
end

#webhook_signing_secretString? (readonly)

Returns:

  • (String, nil)


22
23
24
# File 'lib/surge_api/client.rb', line 22

def webhook_signing_secret
  @webhook_signing_secret
end

#webhooksSurgeAPI::Resources::Webhooks (readonly)



58
59
60
# File 'lib/surge_api/client.rb', line 58

def webhooks
  @webhooks
end