Skip to content

Commit

Permalink
feat(imports): add import endpoints (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme authored May 18, 2022
1 parent 8f8bb0b commit fd209e5
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 20 deletions.
2 changes: 0 additions & 2 deletions .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
],

'suppress_issue_types' => [
'PhanPluginDescriptionlessCommentOnPublicMethod',
'PhanPluginDescriptionlessCommentOnProtectedProperty',
'PhanPluginDescriptionlessCommentOnPublicProperty',
'PhanPluginDescriptionlessCommentOnPrivateMethod',
'PhanPluginDescriptionlessCommentOnPrivateProperty',
'PhanPluginDescriptionlessCommentOnProtectedMethod',
Expand Down
8 changes: 4 additions & 4 deletions lib/GetStream/StreamChat/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*/
class Channel
{
/**
/** Channel type of the channel.
* @var string
*/
public $channelType;

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

/** @link https://getstream.io/chat/docs/php/muting_channels/?language=php
* Unmutes the channel for the given user.
/** Unmutes the channel for the given user.
* @link https://getstream.io/chat/docs/php/muting_channels/?language=php
* @throws StreamException
*/
public function unmute(string $userId): StreamResponse
Expand Down
108 changes: 98 additions & 10 deletions lib/GetStream/StreamChat/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function getBaseUrl(): string
return "https://chat.stream-io-api.com";
}

/**
/** For internal usage only.
* @internal
*/
public function buildRequestUrl(string $uri): string
Expand Down Expand Up @@ -163,7 +163,7 @@ private function getHttpRequestHeaders(): array
/**
* @throws StreamException
*/
public function makeHttpRequest(string $uri, string $method, $data = [], array $queryParams = [], array $multipart = []): StreamResponse
private function makeHttpRequest(string $uri, string $method, $data = [], array $queryParams = [], array $multipart = []): StreamResponse
{
$queryParams['api_key'] = $this->apiKey;
$headers = $this->getHttpRequestHeaders();
Expand Down Expand Up @@ -232,39 +232,44 @@ public function createToken(string $userId, int $expiration = null, int $issuedA
return $this->jwtHandler->encode($this->apiSecret, $payload);
}

/**
/** For internal usage only.
* @internal
* @throws StreamException
*/
public function get(string $uri, array $queryParams = []): StreamResponse
{
return $this->makeHttpRequest($uri, "GET", [], $queryParams);
}

/**
/** For internal usage only.
* @internal
* @throws StreamException
*/
public function delete(string $uri, array $queryParams = []): StreamResponse
{
return $this->makeHttpRequest($uri, "DELETE", [], $queryParams);
}

/**
/** For internal usage only.
* @internal
* @throws StreamException
*/
public function patch(string $uri, array $data, array $queryParams = []): StreamResponse
{
return $this->makeHttpRequest($uri, "PATCH", $data, $queryParams);
}

/**
/** For internal usage only.
* @internal
* @throws StreamException
*/
public function post(string $uri, $data, array $queryParams = []): StreamResponse
{
return $this->makeHttpRequest($uri, "POST", $data, $queryParams);
}

/**
/** For internal usage only.
* @internal
* @throws StreamException
*/
public function put(string $uri, array $data, array $queryParams = []): StreamResponse
Expand Down Expand Up @@ -332,7 +337,8 @@ public function upsertUser(array $user): StreamResponse
return $this->upsertUsers([$user]);
}

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

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

/** @deprecated method: use `$client->Channel` instead
/** Returns a Channel object. Don't use it.
* @deprecated method: use `$client->Channel` instead
* @throws StreamException
*/
public function getChannel(string $channelTypeName, string $channelId, array $data = null): Channel
Expand Down Expand Up @@ -1303,4 +1311,84 @@ public function listPushProviders(): StreamResponse
{
return $this->get("push_providers");
}

/** Create import url
*
* A full flow looks like this:
* ```php
* $urlResp = $client->createImportUrl('myfile.json');
* $guzzleClient->put($urlResp['upload_url'], [
* 'body' => file_get_contents("myfile.json"),
* 'headers' => ['Content-Type' => 'application/json']
* ]);
* $createResp = $client->createImport($urlResp['path'], "upsert");
* $getResp = $client->getImport($createResp['import_task']['id']);
* ```
* @link https://getstream.io/chat/docs/php/import/?language=php
* @throws StreamException
*/
public function createImportUrl(string $filename): StreamResponse
{
return $this->post("import_urls", ["filename" => $filename]);
}

/** Create an import. `$mode` can be `upsert` or `insert`.
*
* A full flow looks like this:
* ```php
* $urlResp = $client->createImportUrl('myfile.json');
* $guzzleClient->put($urlResp['upload_url'], [
* 'body' => file_get_contents("myfile.json"),
* 'headers' => ['Content-Type' => 'application/json']
* ]);
* $createResp = $client->createImport($urlResp['path'], "upsert");
* $getResp = $client->getImport($createResp['import_task']['id']);
* ```
* @link https://getstream.io/chat/docs/php/import/?language=php
* @throws StreamException
*/
public function createImport(string $path, string $mode): StreamResponse
{
return $this->post("imports", ["path" => $path, "mode" => $mode]);
}

/** Get an import
*
* A full flow looks like this:
* ```php
* $urlResp = $client->createImportUrl('myfile.json');
* $guzzleClient->put($urlResp['upload_url'], [
* 'body' => file_get_contents("myfile.json"),
* 'headers' => ['Content-Type' => 'application/json']
* ]);
* $createResp = $client->createImport($urlResp['path'], "upsert");
* $getResp = $client->getImport($createResp['import_task']['id']);
* ```
* @link https://getstream.io/chat/docs/php/import/?language=php
* @throws StreamException
*/
public function getImport(string $id): StreamResponse
{
return $this->get("imports/{$id}");
}

/** List all imports. Options array can contain `limit` and `offset` fields for pagination.
*
* A full flow looks like this:
* ```php
* $urlResp = $client->createImportUrl('myfile.json');
* $guzzleClient->put($urlResp['upload_url'], [
* 'body' => file_get_contents("myfile.json"),
* 'headers' => ['Content-Type' => 'application/json']
* ]);
* $createResp = $client->createImport($urlResp['path'], "upsert");
* $getResp = $client->getImport($createResp['import_task']['id']);
* ```
* @link https://getstream.io/chat/docs/php/import/?language=php
* @throws StreamException
*/
public function listImports(array $options = []): StreamResponse
{
return $this->get("imports", $options);
}
}
6 changes: 3 additions & 3 deletions lib/GetStream/StreamChat/StreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
*/
class StreamException extends \Exception
{
/**
/** Returns the 'limit' value of the rate limit object.
* @return string|null
*/
public function getRateLimitLimit()
{
return $this->getRateLimitValue("limit");
}

/**
/** Returns the 'remaining' value of the rate limit object.
* @return string|null
*/
public function getRateLimitRemaining()
{
return $this->getRateLimitValue("remaining");
}

/**
/** Returns the 'reset' value of the rate limit object.
* @return string|null
*/
public function getRateLimitReset()
Expand Down
28 changes: 27 additions & 1 deletion tests/integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GetStream\StreamChat\Client;
use GetStream\StreamChat\StreamException;
use GuzzleHttp\Client as GuzzleClient;
use PHPUnit\Framework\TestCase;

class IntegrationTest extends TestCase
Expand Down Expand Up @@ -224,7 +225,7 @@ public function testDeleteUsers()
$response = $this->client->deleteUsers([$user["id"]], ["user" => "hard"]);
$this->assertTrue(array_key_exists("task_id", (array)$response));
$taskId = $response["task_id"];
for ($i = 0; $i < 30; $i++) {
for ($i = 0; $i < 50; $i++) {
$response = $this->client->getTask($taskId);
if ($response["status"] == "completed") {
$this->assertSame($response["result"][$user["id"]]["status"], "ok");
Expand Down Expand Up @@ -1148,4 +1149,29 @@ public function testPermissions()
$response = $this->client->getPermission("read-channel");
$this->assertEquals("read-channel", $response['permission']['id']);
}

public function testImportEnd2End()
{
$urlResp = $this->client->createImportUrl("streamchatphp.json");
$this->assertNotEmpty($urlResp['upload_url']);
$this->assertNotEmpty($urlResp['path']);

$guzzleClient = new GuzzleClient();
$resp = $guzzleClient->put($urlResp['upload_url'], [
'body' => "{}",
'headers' => [
'Content-Type' => 'application/json',
],
]);
$this->assertEquals(200, $resp->getStatusCode());

$createResp = $this->client->createImport($urlResp['path'], "upsert");
$this->assertNotEmpty($createResp['import_task']['id']);

$getResp = $this->client->getImport($createResp['import_task']['id']);
$this->assertEquals($createResp['import_task']['id'], $getResp['import_task']['id']);

$listResp = $this->client->listImports(['limit' => 1]);
$this->assertNotEmpty($listResp['import_tasks']);
}
}

0 comments on commit fd209e5

Please sign in to comment.