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`

tokens Defaults to ‘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"])

    When using the SDK in app mode pass this parameter to allow verifying user

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


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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/whop_sdk/client.rb', line 171

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 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)
  @notifications = WhopSDK::Resources::Notifications.new(client: self)
  @disputes = WhopSDK::Resources::Disputes.new(client: self)
  @refunds = WhopSDK::Resources::Refunds.new(client: self)
  @withdrawals = WhopSDK::Resources::Withdrawals.new(client: self)
  @account_links = WhopSDK::Resources::AccountLinks.new(client: self)
  @setup_intents = WhopSDK::Resources::SetupIntents.new(client: self)
  @payment_methods = WhopSDK::Resources::PaymentMethods.new(client: self)
end

Instance Attribute Details

#access_tokensWhopSDK::Resources::AccessTokens (readonly)



121
122
123
# File 'lib/whop_sdk/client.rb', line 121

def access_tokens
  @access_tokens
end


136
137
138
# File 'lib/whop_sdk/client.rb', line 136

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



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

def app_builds
  @app_builds
end

#app_idString? (readonly)

When using the SDK in app mode pass this parameter to allow verifying user tokens

Returns:

  • (String, nil)


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

def app_id
  @app_id
end

#appsWhopSDK::Resources::Apps (readonly)



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

def apps
  @apps
end

#authorized_usersWhopSDK::Resources::AuthorizedUsers (readonly)



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

def authorized_users
  @authorized_users
end

#chat_channelsWhopSDK::Resources::ChatChannels (readonly)



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

def chat_channels
  @chat_channels
end

#checkout_configurationsWhopSDK::Resources::CheckoutConfigurations (readonly)



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

def checkout_configurations
  @checkout_configurations
end

#companiesWhopSDK::Resources::Companies (readonly)



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

def companies
  @companies
end

#course_chaptersWhopSDK::Resources::CourseChapters (readonly)



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

def course_chapters
  @course_chapters
end

#course_lesson_interactionsWhopSDK::Resources::CourseLessonInteractions (readonly)



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

def course_lesson_interactions
  @course_lesson_interactions
end

#course_lessonsWhopSDK::Resources::CourseLessons (readonly)



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

def course_lessons
  @course_lessons
end

#course_studentsWhopSDK::Resources::CourseStudents (readonly)



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

def course_students
  @course_students
end

#coursesWhopSDK::Resources::Courses (readonly)



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

def courses
  @courses
end

#disputesWhopSDK::Resources::Disputes (readonly)



127
128
129
# File 'lib/whop_sdk/client.rb', line 127

def disputes
  @disputes
end

#entriesWhopSDK::Resources::Entries (readonly)



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

def entries
  @entries
end

#experiencesWhopSDK::Resources::Experiences (readonly)



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

def experiences
  @experiences
end

#forum_postsWhopSDK::Resources::ForumPosts (readonly)



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

def forum_posts
  @forum_posts
end

#forumsWhopSDK::Resources::Forums (readonly)



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

def forums
  @forums
end

#invoicesWhopSDK::Resources::Invoices (readonly)



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

def invoices
  @invoices
end

#ledger_accountsWhopSDK::Resources::LedgerAccounts (readonly)



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

def ledger_accounts
  @ledger_accounts
end

#membersWhopSDK::Resources::Members (readonly)



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

def members
  @members
end

#membershipsWhopSDK::Resources::Memberships (readonly)



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

def memberships
  @memberships
end

#messagesWhopSDK::Resources::Messages (readonly)



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

def messages
  @messages
end

#notificationsWhopSDK::Resources::Notifications (readonly)



124
125
126
# File 'lib/whop_sdk/client.rb', line 124

def notifications
  @notifications
end

#payment_methodsWhopSDK::Resources::PaymentMethods (readonly)



142
143
144
# File 'lib/whop_sdk/client.rb', line 142

def payment_methods
  @payment_methods
end

#paymentsWhopSDK::Resources::Payments (readonly)



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

def payments
  @payments
end

#plansWhopSDK::Resources::Plans (readonly)



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

def plans
  @plans
end

#productsWhopSDK::Resources::Products (readonly)



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

def products
  @products
end

#promo_codesWhopSDK::Resources::PromoCodes (readonly)



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

def promo_codes
  @promo_codes
end

#reactionsWhopSDK::Resources::Reactions (readonly)



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

def reactions
  @reactions
end

#refundsWhopSDK::Resources::Refunds (readonly)



130
131
132
# File 'lib/whop_sdk/client.rb', line 130

def refunds
  @refunds
end

#reviewsWhopSDK::Resources::Reviews (readonly)



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

def reviews
  @reviews
end

#setup_intentsWhopSDK::Resources::SetupIntents (readonly)



139
140
141
# File 'lib/whop_sdk/client.rb', line 139

def setup_intents
  @setup_intents
end

#shipmentsWhopSDK::Resources::Shipments (readonly)



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

def shipments
  @shipments
end

#support_channelsWhopSDK::Resources::SupportChannels (readonly)



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

def support_channels
  @support_channels
end

#transfersWhopSDK::Resources::Transfers (readonly)



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

def transfers
  @transfers
end

#usersWhopSDK::Resources::Users (readonly)



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

def users
  @users
end

#webhooksWhopSDK::Resources::Webhooks (readonly)



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

def webhooks
  @webhooks
end

#withdrawalsWhopSDK::Resources::Withdrawals (readonly)



133
134
135
# File 'lib/whop_sdk/client.rb', line 133

def withdrawals
  @withdrawals
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



265
266
267
268
269
# File 'lib/whop_sdk/client.rb', line 265

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



250
251
252
253
254
255
256
# File 'lib/whop_sdk/client.rb', line 250

def verify_user_token!(token_or_headers, **opts)
  opts[:app_id] ||= app_id
  unless opts[:app_id]
    raise StandardError, "You must set app_id in the Whop client if you want to verify user tokens"
  end
  Helpers::VerifyUserToken.verify_user_token!(token_or_headers, **opts)
end