-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
196 lines (170 loc) · 5.35 KB
/
Client.php
File metadata and controls
196 lines (170 loc) · 5.35 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
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace GetStream;
use GetStream\Auth\JWTGenerator;
use GetStream\Exceptions\StreamException;
use GetStream\Generated\CommonTrait;
use GetStream\Http\GuzzleHttpClient;
use GetStream\Http\HttpClientInterface;
/**
* Main GetStream client for interacting with the API.
*/
class Client
{
use CommonTrait;
private string $apiKey;
private string $apiSecret;
private string $baseUrl;
private HttpClientInterface $httpClient;
private JWTGenerator $jwtGenerator;
private array $defaultHeaders;
/**
* Create a new GetStream client.
*
* @param string $apiKey The API key
* @param string $apiSecret The API secret
* @param string $baseUrl The base URL for the API
* @param HttpClientInterface|null $httpClient Optional HTTP client
*/
public function __construct(
string $apiKey,
string $apiSecret,
string $baseUrl = 'https://chat.stream-io-api.com',
?HttpClientInterface $httpClient = null
) {
if (empty($apiKey)) {
throw new StreamException('API key cannot be empty');
}
if (empty($apiSecret)) {
throw new StreamException('API secret cannot be empty');
}
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->baseUrl = rtrim($baseUrl, '/');
$this->httpClient = $httpClient ?? new GuzzleHttpClient();
$this->jwtGenerator = new JWTGenerator($apiSecret);
$this->defaultHeaders = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'User-Agent' => 'getstream-php-sdk/' . Constant::VERSION,
'x-stream-client' => 'stream-php-client-' . Constant::VERSION,
];
}
/**
* Get the API key.
*/
public function getApiKey(): string
{
return $this->apiKey;
}
/**
* Get the API secret.
*/
public function getApiSecret(): string
{
return $this->apiSecret;
}
/**
* Get the base URL.
*/
public function getBaseUrl(): string
{
return $this->baseUrl;
}
/**
* Get the HTTP client.
*/
public function getHttpClient(): HttpClientInterface
{
return $this->httpClient;
}
/**
* Get the JWT generator.
*/
public function getJWTGenerator(): JWTGenerator
{
return $this->jwtGenerator;
}
/**
* Create a feed instance.
*
* @param string $feedGroup The feed group (e.g., 'user', 'timeline')
* @param string $feedId The feed ID (e.g., user ID)
*
* @throws StreamException
*/
public function feed(string $feedGroup, string $feedId): Feed
{
// Create a FeedsV3Client instance using the same configuration
$feedsV3Client = new FeedsV3Client($this->apiKey, $this->apiSecret, $this->baseUrl, $this->httpClient);
return new Feed($feedsV3Client, $feedGroup, $feedId);
}
/**
* Make an authenticated HTTP request to the GetStream API.
*
* @param string $method HTTP method
* @param string $path API path
* @param array $queryParams Query parameters
* @param mixed $body Request body
* @param array $pathParams Path parameters for URL substitution
*
* @return StreamResponse<mixed>
*
* @throws StreamException
*/
public function makeRequest(
string $method,
string $path,
array $queryParams = [],
mixed $body = null,
array $pathParams = []
): StreamResponse {
// Replace path parameters
foreach ($pathParams as $key => $value) {
$path = str_replace('{' . $key . '}', (string) $value, $path);
}
// Build URL
$url = $this->baseUrl . $path;
// Add API key to query parameters
$queryParams['api_key'] = $this->apiKey;
// Add query parameters (there will always be at least api_key)
$url .= '?' . http_build_query($queryParams);
// Generate authentication token
$token = $this->jwtGenerator->generateServerToken();
// Prepare headers
$headers = array_merge($this->defaultHeaders, [
'Authorization' => $token,
'stream-auth-type' => 'jwt',
]);
// Make the request
return $this->httpClient->request($method, $url, $headers, $body);
}
/**
* Generate a user token for client-side authentication.
*
* @param string $userId The user ID
* @param array $claims Additional claims
* @param int|null $expiration Token expiration in seconds (null for no expiration)
*
* @return string JWT token
*/
public function createUserToken(string $userId, array $claims = [], ?int $expiration = null): string
{
return $this->jwtGenerator->generateUserToken($userId, $claims, $expiration);
}
/**
* Create or update users.
*
* @param array $users Array of user data keyed by user ID
*
* @return StreamResponse<mixed>
*
* @throws StreamException
*/
public function upsertUsers(array $users): StreamResponse
{
$path = '/api/v2/users';
$requestData = ['users' => $users];
return $this->makeRequest('POST', $path, [], $requestData);
}
}