Class: Knockapi::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/knockapi/client.rb,
sig/knockapi/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::Knockapi, 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["KNOCK_API_KEY"], branch: ENV["KNOCK_BRANCH"], base_url: ENV["KNOCK_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, idempotency_header: "Idempotency-Key") ⇒ Client

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

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

Parameters:

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

    Defaults to ENV["KNOCK_API_KEY"]

  • branch (String, nil) (defaults to: ENV["KNOCK_BRANCH"])

    The slug of an existing branch Defaults to ENV["KNOCK_BRANCH"]

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


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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/knockapi/client.rb', line 106

def initialize(
  api_key: ENV["KNOCK_API_KEY"],
  branch: ENV["KNOCK_BRANCH"],
  base_url: ENV["KNOCK_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,
  idempotency_header: "Idempotency-Key"
)
  base_url ||= "https://api.knock.app"

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

  headers = {
    "x-knock-branch" => (@branch = branch&.to_s)
  }
  custom_headers_env = ENV["KNOCK_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,
    idempotency_header: idempotency_header
  )

  @recipients = Knockapi::Resources::Recipients.new(client: self)
  @users = Knockapi::Resources::Users.new(client: self)
  @objects = Knockapi::Resources::Objects.new(client: self)
  @tenants = Knockapi::Resources::Tenants.new(client: self)
  @bulk_operations = Knockapi::Resources::BulkOperations.new(client: self)
  @messages = Knockapi::Resources::Messages.new(client: self)
  @providers = Knockapi::Resources::Providers.new(client: self)
  @integrations = Knockapi::Resources::Integrations.new(client: self)
  @workflows = Knockapi::Resources::Workflows.new(client: self)
  @workflow_recipient_runs = Knockapi::Resources::WorkflowRecipientRuns.new(client: self)
  @schedules = Knockapi::Resources::Schedules.new(client: self)
  @channels = Knockapi::Resources::Channels.new(client: self)
  @audiences = Knockapi::Resources::Audiences.new(client: self)
end

Instance Attribute Details

#api_keyString (readonly)

Returns:

  • (String)


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

def api_key
  @api_key
end

#audiencesKnockapi::Resources::Audiences (readonly)

An Audience is a segment of users.



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

def audiences
  @audiences
end

#branchString? (readonly)

The slug of an existing branch

Returns:

  • (String, nil)


23
24
25
# File 'lib/knockapi/client.rb', line 23

def branch
  @branch
end

#bulk_operationsKnockapi::Resources::BulkOperations (readonly)

A bulk operation is a set of changes applied across zero or more records triggered via a call to the Knock API and performed asynchronously.



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

def bulk_operations
  @bulk_operations
end

#channelsKnockapi::Resources::Channels (readonly)



73
74
75
# File 'lib/knockapi/client.rb', line 73

def channels
  @channels
end

#integrationsKnockapi::Resources::Integrations (readonly)



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

def integrations
  @integrations
end

#messagesKnockapi::Resources::Messages (readonly)

A message sent to a single recipient on a channel.



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

def messages
  @messages
end

#objectsKnockapi::Resources::Objects (readonly)

An object represents a resource in your system that you want to map into Knock.



35
36
37
# File 'lib/knockapi/client.rb', line 35

def objects
  @objects
end

#providersKnockapi::Resources::Providers (readonly)



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

def providers
  @providers
end

#recipientsKnockapi::Resources::Recipients (readonly)



26
27
28
# File 'lib/knockapi/client.rb', line 26

def recipients
  @recipients
end

#schedulesKnockapi::Resources::Schedules (readonly)

A schedule is a per-recipient, timezone-aware configuration for when to invoke a workflow.



70
71
72
# File 'lib/knockapi/client.rb', line 70

def schedules
  @schedules
end

#tenantsKnockapi::Resources::Tenants (readonly)

A tenant represents a top-level entity from your system, like a company, organization, account, or workspace.



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

def tenants
  @tenants
end

#usersKnockapi::Resources::Users (readonly)

A user is an individual from your system, represented in Knock. They are most commonly a recipient of a notification.



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

def users
  @users
end

#workflow_recipient_runsKnockapi::Resources::WorkflowRecipientRuns (readonly)

A workflow run represents an individual execution of a workflow for a specific recipient.



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

def workflow_recipient_runs
  @workflow_recipient_runs
end

#workflowsKnockapi::Resources::Workflows (readonly)

A workflow is a structured set of steps that is triggered to produce notifications sent over channels.



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

def workflows
  @workflows
end