Class: Courier::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/courier/client.rb,
sig/courier/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::Courier, 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(api_key: ENV["COURIER_API_KEY"], base_url: ENV["COURIER_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["COURIER_BASE_URL"]

Parameters:

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

    Defaults to ENV["COURIER_API_KEY"]

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


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/courier/client.rb', line 145

def initialize(
  api_key: ENV["COURIER_API_KEY"],
  base_url: ENV["COURIER_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.courier.com"

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

  headers = {}
  custom_headers_env = ENV["COURIER_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

  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
  )

  @send_ = Courier::Resources::Send.new(client: self)
  @audiences = Courier::Resources::Audiences.new(client: self)
  @providers = Courier::Resources::Providers.new(client: self)
  @audit_events = Courier::Resources::AuditEvents.new(client: self)
  @auth = Courier::Resources::Auth.new(client: self)
  @automations = Courier::Resources::Automations.new(client: self)
  @journeys = Courier::Resources::Journeys.new(client: self)
  @broadcasts = Courier::Resources::Broadcasts.new(client: self)
  @brands = Courier::Resources::Brands.new(client: self)
  @digests = Courier::Resources::Digests.new(client: self)
  @inbound = Courier::Resources::Inbound.new(client: self)
  @lists = Courier::Resources::Lists.new(client: self)
  @inbox = Courier::Resources::Inbox.new(client: self)
  @messages = Courier::Resources::Messages.new(client: self)
  @requests = Courier::Resources::Requests.new(client: self)
  @notifications = Courier::Resources::Notifications.new(client: self)
  @routing_strategies = Courier::Resources::RoutingStrategies.new(client: self)
  @workspace_preferences = Courier::Resources::WorkspacePreferences.new(client: self)
  @profiles = Courier::Resources::Profiles.new(client: self)
  @tenants = Courier::Resources::Tenants.new(client: self)
  @translations = Courier::Resources::Translations.new(client: self)
  @users = Courier::Resources::Users.new(client: self)
end

Instance Attribute Details

#api_keyString (readonly)

Returns:

  • (String)


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

def api_key
  @api_key
end

#audiencesCourier::Resources::Audiences (readonly)

Define filter-based groups whose membership Courier recalculates as user profiles change.



29
30
31
# File 'lib/courier/client.rb', line 29

def audiences
  @audiences
end

#audit_eventsCourier::Resources::AuditEvents (readonly)

Read the audit trail of configuration and access changes in your workspace.



38
39
40
# File 'lib/courier/client.rb', line 38

def audit_events
  @audit_events
end

#authCourier::Resources::Auth (readonly)

Issue scoped, short-lived JWTs so client-side SDKs — Inbox, Preferences, and the embedded designer — can call Courier as a single user. Server-side requests authenticate with your workspace API key instead.



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

def auth
  @auth
end

#automationsCourier::Resources::Automations (readonly)

Invoke a stored automation template or an ad hoc automation defined in the request.



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

def automations
  @automations
end

#brandsCourier::Resources::Brands (readonly)

Manage the logos, colors, and layout that give the templates you send a consistent look.



62
63
64
# File 'lib/courier/client.rb', line 62

def brands
  @brands
end

#broadcastsCourier::Resources::Broadcasts (readonly)



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

def broadcasts
  @broadcasts
end

#digestsCourier::Resources::Digests (readonly)



65
66
67
# File 'lib/courier/client.rb', line 65

def digests
  @digests
end

#inboundCourier::Resources::Inbound (readonly)

Record an inbound event that triggers the journeys and automations mapped to it.



69
70
71
# File 'lib/courier/client.rb', line 69

def inbound
  @inbound
end

#inboxCourier::Resources::Inbox (readonly)



77
78
79
# File 'lib/courier/client.rb', line 77

def inbox
  @inbox
end

#journeysCourier::Resources::Journeys (readonly)

Build, version, publish, invoke, and cancel multi-step notification workflows, along with the templates scoped to them.



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

def journeys
  @journeys
end

#listsCourier::Resources::Lists (readonly)

Manage static groups of users that you subscribe explicitly, and send to them by list id or list pattern.



74
75
76
# File 'lib/courier/client.rb', line 74

def lists
  @lists
end

#messagesCourier::Resources::Messages (readonly)

Look up the messages Courier has accepted, inspect their delivery history and rendered output, and cancel, resend, or archive them.



82
83
84
# File 'lib/courier/client.rb', line 82

def messages
  @messages
end

#notificationsCourier::Resources::Notifications (readonly)

Create, update, version, publish, and localize notification templates and their content.



92
93
94
# File 'lib/courier/client.rb', line 92

def notifications
  @notifications
end

#profilesCourier::Resources::Profiles (readonly)

Store the contact information Courier delivers to for each user — email, phone number, push tokens, and any custom data you send to.



107
108
109
# File 'lib/courier/client.rb', line 107

def profiles
  @profiles
end

#providersCourier::Resources::Providers (readonly)

Configure the channel providers Courier delivers through, and browse the provider types it supports.



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

def providers
  @providers
end

#requestsCourier::Resources::Requests (readonly)

Look up the messages Courier has accepted, inspect their delivery history and rendered output, and cancel, resend, or archive them.



87
88
89
# File 'lib/courier/client.rb', line 87

def requests
  @requests
end

#routing_strategiesCourier::Resources::RoutingStrategies (readonly)

Define reusable channel routing and failover strategies, and see which templates use them.



97
98
99
# File 'lib/courier/client.rb', line 97

def routing_strategies
  @routing_strategies
end

#send_Courier::Resources::Send (readonly)

Send a message to one or more recipients — users, lists, audiences, or tenants — across every channel you have configured.



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

def send_
  @send_
end

#tenantsCourier::Resources::Tenants (readonly)

Manage tenants — the organizations, teams, or accounts your users belong to — along with their users and default preferences.



112
113
114
# File 'lib/courier/client.rb', line 112

def tenants
  @tenants
end

#translationsCourier::Resources::Translations (readonly)

Store and retrieve the translation strings Courier uses to render localized template content.



117
118
119
# File 'lib/courier/client.rb', line 117

def translations
  @translations
end

#usersCourier::Resources::Users (readonly)



120
121
122
# File 'lib/courier/client.rb', line 120

def users
  @users
end

#workspace_preferencesCourier::Resources::WorkspacePreferences (readonly)

Manage the workspace catalog of subscription topics, the sections that group them, and publishing the preference page.



102
103
104
# File 'lib/courier/client.rb', line 102

def workspace_preferences
  @workspace_preferences
end