Skip to content

Commit e72f104

Browse files
authored
feat: add custom http client possibility (#73)
1 parent 4ffdaa1 commit e72f104

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

.phan/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// that functions removed in php 7.0 exist.
1717
// (See `backward_compatibility_checks` for additional options)
1818
// TODO: Set this.
19-
'target_php_version' => null,
19+
'minimum_target_php_version' => '7.3',
2020

2121
'plugins' => [
2222
'AlwaysReturnPlugin',

lib/GetStream/StreamChat/Client.php

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,11 @@ class Client
3535
*/
3636
protected $apiSecret;
3737

38-
/**
39-
* @var string
40-
*/
41-
protected $location;
42-
4338
/**
4439
* @var string
4540
*/
4641
protected $authToken;
4742

48-
/**
49-
* @var string
50-
*/
51-
public $apiVersion;
52-
53-
/**
54-
* @var float
55-
*/
56-
public $timeout;
57-
5843
/**
5944
* @var array
6045
*/
@@ -66,19 +51,25 @@ class Client
6651
protected $httpRequestHeaders = [];
6752

6853
/**
69-
* @var HandlerStack
54+
* @var GuzzleClient
7055
*/
71-
private $handler;
56+
private $client;
7257

7358
/**
7459
* @param string $apiKey
7560
* @param string $apiSecret
76-
* @param string $apiVersion
77-
* @param string $location
61+
* @param string $apiVersion @deprecated will be removed in the next version
62+
* @param string $location @deprecated will be removed in the next version
7863
* @param float $timeout
7964
*/
80-
public function __construct($apiKey, $apiSecret, $apiVersion='v1.0', $location='us-east', $timeout=null)
65+
public function __construct($apiKey, $apiSecret, $apiVersion=null, $location=null, $timeout=null)
8166
{
67+
if ($apiVersion !== null || $location !== null) {
68+
$warn = "\$apiVersion and \$location parameters are deprecated and will be removed in a future version. ";
69+
$warn .= "Please provide null to suppress this warning.";
70+
trigger_error($warn, E_USER_NOTICE);
71+
}
72+
8273
$this->apiKey = $apiKey ?? getenv("STREAM_KEY");
8374
$this->apiSecret = $apiSecret ?? getenv("STREAM_SECRET");
8475

@@ -87,17 +78,20 @@ public function __construct($apiKey, $apiSecret, $apiVersion='v1.0', $location='
8778
}
8879

8980
if ($timeout != null) {
90-
$this->timeout = $timeout;
81+
$timeout = $timeout;
9182
} elseif (getenv("STREAM_CHAT_TIMEOUT")) {
92-
$this->timeout = floatval(getenv("STREAM_CHAT_TIMEOUT"));
83+
$timeout = floatval(getenv("STREAM_CHAT_TIMEOUT"));
9384
} else {
94-
$this->timeout = 3.0;
85+
$timeout = 3.0;
9586
}
9687

97-
$this->apiVersion = $apiVersion;
98-
$this->location = $location;
9988
$this->authToken = JWT::encode(["server"=>"true"], $this->apiSecret, 'HS256');
100-
$this->handler = HandlerStack::create();
89+
$this->client = new GuzzleClient([
90+
'base_uri' => $this->getBaseUrl(),
91+
'timeout' => $timeout,
92+
'handler' => HandlerStack::create(),
93+
'headers' => ['Accept-Encoding' => 'gzip'],
94+
]);
10195
}
10296

10397
/** Sets the location for the URL. Deprecated, and will be removed in a future version.
@@ -144,17 +138,12 @@ public function buildRequestUrl($uri)
144138
return "{$baseUrl}/{$uri}";
145139
}
146140

147-
/**
148-
* @return \GuzzleHttp\Client
141+
/** Sets the underlying HTTP client. Make sure you set a base_uri.
142+
* @param \GuzzleHttp\Client $client
149143
*/
150-
public function getHttpClient()
144+
public function setHttpClient($client)
151145
{
152-
return new GuzzleClient([
153-
'base_uri' => $this->getBaseUrl(),
154-
'timeout' => $this->timeout,
155-
'handler' => $this->handler,
156-
'headers' => ['Accept-Encoding' => 'gzip'],
157-
]);
146+
$this->client = $client;
158147
}
159148

160149
/** Sets a Guzzle HTTP option that add to the request. See `\GuzzleHttp\RequestOptions`.
@@ -193,7 +182,6 @@ protected function getHttpRequestHeaders()
193182
public function makeHttpRequest($uri, $method, $data = [], $queryParams = [], $multipart = [])
194183
{
195184
$queryParams['api_key'] = $this->apiKey;
196-
$client = $this->getHttpClient();
197185
$headers = $this->getHttpRequestHeaders();
198186

199187
$uri = (new Uri($this->buildRequestUrl($uri)))
@@ -214,7 +202,7 @@ public function makeHttpRequest($uri, $method, $data = [], $queryParams = [], $m
214202
$options['headers'] = $headers;
215203

216204
try {
217-
$response = $client->request($method, $uri, $options);
205+
$response = $this->client->request($method, $uri, $options);
218206
} catch (ClientException $e) {
219207
$response = $e->getResponse();
220208
$msg = $response->getBody()->getContents();

tests/integration/IntegrationTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ class IntegrationTest extends TestCase
1515

1616
protected function setUp():void
1717
{
18-
$this->client = new Client(
19-
getenv('STREAM_KEY'),
20-
getenv('STREAM_SECRET'),
21-
'v1.0',
22-
getenv('STREAM_REGION')
23-
);
18+
$this->client = new Client(getenv('STREAM_KEY'), getenv('STREAM_SECRET'));
2419
$this->client->timeout = 10000;
2520
}
2621

@@ -29,6 +24,22 @@ private function generateGuid()
2924
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
3025
}
3126

27+
public function testHttpClientSet()
28+
{
29+
$client = new Client(getenv('STREAM_KEY'), getenv('STREAM_SECRET'));
30+
31+
$client->setHttpClient(new \GuzzleHttp\Client(['base_uri' => 'https://getstream.io']));
32+
try {
33+
$response = $this->client->getAppSettings();
34+
$this->fail("Expected to throw exception");
35+
} catch (\Exception $e) {
36+
}
37+
38+
$client->setHttpClient(new \GuzzleHttp\Client(['base_uri' => 'https://chat.stream-io-api.com']));
39+
$response = $this->client->getAppSettings();
40+
$this->assertTrue(array_key_exists("app", (array)$response));
41+
}
42+
3243
public function testStreamResponse()
3344
{
3445
$response = $this->client->getAppSettings();
@@ -1026,7 +1037,7 @@ public function testChannelSendAndDeleteFile()
10261037

10271038
public function testChannelSendAndDeleteImage()
10281039
{
1029-
$url = "https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png";
1040+
$url = "https://getstream.io/images/icons/favicon-32x32.png";
10301041
$user = $this->getUser();
10311042
$channel = $this->getChannel();
10321043
$resp = $channel->sendImage($url, "logo.png", $user);

0 commit comments

Comments
 (0)