44
55namespace GetStream ;
66
7- use GetStream \Exceptions \StreamException ;
8- use GetStream \Http \HttpClientInterface ;
9- use GetStream \Http \GuzzleHttpClient ;
107use GetStream \Auth \JWTGenerator ;
8+ use GetStream \Exceptions \StreamException ;
119use 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 */
1616class 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