From 271a1eecba265d4acbfb62d04521bbaa2f48a1c2 Mon Sep 17 00:00:00 2001 From: Hannes Van De Vreken Date: Fri, 6 Apr 2018 17:28:52 +0200 Subject: [PATCH 1/6] Collections and Personalization added --- lib/GetStream/Stream/Client.php | 16 +++ lib/GetStream/Stream/Collections.php | 145 +++++++++++++++++++++++ lib/GetStream/Stream/Personalization.php | 132 +++++++++++++++++++++ 3 files changed, 293 insertions(+) create mode 100644 lib/GetStream/Stream/Collections.php create mode 100644 lib/GetStream/Stream/Personalization.php diff --git a/lib/GetStream/Stream/Client.php b/lib/GetStream/Stream/Client.php index d22db7d..7b1dfa7 100644 --- a/lib/GetStream/Stream/Client.php +++ b/lib/GetStream/Stream/Client.php @@ -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 */ diff --git a/lib/GetStream/Stream/Collections.php b/lib/GetStream/Stream/Collections.php new file mode 100644 index 0000000..37b69bf --- /dev/null +++ b/lib/GetStream/Stream/Collections.php @@ -0,0 +1,145 @@ +apiKey = $apiKey; + $this->apiSecret = $apiSecret; + $this->client = new Client([ + 'base_uri' => $streamClient->getBaseUrl(), + '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) + { + $options = ['json' => ['collection_name' => $collectionName, 'ids' => $ids]]; + + try { + $response = $this->client->request('DELETE', '/meta', $options); + } 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; + } +} diff --git a/lib/GetStream/Stream/Personalization.php b/lib/GetStream/Stream/Personalization.php new file mode 100644 index 0000000..1c01cfd --- /dev/null +++ b/lib/GetStream/Stream/Personalization.php @@ -0,0 +1,132 @@ +apiKey = $apiKey; + $this->apiSecret = $apiSecret; + $this->client = new Client([ + '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; + } +} From 5b38c2e6b63c6f3deb4871ab29ef6fa08e0bf57f Mon Sep 17 00:00:00 2001 From: Hannes Van De Vreken Date: Tue, 10 Apr 2018 17:39:33 +0200 Subject: [PATCH 2/6] Automatically add a trailing slash --- lib/GetStream/Stream/Personalization.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/GetStream/Stream/Personalization.php b/lib/GetStream/Stream/Personalization.php index 1c01cfd..c056217 100644 --- a/lib/GetStream/Stream/Personalization.php +++ b/lib/GetStream/Stream/Personalization.php @@ -89,7 +89,7 @@ private function request($method, $resource, array $params) $queryParams = ['api_key' => $this->apiKey]; $queryParams += $params; - $uri = $resource .'?'. http_build_query($queryParams); + $uri = $resource .'/?'. http_build_query($queryParams); try { $response = $this->client->request($method, $uri); From 54f2045e5ae13d71a73b2cd5a97c0cfb8662803c Mon Sep 17 00:00:00 2001 From: Hannes Van De Vreken Date: Tue, 10 Apr 2018 18:14:43 +0200 Subject: [PATCH 3/6] Trailing slash and api version --- lib/GetStream/Stream/Collections.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/GetStream/Stream/Collections.php b/lib/GetStream/Stream/Collections.php index 37b69bf..7a76ae1 100644 --- a/lib/GetStream/Stream/Collections.php +++ b/lib/GetStream/Stream/Collections.php @@ -36,7 +36,7 @@ public function __construct(StreamClient $streamClient, $apiKey, $apiSecret) $this->apiKey = $apiKey; $this->apiSecret = $apiSecret; $this->client = new Client([ - 'base_uri' => $streamClient->getBaseUrl(), + 'base_uri' => $streamClient->getBaseUrl().'/'.$streamClient->api_version.'/', 'timeout' => $streamClient->timeout, 'handler' => $this->handlerStack(), ]); @@ -57,7 +57,7 @@ public function select($collectionName, array $ids) $queryParams = ['foreign_ids' => join(',', $mappedIds)]; try { - $response = $this->client->request('GET', '/meta?'.http_build_query($queryParams)); + $response = $this->client->request('GET', 'meta/?'.http_build_query($queryParams)); } catch (ClientException $e) { throw new StreamFeedException($e->getResponse()->getBody()); } @@ -78,7 +78,7 @@ public function upsert($collectionName, array $data) $options = ['json' => ['data' => [$collectionName => $data]]]; try { - $response = $this->client->request('POST', '/meta', $options); + $response = $this->client->request('POST', 'meta/', $options); } catch (ClientException $e) { throw new StreamFeedException($e->getResponse()->getBody()); } @@ -99,7 +99,7 @@ public function delete($collectionName, array $ids) $options = ['json' => ['collection_name' => $collectionName, 'ids' => $ids]]; try { - $response = $this->client->request('DELETE', '/meta', $options); + $response = $this->client->request('DELETE', 'meta/', $options); } catch (ClientException $e) { throw new StreamFeedException($e->getResponse()->getBody()); } From c0510f3e23a9648e12dbb25c5508cecc854f3bcb Mon Sep 17 00:00:00 2001 From: Hannes Van De Vreken Date: Tue, 10 Apr 2018 18:27:09 +0200 Subject: [PATCH 4/6] Trying to fix legacy (php 5.5 & 5.6) namespacing issue --- lib/GetStream/Stream/Collections.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/GetStream/Stream/Collections.php b/lib/GetStream/Stream/Collections.php index 7a76ae1..6e17ad9 100644 --- a/lib/GetStream/Stream/Collections.php +++ b/lib/GetStream/Stream/Collections.php @@ -4,7 +4,7 @@ use Firebase\JWT\JWT; use GetStream\Stream\Client as StreamClient; -use GuzzleHttp\Client; +use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\HandlerStack; use Psr\Http\Message\RequestInterface; @@ -35,7 +35,7 @@ public function __construct(StreamClient $streamClient, $apiKey, $apiSecret) { $this->apiKey = $apiKey; $this->apiSecret = $apiSecret; - $this->client = new Client([ + $this->client = new GuzzleClient([ 'base_uri' => $streamClient->getBaseUrl().'/'.$streamClient->api_version.'/', 'timeout' => $streamClient->timeout, 'handler' => $this->handlerStack(), @@ -77,6 +77,8 @@ public function upsert($collectionName, array $data) { $options = ['json' => ['data' => [$collectionName => $data]]]; + var_dump($options); exit; + try { $response = $this->client->request('POST', 'meta/', $options); } catch (ClientException $e) { From 37528d0082fe89a77f2368adeba5b8528f05f25b Mon Sep 17 00:00:00 2001 From: Hannes Van De Vreken Date: Wed, 11 Apr 2018 10:58:00 +0200 Subject: [PATCH 5/6] Same as collections --- lib/GetStream/Stream/Personalization.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/GetStream/Stream/Personalization.php b/lib/GetStream/Stream/Personalization.php index c056217..5f12894 100644 --- a/lib/GetStream/Stream/Personalization.php +++ b/lib/GetStream/Stream/Personalization.php @@ -4,7 +4,7 @@ use Firebase\JWT\JWT; use GetStream\Stream\Client as StreamClient; -use GuzzleHttp\Client; +use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\HandlerStack; use Psr\Http\Message\RequestInterface; @@ -37,7 +37,7 @@ public function __construct(StreamClient $streamClient, $apiKey, $apiSecret) { $this->apiKey = $apiKey; $this->apiSecret = $apiSecret; - $this->client = new Client([ + $this->client = new GuzzleClient([ 'base_uri' => self::API_ENDPOINT, 'timeout' => $streamClient->timeout, 'handler' => $this->handlerStack(), From 1ade2afe081a1b7b315a70448d5e387476e023f5 Mon Sep 17 00:00:00 2001 From: Hannes Van De Vreken Date: Fri, 13 Apr 2018 15:16:00 +0200 Subject: [PATCH 6/6] Change DELETE param spec --- lib/GetStream/Stream/Collections.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/GetStream/Stream/Collections.php b/lib/GetStream/Stream/Collections.php index 6e17ad9..20bb132 100644 --- a/lib/GetStream/Stream/Collections.php +++ b/lib/GetStream/Stream/Collections.php @@ -77,8 +77,6 @@ public function upsert($collectionName, array $data) { $options = ['json' => ['data' => [$collectionName => $data]]]; - var_dump($options); exit; - try { $response = $this->client->request('POST', 'meta/', $options); } catch (ClientException $e) { @@ -98,10 +96,11 @@ public function upsert($collectionName, array $data) */ public function delete($collectionName, array $ids) { - $options = ['json' => ['collection_name' => $collectionName, 'ids' => $ids]]; + $ids = join(',', $ids); + $queryParams = ['collection_name' => $collectionName, 'ids' => $ids]; try { - $response = $this->client->request('DELETE', 'meta/', $options); + $response = $this->client->request('DELETE', 'meta/?'.http_build_query($queryParams)); } catch (ClientException $e) { throw new StreamFeedException($e->getResponse()->getBody()); }