-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhopClient.php
More file actions
189 lines (167 loc) · 6.59 KB
/
Copy pathWhopClient.php
File metadata and controls
189 lines (167 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
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
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
<?php
declare(strict_types=1);
namespace Rapttor\WhopSdk;
use Rapttor\WhopSdk\Http\HttpClient;
use Rapttor\WhopSdk\Resources\AccessTokens;
use Rapttor\WhopSdk\Resources\Apps;
use Rapttor\WhopSdk\Resources\CheckoutConfigurations;
use Rapttor\WhopSdk\Resources\Companies;
use Rapttor\WhopSdk\Resources\Experiences;
use Rapttor\WhopSdk\Resources\Invoices;
use Rapttor\WhopSdk\Resources\Members;
use Rapttor\WhopSdk\Resources\Memberships;
use Rapttor\WhopSdk\Resources\Payments;
use Rapttor\WhopSdk\Resources\Plans;
use Rapttor\WhopSdk\Resources\Products;
use Rapttor\WhopSdk\Resources\PromoCodes;
use Rapttor\WhopSdk\Resources\Refunds;
use Rapttor\WhopSdk\Resources\Reviews;
use Rapttor\WhopSdk\Resources\Users;
use Rapttor\WhopSdk\Resources\Webhooks;
/**
* Whop API client.
*
* @property-read AccessTokens $accessTokens
* @property-read Apps $apps
* @property-read CheckoutConfigurations $checkoutConfigurations
* @property-read Companies $companies
* @property-read Experiences $experiences
* @property-read Invoices $invoices
* @property-read Members $members
* @property-read Memberships $memberships
* @property-read Payments $payments
* @property-read Plans $plans
* @property-read Products $products
* @property-read PromoCodes $promoCodes
* @property-read Refunds $refunds
* @property-read Reviews $reviews
* @property-read Users $users
* @property-read Webhooks $webhooks
*/
class WhopClient
{
private HttpClient $http;
/** @var array<string, object> */
private array $resourceCache = [];
/** @var array<string, class-string> */
private const RESOURCES = [
'accessTokens' => AccessTokens::class,
'apps' => Apps::class,
'checkoutConfigurations' => CheckoutConfigurations::class,
'companies' => Companies::class,
'experiences' => Experiences::class,
'invoices' => Invoices::class,
'members' => Members::class,
'memberships' => Memberships::class,
'payments' => Payments::class,
'plans' => Plans::class,
'products' => Products::class,
'promoCodes' => PromoCodes::class,
'refunds' => Refunds::class,
'reviews' => Reviews::class,
'users' => Users::class,
'webhooks' => Webhooks::class,
];
/**
* @param string|null $apiKey Whop API key. Falls back to the WHOP_API_KEY env variable.
* @param string $baseUrl Override the API base URL.
* @param int $maxRetries Number of automatic retries (default: 2).
* @param int $timeout Request timeout in seconds (default: 60).
* @param array<string, string> $defaultHeaders Extra headers sent on every request.
*/
public function __construct(
?string $apiKey = null,
string $baseUrl = 'https://api.whop.com',
int $maxRetries = 2,
int $timeout = 60,
array $defaultHeaders = [],
) {
$resolvedKey = $apiKey ?? (string) getenv('WHOP_API_KEY');
if ($resolvedKey === '') {
throw new \InvalidArgumentException(
'A Whop API key is required. Pass it via $apiKey or set the WHOP_API_KEY environment variable.'
);
}
$this->http = new HttpClient(
apiKey: $resolvedKey,
baseUrl: rtrim($baseUrl, '/'),
maxRetries: $maxRetries,
timeout: $timeout,
defaultHeaders: $defaultHeaders,
);
}
// ── Magic resource access ──────────────────────────────────────────────────
public function __get(string $name): object
{
if (!array_key_exists($name, self::RESOURCES)) {
throw new \InvalidArgumentException(
"Unknown resource \"{$name}\". Available: " . implode(', ', array_keys(self::RESOURCES))
);
}
if (!isset($this->resourceCache[$name])) {
$class = self::RESOURCES[$name];
$this->resourceCache[$name] = new $class($this->http);
}
return $this->resourceCache[$name];
}
public function __isset(string $name): bool
{
return array_key_exists($name, self::RESOURCES);
}
// ── Option overrides ───────────────────────────────────────────────────────
/**
* Return a new client instance with the given options overridden.
*
* Useful for per-request timeout / retry customisation without mutating the
* original client.
*
* @param array{
* max_retries?: int,
* timeout?: int,
* headers?: array<string, string>
* } $options
*/
public function withOptions(array $options): static
{
$clone = clone $this;
$clone->http = $this->http->withOptions($options);
$clone->resourceCache = [];
return $clone;
}
// ── Raw HTTP helpers ───────────────────────────────────────────────────────
/**
* @param array<string, mixed> $query
* @param array<string, mixed> $options
* @return array<string, mixed>
*/
public function get(string $path, array $query = [], array $options = []): array
{
return $this->http->get($path, $query, $options);
}
/**
* @param array<string, mixed> $body
* @param array<string, mixed> $options
* @return array<string, mixed>
*/
public function post(string $path, array $body = [], array $options = []): array
{
return $this->http->post($path, $body, $options);
}
/**
* @param array<string, mixed> $body
* @param array<string, mixed> $options
* @return array<string, mixed>
*/
public function patch(string $path, array $body = [], array $options = []): array
{
return $this->http->patch($path, $body, $options);
}
/**
* @param array<string, mixed> $options
* @return array<string, mixed>
*/
public function delete(string $path, array $options = []): array
{
return $this->http->delete($path, $options);
}
}