Skip to content

Commit b8533a7

Browse files
authored
Merge pull request #7 from GetStream/sdk-update
[FEEDS-000]Sdk update
2 parents 7055ff1 + c2503d1 commit b8533a7

214 files changed

Lines changed: 3669 additions & 911 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Auth/JWTGenerator.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44

55
namespace GetStream\Auth;
66

7-
use GetStream\Exceptions\StreamException;
87
use Firebase\JWT\JWT;
98
use Firebase\JWT\Key;
9+
use GetStream\Exceptions\StreamException;
1010

1111
/**
12-
* JWT token generator for GetStream authentication
12+
* JWT token generator for GetStream authentication.
1313
*/
1414
class JWTGenerator
1515
{
1616
private string $secret;
1717
private string $algorithm;
1818

1919
/**
20-
* Create a new JWTGenerator
20+
* Create a new JWTGenerator.
2121
*
22-
* @param string $secret The API secret
22+
* @param string $secret The API secret
2323
* @param string $algorithm JWT algorithm (default: HS256)
2424
*/
2525
public function __construct(string $secret, string $algorithm = 'HS256')
@@ -33,16 +33,17 @@ public function __construct(string $secret, string $algorithm = 'HS256')
3333
}
3434

3535
/**
36-
* Generate a server-side token for API authentication
36+
* Generate a server-side token for API authentication.
3737
*
38-
* @param array $claims Additional claims to include
38+
* @param array $claims Additional claims to include
3939
* @param int|null $expiration Token expiration in seconds (null for no expiration)
40+
*
4041
* @return string JWT token
4142
*/
4243
public function generateServerToken(array $claims = [], ?int $expiration = null): string
4344
{
4445
$now = time();
45-
46+
4647
$payload = array_merge([
4748
'iat' => $now,
4849
'server' => true,
@@ -56,11 +57,12 @@ public function generateServerToken(array $claims = [], ?int $expiration = null)
5657
}
5758

5859
/**
59-
* Generate a user token for client-side authentication
60+
* Generate a user token for client-side authentication.
6061
*
61-
* @param string $userId The user ID
62-
* @param array $claims Additional claims to include
62+
* @param string $userId The user ID
63+
* @param array $claims Additional claims to include
6364
* @param int|null $expiration Token expiration in seconds (null for no expiration)
65+
*
6466
* @return string JWT token
6567
*/
6668
public function generateUserToken(string $userId, array $claims = [], ?int $expiration = null): string
@@ -70,7 +72,7 @@ public function generateUserToken(string $userId, array $claims = [], ?int $expi
7072
}
7173

7274
$now = time();
73-
75+
7476
$payload = array_merge([
7577
'user_id' => $userId,
7678
'iat' => $now,
@@ -84,33 +86,37 @@ public function generateUserToken(string $userId, array $claims = [], ?int $expi
8486
}
8587

8688
/**
87-
* Verify and decode a JWT token
89+
* Verify and decode a JWT token.
8890
*
8991
* @param string $token The JWT token to verify
92+
*
9093
* @return array Decoded token payload
94+
*
9195
* @throws StreamException
9296
*/
9397
public function verifyToken(string $token): array
9498
{
9599
try {
96100
$decoded = JWT::decode($token, new Key($this->secret, $this->algorithm));
101+
97102
return (array) $decoded;
98103
} catch (\Exception $e) {
99104
throw new StreamException('Invalid JWT token: ' . $e->getMessage(), 0, $e);
100105
}
101106
}
102107

103108
/**
104-
* Verify a webhook signature
109+
* Verify a webhook signature.
105110
*
106-
* @param string $body The request body
111+
* @param string $body The request body
107112
* @param string $signature The signature to verify
113+
*
108114
* @return bool True if signature is valid
109115
*/
110116
public function verifyWebhookSignature(string $body, string $signature): bool
111117
{
112118
$expectedSignature = hash_hmac('sha256', $body, $this->secret);
119+
113120
return hash_equals($expectedSignature, $signature);
114121
}
115122
}
116-

src/Client.php

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace GetStream;
66

7-
use GetStream\Exceptions\StreamException;
8-
use GetStream\Http\HttpClientInterface;
9-
use GetStream\Http\GuzzleHttpClient;
107
use GetStream\Auth\JWTGenerator;
8+
use GetStream\Exceptions\StreamException;
119
use GetStream\Generated\CommonTrait;
10+
use GetStream\Http\GuzzleHttpClient;
11+
use GetStream\Http\HttpClientInterface;
1212

1313
/**
14-
* Main GetStream client for interacting with the API
14+
* Main GetStream client for interacting with the API.
1515
*/
1616
class Client
1717
{
@@ -24,11 +24,11 @@ class Client
2424
private array $defaultHeaders;
2525

2626
/**
27-
* Create a new GetStream client
27+
* Create a new GetStream client.
2828
*
29-
* @param string $apiKey The API key
30-
* @param string $apiSecret The API secret
31-
* @param string $baseUrl The base URL for the API
29+
* @param string $apiKey The API key
30+
* @param string $apiSecret The API secret
31+
* @param string $baseUrl The base URL for the API
3232
* @param HttpClientInterface|null $httpClient Optional HTTP client
3333
*/
3434
public function __construct(
@@ -40,7 +40,7 @@ public function __construct(
4040
if (empty($apiKey)) {
4141
throw new StreamException('API key cannot be empty');
4242
}
43-
43+
4444
if (empty($apiSecret)) {
4545
throw new StreamException('API secret cannot be empty');
4646
}
@@ -50,7 +50,7 @@ public function __construct(
5050
$this->baseUrl = rtrim($baseUrl, '/');
5151
$this->httpClient = $httpClient ?? new GuzzleHttpClient();
5252
$this->jwtGenerator = new JWTGenerator($apiSecret);
53-
53+
5454
$this->defaultHeaders = [
5555
'Content-Type' => 'application/json',
5656
'Accept' => 'application/json',
@@ -59,69 +59,72 @@ public function __construct(
5959
}
6060

6161
/**
62-
* Get the API key
62+
* Get the API key.
6363
*/
6464
public function getApiKey(): string
6565
{
6666
return $this->apiKey;
6767
}
6868

6969
/**
70-
* Get the API secret
70+
* Get the API secret.
7171
*/
7272
public function getApiSecret(): string
7373
{
7474
return $this->apiSecret;
7575
}
7676

7777
/**
78-
* Get the base URL
78+
* Get the base URL.
7979
*/
8080
public function getBaseUrl(): string
8181
{
8282
return $this->baseUrl;
8383
}
8484

8585
/**
86-
* Get the HTTP client
86+
* Get the HTTP client.
8787
*/
8888
public function getHttpClient(): HttpClientInterface
8989
{
9090
return $this->httpClient;
9191
}
9292

9393
/**
94-
* Get the JWT generator
94+
* Get the JWT generator.
9595
*/
9696
public function getJWTGenerator(): JWTGenerator
9797
{
9898
return $this->jwtGenerator;
9999
}
100100

101101
/**
102-
* Create a feed instance
102+
* Create a feed instance.
103103
*
104104
* @param string $feedGroup The feed group (e.g., 'user', 'timeline')
105-
* @param string $feedId The feed ID (e.g., user ID)
106-
* @return Feed
105+
* @param string $feedId The feed ID (e.g., user ID)
106+
*
107107
* @throws StreamException
108108
*/
109109
public function feed(string $feedGroup, string $feedId): Feed
110110
{
111111
// Create a FeedsV3Client instance using the same configuration
112112
$feedsV3Client = new FeedsV3Client($this->apiKey, $this->apiSecret, $this->baseUrl, $this->httpClient);
113+
113114
return new Feed($feedsV3Client, $feedGroup, $feedId);
114115
}
115116

116117
/**
117-
* Make an authenticated HTTP request to the GetStream API
118+
* Make an authenticated HTTP request to the GetStream API.
119+
*
120+
* @param string $method HTTP method
121+
* @param string $path API path
122+
* @param array $queryParams Query parameters
123+
* @param mixed $body Request body
124+
* @param array $pathParams Path parameters for URL substitution
118125
*
119-
* @param string $method HTTP method
120-
* @param string $path API path
121-
* @param array $queryParams Query parameters
122-
* @param mixed $body Request body
123-
* @param array $pathParams Path parameters for URL substitution
124126
* @return StreamResponse<mixed>
127+
*
125128
* @throws StreamException
126129
*/
127130
public function makeRequest(
@@ -133,21 +136,21 @@ public function makeRequest(
133136
): StreamResponse {
134137
// Replace path parameters
135138
foreach ($pathParams as $key => $value) {
136-
$path = str_replace('{' . $key . '}', (string)$value, $path);
139+
$path = str_replace('{' . $key . '}', (string) $value, $path);
137140
}
138141

139142
// Build URL
140143
$url = $this->baseUrl . $path;
141-
144+
142145
// Add API key to query parameters
143146
$queryParams['api_key'] = $this->apiKey;
144-
147+
145148
// Add query parameters (there will always be at least api_key)
146149
$url .= '?' . http_build_query($queryParams);
147150

148151
// Generate authentication token
149152
$token = $this->jwtGenerator->generateServerToken();
150-
153+
151154
// Prepare headers
152155
$headers = array_merge($this->defaultHeaders, [
153156
'Authorization' => $token,
@@ -158,14 +161,13 @@ public function makeRequest(
158161
return $this->httpClient->request($method, $url, $headers, $body);
159162
}
160163

161-
162-
163164
/**
164-
* Generate a user token for client-side authentication
165+
* Generate a user token for client-side authentication.
165166
*
166-
* @param string $userId The user ID
167-
* @param array $claims Additional claims
167+
* @param string $userId The user ID
168+
* @param array $claims Additional claims
168169
* @param int|null $expiration Token expiration in seconds (null for no expiration)
170+
*
169171
* @return string JWT token
170172
*/
171173
public function createUserToken(string $userId, array $claims = [], ?int $expiration = null): string
@@ -174,17 +176,19 @@ public function createUserToken(string $userId, array $claims = [], ?int $expira
174176
}
175177

176178
/**
177-
* Create or update users
179+
* Create or update users.
178180
*
179181
* @param array $users Array of user data keyed by user ID
182+
*
180183
* @return StreamResponse<mixed>
184+
*
181185
* @throws StreamException
182186
*/
183187
public function upsertUsers(array $users): StreamResponse
184188
{
185189
$path = '/api/v2/users';
186190
$requestData = ['users' => $users];
187-
191+
188192
return $this->makeRequest('POST', $path, [], $requestData);
189193
}
190194
}

0 commit comments

Comments
 (0)