-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from GetStream/feature/personalization
Collections and Personalization added
- Loading branch information
Showing
3 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
namespace GetStream\Stream; | ||
|
||
use Firebase\JWT\JWT; | ||
use GetStream\Stream\Client as StreamClient; | ||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\HandlerStack; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class Collections | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $apiKey; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $apiSecret; | ||
|
||
/** | ||
* @param \GetStream\Stream\Client $streamClient | ||
* @param string $apiKey | ||
* @param string $apiSecret | ||
*/ | ||
public function __construct(StreamClient $streamClient, $apiKey, $apiSecret) | ||
{ | ||
$this->apiKey = $apiKey; | ||
$this->apiSecret = $apiSecret; | ||
$this->client = new GuzzleClient([ | ||
'base_uri' => $streamClient->getBaseUrl().'/'.$streamClient->api_version.'/', | ||
'timeout' => $streamClient->timeout, | ||
'handler' => $this->handlerStack(), | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $collectionName | ||
* @param array $ids | ||
* | ||
* @return array | ||
*/ | ||
public function select($collectionName, array $ids) | ||
{ | ||
$mappedIds = array_map(function ($id) use ($collectionName) { | ||
return sprintf('%s:%s', $collectionName, $id); | ||
}, $ids); | ||
|
||
$queryParams = ['foreign_ids' => join(',', $mappedIds)]; | ||
|
||
try { | ||
$response = $this->client->request('GET', 'meta/?'.http_build_query($queryParams)); | ||
} catch (ClientException $e) { | ||
throw new StreamFeedException($e->getResponse()->getBody()); | ||
} | ||
|
||
$body = $response->getBody()->getContents(); | ||
|
||
return json_decode($body, true); | ||
} | ||
|
||
/** | ||
* @param string $collectionName | ||
* @param array $data | ||
* | ||
* @return array | ||
*/ | ||
public function upsert($collectionName, array $data) | ||
{ | ||
$options = ['json' => ['data' => [$collectionName => $data]]]; | ||
|
||
try { | ||
$response = $this->client->request('POST', 'meta/', $options); | ||
} catch (ClientException $e) { | ||
throw new StreamFeedException($e->getResponse()->getBody()); | ||
} | ||
|
||
$body = $response->getBody()->getContents(); | ||
|
||
return json_decode($body, true); | ||
} | ||
|
||
/** | ||
* @param string $collectionName | ||
* @param array $ids | ||
* | ||
* @return array | ||
*/ | ||
public function delete($collectionName, array $ids) | ||
{ | ||
$ids = join(',', $ids); | ||
$queryParams = ['collection_name' => $collectionName, 'ids' => $ids]; | ||
|
||
try { | ||
$response = $this->client->request('DELETE', 'meta/?'.http_build_query($queryParams)); | ||
} catch (ClientException $e) { | ||
throw new StreamFeedException($e->getResponse()->getBody()); | ||
} | ||
|
||
$body = $response->getBody()->getContents(); | ||
|
||
return json_decode($body, true); | ||
} | ||
|
||
/** | ||
* @return HandlerStack | ||
*/ | ||
private function handlerStack() | ||
{ | ||
$token = JWT::encode([ | ||
'action' => '*', | ||
'user_id' => '*', | ||
'feed_id' => '*', | ||
'resource' => 'collections', | ||
], $this->apiSecret, 'HS256'); | ||
|
||
$stack = HandlerStack::create(); | ||
$stack->push(function (callable $handler) use ($token) { | ||
return function (RequestInterface $request, array $options) use ($handler, $token) { | ||
// Add authentication headers. | ||
$request = $request | ||
->withAddedHeader('Authorization', $token) | ||
->withAddedHeader('Stream-Auth-Type', 'jwt') | ||
->withAddedHeader('Content-Type', 'application/json') | ||
->withAddedHeader('X-Stream-Client', 'stream-php-client-' . VERSION); | ||
|
||
// Add a api_key query param. | ||
$queryParams = \GuzzleHttp\Psr7\parse_query($request->getUri()->getQuery()); | ||
$query = http_build_query($queryParams + ['api_key' => $this->apiKey]); | ||
$request = $request->withUri($request->getUri()->withQuery($query)); | ||
|
||
return $handler($request, $options); | ||
}; | ||
}); | ||
|
||
return $stack; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace GetStream\Stream; | ||
|
||
use Firebase\JWT\JWT; | ||
use GetStream\Stream\Client as StreamClient; | ||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\HandlerStack; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class Personalization | ||
{ | ||
const API_ENDPOINT = 'https://personalization.stream-io-api.com/personalization/v1.0/'; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $apiKey; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $apiSecret; | ||
|
||
/** | ||
* @param \GetStream\Stream\Client $streamClient | ||
* @param string $apiKey | ||
* @param string $apiSecret | ||
*/ | ||
public function __construct(StreamClient $streamClient, $apiKey, $apiSecret) | ||
{ | ||
$this->apiKey = $apiKey; | ||
$this->apiSecret = $apiSecret; | ||
$this->client = new GuzzleClient([ | ||
'base_uri' => self::API_ENDPOINT, | ||
'timeout' => $streamClient->timeout, | ||
'handler' => $this->handlerStack(), | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $resource | ||
* @param array $params | ||
* | ||
* @return array | ||
*/ | ||
public function get($resource, array $params) | ||
{ | ||
return $this->request('GET', $resource, $params); | ||
} | ||
|
||
/** | ||
* @param string $resource | ||
* @param array $params | ||
* | ||
* @return array | ||
*/ | ||
public function post($resource, array $params) | ||
{ | ||
return $this->request('POST', $resource, $params); | ||
} | ||
|
||
/** | ||
* @param string $resource | ||
* @param array $params | ||
* | ||
* @return array | ||
*/ | ||
public function delete($resource, array $params) | ||
{ | ||
return $this->request('DELETE', $resource, $params); | ||
} | ||
|
||
/** | ||
* @param string $method | ||
* @param string $resource | ||
* @param array $params | ||
* | ||
* @return array | ||
*/ | ||
private function request($method, $resource, array $params) | ||
{ | ||
$queryParams = ['api_key' => $this->apiKey]; | ||
$queryParams += $params; | ||
|
||
$uri = $resource .'/?'. http_build_query($queryParams); | ||
|
||
try { | ||
$response = $this->client->request($method, $uri); | ||
} catch (ClientException $e) { | ||
throw new StreamFeedException($e->getResponse()->getBody()); | ||
} | ||
|
||
$body = $response->getBody()->getContents(); | ||
|
||
return json_decode($body, true); | ||
} | ||
|
||
/** | ||
* @return HandlerStack | ||
*/ | ||
private function handlerStack() | ||
{ | ||
$token = JWT::encode([ | ||
'action' => '*', | ||
'user_id' => '*', | ||
'feed_id' => '*', | ||
'resource' => 'personalization', | ||
], $this->apiSecret, 'HS256'); | ||
|
||
$stack = HandlerStack::create(); | ||
$stack->push(function (callable $handler) use ($token) { | ||
return function (RequestInterface $request, array $options) use ($handler, $token) { | ||
$request = $request | ||
->withAddedHeader('Authorization', $token) | ||
->withAddedHeader('Stream-Auth-Type', 'jwt') | ||
->withAddedHeader('Content-Type', 'application/json') | ||
->withAddedHeader('X-Stream-Client', 'stream-php-client-' . VERSION); | ||
|
||
return $handler($request, $options); | ||
}; | ||
}); | ||
|
||
return $stack; | ||
} | ||
} |