Skip to content

Commit

Permalink
feat(guzzle): add custom http middleware possibility (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme authored May 27, 2022
1 parent 971f6a6 commit 6960b3f
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ $client = new GetStream\Stream\Client('YOUR_API_KEY', 'YOUR_API_SECRET');

// Instantiate a feed object
$userFeed = $client->feed('user', '1');

// If you want, you can set a custom http handler
$handler = new \GuzzleHttp\HandlerStack();
$stack->push(Middleware::mapRequest(function (RequestInterface $r) {
echo 'Sending request to Stream Feeds API: ' . $r->getUri() . PHP_EOL;
return $r;
}));
$client->setCustomHttpHandler($handler);
```

By default, the Client will target the GetStream REST API at `stream-io-api.com/api`.
Expand Down
6 changes: 5 additions & 1 deletion lib/GetStream/Stream/BaseFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,11 @@ public function getHandlerStack()
*/
public function getHttpClient()
{
$handler = $this->getHandlerStack();
$handler = $this->client->getCustomHttpHandlerStack();
if (!$handler) {
$handler = $this->getHandlerStack();
}

return new GuzzleClient([
'base_uri' => $this->client->getBaseUrl(),
'timeout' => $this->client->timeout,
Expand Down
23 changes: 23 additions & 0 deletions lib/GetStream/Stream/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use DateTime;
use Exception;
use GuzzleHttp\HandlerStack;

class Client implements ClientInterface
{
Expand Down Expand Up @@ -43,6 +44,11 @@ class Client implements ClientInterface
*/
public $timeout;

/**
* @var HandlerStack|null
*/
public $customHttpHandler;

/**
* @param string $api_key
* @param string $api_secret
Expand All @@ -61,6 +67,23 @@ public function __construct($api_key, $api_secret, $api_version='v1.0', $locatio
$this->protocol = 'https';
}

/**
* @param HandlerStack $handler
* @return void
*/
public function setCustomHttpHandler($handler)
{
$this->customHttpHandler = $handler;
}

/**
* @return HandlerStack|null
*/
public function getCustomHttpHandlerStack()
{
return $this->customHttpHandler;
}

/**
* @param string|null $url
* @return Client
Expand Down
7 changes: 7 additions & 0 deletions lib/GetStream/Stream/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace GetStream\Stream;

use GuzzleHttp\HandlerStack;

/**
* @property string $timeout
* @property string $api_version
Expand All @@ -18,6 +20,11 @@ public function setProtocol($protocol);
*/
public function setLocation($location);

/**
* @return HandlerStack|null
*/
public function getCustomHttpHandlerStack();

/**
* @param string $user_id
* @param ?array $extra_data
Expand Down
2 changes: 1 addition & 1 deletion lib/GetStream/Stream/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(ClientInterface $streamClient, $apiKey, $apiSecret)
$this->client = new GuzzleClient([
'base_uri' => $streamClient->getBaseUrl().'/'.$streamClient->api_version.'/',
'timeout' => $streamClient->timeout,
'handler' => Util::handlerStack($apiKey, $apiSecret, 'collections'),
'handler' => Util::handlerStack($streamClient, $apiKey, $apiSecret, 'collections'),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/GetStream/Stream/Personalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(ClientInterface $streamClient, $apiKey, $apiSecret)
$this->client = new GuzzleClient([
'base_uri' => self::API_ENDPOINT,
'timeout' => $streamClient->timeout,
'handler' => Util::handlerStack($apiKey, $apiSecret, 'personalization'),
'handler' => Util::handlerStack($streamClient, $apiKey, $apiSecret, 'personalization'),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/GetStream/Stream/Reactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(ClientInterface $streamClient, $apiKey, $apiSecret)
$this->client = new GuzzleClient([
'base_uri' => $streamClient->getBaseUrl().'/'.$streamClient->api_version.'/',
'timeout' => $streamClient->timeout,
'handler' => Util::handlerStack($apiKey, $apiSecret, 'reactions'),
'handler' => Util::handlerStack($streamClient, $apiKey, $apiSecret, 'reactions'),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/GetStream/Stream/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(ClientInterface $streamClient, $apiKey, $apiSecret)
$this->client = new GuzzleClient([
'base_uri' => $streamClient->getBaseUrl().'/'.$streamClient->api_version.'/',
'timeout' => $streamClient->timeout,
'handler' => Util::handlerStack($apiKey, $apiSecret, 'users'),
'handler' => Util::handlerStack($streamClient, $apiKey, $apiSecret, 'users'),
]);
}

Expand Down
13 changes: 11 additions & 2 deletions lib/GetStream/Stream/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@
class Util
{
/**
* @param ClientInterface $streamClient
* @param string $apiKey
* @param string $apiSecret
* @param string $resource
* @return HandlerStack
*/
public static function handlerStack($apiKey, $apiSecret, $resource)
public static function handlerStack($streamClient, $apiKey, $apiSecret, $resource)
{
$token = JWT::encode([
'action' => '*',
'user_id' => '*',
'feed_id' => '*',
'resource' => $resource,
], $apiSecret, 'HS256');
$stack = HandlerStack::create();


$stack = $streamClient->getCustomHttpHandlerStack();
if (!$stack) {
$stack = HandlerStack::create();
}
$stack->push(function (callable $handler) use ($token, $apiKey) {
return function (RequestInterface $request, array $options) use ($handler, $token, $apiKey) {
// Add authentication headers.
Expand Down

0 comments on commit 6960b3f

Please sign in to comment.