Class: WhopSDK::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/whop_sdk/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["WHOP_API_KEY"], app_id: ENV["WHOP_APP_ID"], base_url: ENV["WHOP_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.

ENV`

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

Parameters:

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

    The app API key from an app from the /dashboard/developer page Defaults to

  • app_id (String, nil) (defaults to: ENV["WHOP_APP_ID"])

    Defaults to ‘ENV`

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


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
206
207
208
209
210
211
212
# File 'lib/whop_sdk/client.rb', line 147

def initialize(
  api_key: ENV["WHOP_API_KEY"],
  app_id: ENV["WHOP_APP_ID"],
  base_url: ENV["WHOP_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.whop.com/api/v1"

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

  headers = {
    "x-whop-app-id" => (@app_id = app_id.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
  )

  @apps = WhopSDK::Resources::Apps.new(client: self)
  @invoices = WhopSDK::Resources::Invoices.new(client: self)
  @course_lesson_interactions = WhopSDK::Resources::CourseLessonInteractions.new(client: self)
  @products = WhopSDK::Resources::Products.new(client: self)
  @companies = WhopSDK::Resources::Companies.new(client: self)
  @webhooks = WhopSDK::Resources::Webhooks.new(client: self)
  @plans = WhopSDK::Resources::Plans.new(client: self)
  @entries = WhopSDK::Resources::Entries.new(client: self)
  @forum_posts = WhopSDK::Resources::ForumPosts.new(client: self)
  @transfers = WhopSDK::Resources::Transfers.new(client: self)
  @ledger_accounts = WhopSDK::Resources::LedgerAccounts.new(client: self)
  @memberships = WhopSDK::Resources::Memberships.new(client: self)
  @authorized_users = WhopSDK::Resources::AuthorizedUsers.new(client: self)
  @app_builds = WhopSDK::Resources::AppBuilds.new(client: self)
  @shipments = WhopSDK::Resources::Shipments.new(client: self)
  @checkout_configurations = WhopSDK::Resources::CheckoutConfigurations.new(client: self)
  @messages = WhopSDK::Resources::Messages.new(client: self)
  @chat_channels = WhopSDK::Resources::ChatChannels.new(client: self)
  @users = WhopSDK::Resources::Users.new(client: self)
  @payments = WhopSDK::Resources::Payments.new(client: self)
  @support_channels = WhopSDK::Resources::SupportChannels.new(client: self)
  @experiences = WhopSDK::Resources::Experiences.new(client: self)
  @reactions = WhopSDK::Resources::Reactions.new(client: self)
  @members = WhopSDK::Resources::Members.new(client: self)
  @forums = WhopSDK::Resources::Forums.new(client: self)
  @promo_codes = WhopSDK::Resources::PromoCodes.new(client: self)
  @courses = WhopSDK::Resources::Courses.new(client: self)
  @course_chapters = WhopSDK::Resources::CourseChapters.new(client: self)
  @course_lessons = WhopSDK::Resources::CourseLessons.new(client: self)
  @reviews = WhopSDK::Resources::Reviews.new(client: self)
  @course_students = WhopSDK::Resources::CourseStudents.new(client: self)
  @access_tokens = WhopSDK::Resources::AccessTokens.new(client: self)
end

Instance Attribute Details

#access_tokensWhopSDK::Resources::AccessTokens (readonly)



119
120
121
# File 'lib/whop_sdk/client.rb', line 119

def access_tokens
  @access_tokens
end

#api_keyString (readonly)

The app API key from an app from the /dashboard/developer page

Returns:

  • (String)


20
21
22
# File 'lib/whop_sdk/client.rb', line 20

def api_key
  @api_key
end

#app_buildsWhopSDK::Resources::AppBuilds (readonly)



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

def app_builds
  @app_builds
end

#app_idString (readonly)

Returns:

  • (String)


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

def app_id
  @app_id
end

#appsWhopSDK::Resources::Apps (readonly)



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

def apps
  @apps
end

#authorized_usersWhopSDK::Resources::AuthorizedUsers (readonly)



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

def authorized_users
  @authorized_users
end

#chat_channelsWhopSDK::Resources::ChatChannels (readonly)



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

def chat_channels
  @chat_channels
end

#checkout_configurationsWhopSDK::Resources::CheckoutConfigurations (readonly)



71
72
73
# File 'lib/whop_sdk/client.rb', line 71

def checkout_configurations
  @checkout_configurations
end

#companiesWhopSDK::Resources::Companies (readonly)



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

def companies
  @companies
end

#course_chaptersWhopSDK::Resources::CourseChapters (readonly)



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

def course_chapters
  @course_chapters
end

#course_lesson_interactionsWhopSDK::Resources::CourseLessonInteractions (readonly)



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

def course_lesson_interactions
  @course_lesson_interactions
end

#course_lessonsWhopSDK::Resources::CourseLessons (readonly)



110
111
112
# File 'lib/whop_sdk/client.rb', line 110

def course_lessons
  @course_lessons
end

#course_studentsWhopSDK::Resources::CourseStudents (readonly)



116
117
118
# File 'lib/whop_sdk/client.rb', line 116

def course_students
  @course_students
end

#coursesWhopSDK::Resources::Courses (readonly)



104
105
106
# File 'lib/whop_sdk/client.rb', line 104

def courses
  @courses
end

#entriesWhopSDK::Resources::Entries (readonly)



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

def entries
  @entries
end

#experiencesWhopSDK::Resources::Experiences (readonly)



89
90
91
# File 'lib/whop_sdk/client.rb', line 89

def experiences
  @experiences
end

#forum_postsWhopSDK::Resources::ForumPosts (readonly)



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

def forum_posts
  @forum_posts
end

#forumsWhopSDK::Resources::Forums (readonly)



98
99
100
# File 'lib/whop_sdk/client.rb', line 98

def forums
  @forums
end

#invoicesWhopSDK::Resources::Invoices (readonly)



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

def invoices
  @invoices
end

#ledger_accountsWhopSDK::Resources::LedgerAccounts (readonly)



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

def ledger_accounts
  @ledger_accounts
end

#membersWhopSDK::Resources::Members (readonly)



95
96
97
# File 'lib/whop_sdk/client.rb', line 95

def members
  @members
end

#membershipsWhopSDK::Resources::Memberships (readonly)



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

def memberships
  @memberships
end

#messagesWhopSDK::Resources::Messages (readonly)



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

def messages
  @messages
end

#paymentsWhopSDK::Resources::Payments (readonly)



83
84
85
# File 'lib/whop_sdk/client.rb', line 83

def payments
  @payments
end

#plansWhopSDK::Resources::Plans (readonly)



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

def plans
  @plans
end

#productsWhopSDK::Resources::Products (readonly)



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

def products
  @products
end

#promo_codesWhopSDK::Resources::PromoCodes (readonly)



101
102
103
# File 'lib/whop_sdk/client.rb', line 101

def promo_codes
  @promo_codes
end

#reactionsWhopSDK::Resources::Reactions (readonly)



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

def reactions
  @reactions
end

#reviewsWhopSDK::Resources::Reviews (readonly)



113
114
115
# File 'lib/whop_sdk/client.rb', line 113

def reviews
  @reviews
end

#shipmentsWhopSDK::Resources::Shipments (readonly)



68
69
70
# File 'lib/whop_sdk/client.rb', line 68

def shipments
  @shipments
end

#support_channelsWhopSDK::Resources::SupportChannels (readonly)



86
87
88
# File 'lib/whop_sdk/client.rb', line 86

def support_channels
  @support_channels
end

#transfersWhopSDK::Resources::Transfers (readonly)



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

def transfers
  @transfers
end

#usersWhopSDK::Resources::Users (readonly)



80
81
82
# File 'lib/whop_sdk/client.rb', line 80

def users
  @users
end

#webhooksWhopSDK::Resources::Webhooks (readonly)



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

def webhooks
  @webhooks
end

Instance Method Details

#verify_user_token(token_or_headers, **opts) ⇒ UserTokenPayload?

Verifies a Whop user token

Parameters:

  • token_or_headers (String, Hash, nil)

    The token string or headers hash

  • app_id (String, nil)

    The app id to verify against

  • public_key (String, nil)

    Optional custom public key (PEM format)

  • header_name (String, nil)

    The header name to use (defaults to x-whop-user-token)

Returns:

  • (UserTokenPayload, nil)

    The verified token payload or nil if the verification failed



234
235
236
237
238
# File 'lib/whop_sdk/client.rb', line 234

def verify_user_token(token_or_headers, **opts)
  verify_user_token!(token_or_headers, **opts)
rescue StandardError
  nil
end

#verify_user_token!(token_or_headers, **opts) ⇒ UserTokenPayload

Verifies a Whop user token

Parameters:

  • token_or_headers (String, Hash, nil)

    The token string or headers hash

  • app_id (String, nil)

    The app id to verify against

  • public_key (String, nil)

    Optional custom public key (PEM format)

  • header_name (String, nil)

    The header name to use (defaults to x-whop-user-token)

Returns:

  • (UserTokenPayload)

    The verified token payload

Raises:

  • (StandardError)

    If verification fails



222
223
224
225
# File 'lib/whop_sdk/client.rb', line 222

def verify_user_token!(token_or_headers, **opts)
  opts[:app_id] ||= app_id
  Helpers::VerifyUserToken.verify_user_token!(token_or_headers, **opts)
end