Class: Knockapi::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/knockapi/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(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.

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

Parameters:

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

    Defaults to ‘ENV`

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

    The slug of an existing branch Defaults to ‘ENV`

  • 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")


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
# File 'lib/knockapi/client.rb', line 88

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)
  }

  @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)
  @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)



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

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)



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

def bulk_operations
  @bulk_operations
end

#channelsKnockapi::Resources::Channels (readonly)



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

def channels
  @channels
end

#integrationsKnockapi::Resources::Integrations (readonly)



47
48
49
# File 'lib/knockapi/client.rb', line 47

def integrations
  @integrations
end

#messagesKnockapi::Resources::Messages (readonly)



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

def messages
  @messages
end

#objectsKnockapi::Resources::Objects (readonly)



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

def objects
  @objects
end

#providersKnockapi::Resources::Providers (readonly)



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

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)



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

def schedules
  @schedules
end

#tenantsKnockapi::Resources::Tenants (readonly)



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

def tenants
  @tenants
end

#usersKnockapi::Resources::Users (readonly)



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

def users
  @users
end

#workflowsKnockapi::Resources::Workflows (readonly)



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

def workflows
  @workflows
end