Skip to content

Commit fd209e5

Browse files
authored
feat(imports): add import endpoints (#92)
1 parent 8f8bb0b commit fd209e5

File tree

5 files changed

+132
-20
lines changed

5 files changed

+132
-20
lines changed

.phan/config.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
],
4444

4545
'suppress_issue_types' => [
46-
'PhanPluginDescriptionlessCommentOnPublicMethod',
4746
'PhanPluginDescriptionlessCommentOnProtectedProperty',
48-
'PhanPluginDescriptionlessCommentOnPublicProperty',
4947
'PhanPluginDescriptionlessCommentOnPrivateMethod',
5048
'PhanPluginDescriptionlessCommentOnPrivateProperty',
5149
'PhanPluginDescriptionlessCommentOnProtectedMethod',

lib/GetStream/StreamChat/Channel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
*/
1010
class Channel
1111
{
12-
/**
12+
/** Channel type of the channel.
1313
* @var string
1414
*/
1515
public $channelType;
1616

17-
/**
17+
/** The id of the channel.
1818
* @var string|null
1919
*/
2020
public $id;
@@ -510,8 +510,8 @@ public function mute(string $userId, int $expirationInMilliSeconds = null): Stre
510510
return $this->client->post("moderation/mute/channel", $postData);
511511
}
512512

513-
/** @link https://getstream.io/chat/docs/php/muting_channels/?language=php
514-
* Unmutes the channel for the given user.
513+
/** Unmutes the channel for the given user.
514+
* @link https://getstream.io/chat/docs/php/muting_channels/?language=php
515515
* @throws StreamException
516516
*/
517517
public function unmute(string $userId): StreamResponse

lib/GetStream/StreamChat/Client.php

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function getBaseUrl(): string
123123
return "https://chat.stream-io-api.com";
124124
}
125125

126-
/**
126+
/** For internal usage only.
127127
* @internal
128128
*/
129129
public function buildRequestUrl(string $uri): string
@@ -163,7 +163,7 @@ private function getHttpRequestHeaders(): array
163163
/**
164164
* @throws StreamException
165165
*/
166-
public function makeHttpRequest(string $uri, string $method, $data = [], array $queryParams = [], array $multipart = []): StreamResponse
166+
private function makeHttpRequest(string $uri, string $method, $data = [], array $queryParams = [], array $multipart = []): StreamResponse
167167
{
168168
$queryParams['api_key'] = $this->apiKey;
169169
$headers = $this->getHttpRequestHeaders();
@@ -232,39 +232,44 @@ public function createToken(string $userId, int $expiration = null, int $issuedA
232232
return $this->jwtHandler->encode($this->apiSecret, $payload);
233233
}
234234

235-
/**
235+
/** For internal usage only.
236+
* @internal
236237
* @throws StreamException
237238
*/
238239
public function get(string $uri, array $queryParams = []): StreamResponse
239240
{
240241
return $this->makeHttpRequest($uri, "GET", [], $queryParams);
241242
}
242243

243-
/**
244+
/** For internal usage only.
245+
* @internal
244246
* @throws StreamException
245247
*/
246248
public function delete(string $uri, array $queryParams = []): StreamResponse
247249
{
248250
return $this->makeHttpRequest($uri, "DELETE", [], $queryParams);
249251
}
250252

251-
/**
253+
/** For internal usage only.
254+
* @internal
252255
* @throws StreamException
253256
*/
254257
public function patch(string $uri, array $data, array $queryParams = []): StreamResponse
255258
{
256259
return $this->makeHttpRequest($uri, "PATCH", $data, $queryParams);
257260
}
258261

259-
/**
262+
/** For internal usage only.
263+
* @internal
260264
* @throws StreamException
261265
*/
262266
public function post(string $uri, $data, array $queryParams = []): StreamResponse
263267
{
264268
return $this->makeHttpRequest($uri, "POST", $data, $queryParams);
265269
}
266270

267-
/**
271+
/** For internal usage only.
272+
* @internal
268273
* @throws StreamException
269274
*/
270275
public function put(string $uri, array $data, array $queryParams = []): StreamResponse
@@ -332,7 +337,8 @@ public function upsertUser(array $user): StreamResponse
332337
return $this->upsertUsers([$user]);
333338
}
334339

