Skip to content

Commit

Permalink
Merge pull request #64 from GetStream/feature/personalization
Browse files Browse the repository at this point in the history
Collections and Personalization added
  • Loading branch information
tbarbugli committed Apr 16, 2018
2 parents 69dc716 + 1ade2af commit 53ac454
Show file tree
Hide file tree
Showing 3 changed files with 294 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/GetStream/Stream/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ public function batcher()
return new Batcher($this, $this->signer, $this->api_key);
}

/**
* @return Personalization
*/
public function personalization()
{
return new Personalization($this, $this->api_key, $this->api_secret);
}

/**
* @return Collections
*/
public function collections()
{
return new Collections($this, $this->api_key, $this->api_secret);
}

/**
* @return string
*/
Expand Down
146 changes: 146 additions & 0 deletions lib/GetStream/Stream/Collections.php
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;
}
}
132 changes: 132 additions & 0 deletions lib/GetStream/Stream/Personalization.php
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;
}
}

0 comments on commit 53ac454

Please sign in to comment.