335-
/** @link https://getstream.io/chat/docs/php/update_users/?language=php
340+
/** Update multiple users.
341+
* @link https://getstream.io/chat/docs/php/update_users/?language=php
336342
* @deprecated use `$client->upsertUsers` instead
337343
* @throws StreamException
338344
*/
@@ -341,7 +347,8 @@ public function updateUsers(array $users): StreamResponse
341347
return $this->upsertUsers($users);
342348
}
343349

344-
/** @link https://getstream.io/chat/docs/php/update_users/?language=php
350+
/** Update a single user.
351+
* @link https://getstream.io/chat/docs/php/update_users/?language=php
345352
* @deprecated use `$client->upsertUser` instead
346353
* @throws StreamException
347354
*/
@@ -869,7 +876,8 @@ public function Channel(string $channelTypeName, ?string $channelId, array $data
869876
return new Channel($this, $channelTypeName, $channelId, $data);
870877
}
871878

872-
/** @deprecated method: use `$client->Channel` instead
879+
/** Returns a Channel object. Don't use it.
880+
* @deprecated method: use `$client->Channel` instead
873881
* @throws StreamException
874882
*/
875883
public function getChannel(string $channelTypeName, string $channelId, array $data = null): Channel
@@ -1303,4 +1311,84 @@ public function listPushProviders(): StreamResponse
13031311
{
13041312
return $this->get("push_providers");
13051313
}
1314+
1315+
/** Create import url
1316+
*
1317+
* A full flow looks like this:
1318+
* ```php
1319+
* $urlResp = $client->createImportUrl('myfile.json');
1320+
* $guzzleClient->put($urlResp['upload_url'], [
1321+
* 'body' => file_get_contents("myfile.json"),
1322+
* 'headers' => ['Content-Type' => 'application/json']
1323+
* ]);
1324+
* $createResp = $client->createImport($urlResp['path'], "upsert");
1325+
* $getResp = $client->getImport($createResp['import_task']['id']);
1326+
* ```
1327+
* @link https://getstream.io/chat/docs/php/import/?language=php
1328+
* @throws StreamException
1329+
*/
1330+
public function createImportUrl(string $filename): StreamResponse
1331+
{
1332+
return $this->post("import_urls", ["filename" => $filename]);
1333+
}
1334+
1335+
/** Create an import. `$mode` can be `upsert` or `insert`.
1336+
*
1337+
* A full flow looks like this:
1338+
* ```php
1339+
* $urlResp = $client->createImportUrl('myfile.json');
1340+
* $guzzleClient->put($urlResp['upload_url'], [
1341+
* 'body' => file_get_contents("myfile.json"),
1342+
* 'headers' => ['Content-Type' => 'application/json']
1343+
* ]);
1344+
* $createResp = $client->createImport($urlResp['path'], "upsert");
1345+
* $getResp = $client->getImport($createResp['import_task']['id']);
1346+
* ```
1347+
* @link https://getstream.io/chat/docs/php/import/?language=php
1348+
* @throws StreamException
1349+
*/
1350+
public function createImport(string $path, string $mode): StreamResponse
1351+
{
1352+
return $this->post("imports", ["path" => $path, "mode" => $mode]);
1353+
}
1354+
1355+
/** Get an import
1356+
*
1357+
* A full flow looks like this:
1358+
* ```php
1359+
* $urlResp = $client->createImportUrl('myfile.json');
1360+
* $guzzleClient->put($urlResp['upload_url'], [
1361+
* 'body' => file_get_contents("myfile.json"),
1362+
* 'headers' => ['Content-Type' => 'application/json']
1363+
* ]);
1364+
* $createResp = $client->createImport($urlResp['path'], "upsert");
1365+
* $getResp = $client->getImport($createResp['import_task']['id']);
1366+
* ```
1367+
* @link https://getstream.io/chat/docs/php/import/?language=php
1368+
* @throws StreamException
1369+
*/
1370+
public function getImport(string $id): StreamResponse
1371+
{
1372+
return $this->get("imports/{$id}");
1373+
}
1374+
1375+
/** List all imports. Options array can contain `limit` and `offset` fields for pagination.
1376+
*
1377+
* A full flow looks like this:
1378+
* ```php
1379+
* $urlResp = $client->createImportUrl('myfile.json');
1380+
* $guzzleClient->put($urlResp['upload_url'], [
1381+
* 'body' => file_get_contents("myfile.json"),
1382+
* 'headers' => ['Content-Type' => 'application/json']
1383+
* ]);
1384+
* $createResp = $client->createImport($urlResp['path'], "upsert");
1385+
* $getResp = $client->getImport($createResp['import_task']['id']);
1386+
* ```
1387+
* @link https://getstream.io/chat/docs/php/import/?language=php
1388+
* @throws StreamException
1389+
*/
1390+
public function listImports(array $options = []): StreamResponse
1391+
{
1392+
return $this->get("imports", $options);
1393+
}
13061394
}

lib/GetStream/StreamChat/StreamException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
*/
1212
class StreamException extends \Exception
1313
{
14-
/**
14+
/** Returns the 'limit' value of the rate limit object.
1515
* @return string|null
1616
*/
1717
public function getRateLimitLimit()
1818
{
1919
return $this->getRateLimitValue("limit");
2020
}
2121

22-
/**
22+
/** Returns the 'remaining' value of the rate limit object.
2323
* @return string|null
2424
*/
2525
public function getRateLimitRemaining()
2626
{
2727
return $this->getRateLimitValue("remaining");
2828
}
2929

30-
/**
30+
/** Returns the 'reset' value of the rate limit object.
3131
* @return string|null
3232
*/
3333
public function getRateLimitReset()

tests/integration/IntegrationTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GetStream\StreamChat\Client;
88
use GetStream\StreamChat\StreamException;
9+
use GuzzleHttp\Client as GuzzleClient;
910
use PHPUnit\Framework\TestCase;
1011

1112
class IntegrationTest extends TestCase
@@ -224,7 +225,7 @@ public function testDeleteUsers()
224225
$response = $this->client->deleteUsers([$user["id"]], ["user" => "hard"]);
225226
$this->assertTrue(array_key_exists("task_id", (array)$response));
226227
$taskId = $response["task_id"];
227-
for ($i = 0; $i < 30; $i++) {
228+
for ($i = 0; $i < 50; $i++) {
228229
$response = $this->client->getTask($taskId);
229230
if ($response["status"] == "completed") {
230231
$this->assertSame($response["result"][$user["id"]]["status"], "ok");
@@ -1148,4 +1149,29 @@ public function testPermissions()
11481149
$response = $this->client->getPermission("read-channel");
11491150
$this->assertEquals("read-channel", $response['permission']['id']);
11501151
}
1152+
1153+
public function testImportEnd2End()
1154+
{
1155+
$urlResp = $this->client->createImportUrl("streamchatphp.json");
1156+
$this->assertNotEmpty($urlResp['upload_url']);
1157+
$this->assertNotEmpty($urlResp['path']);
1158+
1159+
$guzzleClient = new GuzzleClient();
1160+
$resp = $guzzleClient->put($urlResp['upload_url'], [
1161+
'body' => "{}",
1162+
'headers' => [
1163+
'Content-Type' => 'application/json',
1164+
],
1165+
]);
1166+
$this->assertEquals(200, $resp->getStatusCode());
1167+
1168+
$createResp = $this->client->createImport($urlResp['path'], "upsert");
1169+
$this->assertNotEmpty($createResp['import_task']['id']);
1170+
1171+
$getResp = $this->client->getImport($createResp['import_task']['id']);
1172+
$this->assertEquals($createResp['import_task']['id'], $getResp['import_task']['id']);
1173+
1174+
$listResp = $this->client->listImports(['limit' => 1]);
1175+
$this->assertNotEmpty($listResp['import_tasks']);
1176+
}
11511177
}

0 commit comments

Comments
 (0)