Skip to content

Commit 1c3908f

Browse files
committed
Use one ApiCall object per Client instance
1 parent fe0523f commit 1c3908f

13 files changed

Lines changed: 196 additions & 206 deletions

src/Alias.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Typesense;
44

5+
use GuzzleHttp\Exception\GuzzleException;
6+
use Typesense\Exceptions\TypesenseClientError;
57
use Typesense\Lib\Configuration;
68

79
/**
@@ -14,32 +16,26 @@
1416
class Alias
1517
{
1618

17-
/**
18-
* @var \Typesense\Lib\Configuration
19-
*/
20-
private Configuration $config;
21-
2219
/**
2320
* @var string
2421
*/
2522
private string $name;
2623

2724
/**
28-
* @var \Typesense\ApiCall
25+
* @var ApiCall
2926
*/
3027
private ApiCall $apiCall;
3128

3229
/**
3330
* Alias constructor.
3431
*
35-
* @param \Typesense\Lib\Configuration $config
3632
* @param string $name
33+
* @param ApiCall $apiCall
3734
*/
38-
public function __construct(Configuration $config, string $name)
35+
public function __construct(string $name, ApiCall $apiCall)
3936
{
40-
$this->config = $config;
4137
$this->name = $name;
42-
$this->apiCall = new ApiCall($this->config);
38+
$this->apiCall = $apiCall;
4339
}
4440

4541
/**
@@ -52,7 +48,7 @@ public function endPointPath(): string
5248

5349
/**
5450
* @return array
55-
* @throws \Typesense\Exceptions\TypesenseClientError|\GuzzleHttp\Exception\GuzzleException
51+
* @throws TypesenseClientError|GuzzleException
5652
*/
5753
public function retrieve(): array
5854
{
@@ -61,7 +57,7 @@ public function retrieve(): array
6157

6258
/**
6359
* @return array
64-
* @throws \Typesense\Exceptions\TypesenseClientError|\GuzzleHttp\Exception\GuzzleException
60+
* @throws TypesenseClientError|GuzzleException
6561
*/
6662
public function delete(): array
6763
{

src/Aliases.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Typesense;
44

5+
use GuzzleHttp\Exception\GuzzleException;
6+
use Typesense\Exceptions\TypesenseClientError;
57
use Typesense\Lib\Configuration;
68

79
/**
@@ -17,12 +19,7 @@ class Aliases implements \ArrayAccess
1719
public const RESOURCE_PATH = '/aliases';
1820

1921
/**
20-
* @var \Typesense\Lib\Configuration
21-
*/
22-
private Configuration $config;
23-
24-
/**
25-
* @var \Typesense\ApiCall
22+
* @var ApiCall
2623
*/
2724
private ApiCall $apiCall;
2825

@@ -34,12 +31,11 @@ class Aliases implements \ArrayAccess
3431
/**
3532
* Aliases constructor.
3633
*
37-
* @param \Typesense\Lib\Configuration $config
34+
* @param ApiCall $apiCall
3835
*/
39-
public function __construct(Configuration $config)
36+
public function __construct(ApiCall $apiCall)
4037
{
41-
$this->config = $config;
42-
$this->apiCall = new ApiCall($this->config);
38+
$this->apiCall = $apiCall;
4339
}
4440

4541
/**
@@ -57,7 +53,7 @@ public function endPointPath(string $aliasName): string
5753
* @param array $mapping
5854
*
5955
* @return array
60-
* @throws \Typesense\Exceptions\TypesenseClientError|\GuzzleHttp\Exception\GuzzleException
56+
* @throws TypesenseClientError|GuzzleException
6157
*/
6258
public function upsert(string $name, array $mapping): array
6359
{
@@ -66,7 +62,7 @@ public function upsert(string $name, array $mapping): array
6662

6763
/**
6864
* @return array
69-
* @throws \Typesense\Exceptions\TypesenseClientError|\GuzzleHttp\Exception\GuzzleException
65+
* @throws TypesenseClientError|GuzzleException
7066
*/
7167
public function retrieve(): array
7268
{
@@ -85,7 +81,7 @@ public function __get($name)
8581
}
8682

8783
if (!isset($this->aliases[$name])) {
88-
$this->aliases[$name] = new Alias($this->config, $name);
84+
$this->aliases[$name] = new Alias($name, $this->apiCall);
8985
}
9086

9187
return $this->aliases[$name];
@@ -105,7 +101,7 @@ public function offsetExists($offset): bool
105101
public function offsetGet($offset)
106102
{
107103
if (!isset($this->aliases[$offset])) {
108-
$this->aliases[$offset] = new Alias($this->config, $offset);
104+
$this->aliases[$offset] = new Alias($offset, $this->apiCall);
109105
}
110106

111107
return $this->aliases[$offset];

src/ApiCall.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Typesense;
44

5+
use Exception;
6+
use GuzzleHttp\Exception\GuzzleException;
57
use Typesense\Lib\Node;
68
use Typesense\Lib\Configuration;
79
use GuzzleHttp\Exception\ClientException;
@@ -34,17 +36,17 @@ class ApiCall
3436
private \GuzzleHttp\Client $client;
3537

3638
/**
37-
* @var \Typesense\Lib\Configuration
39+
* @var Configuration
3840
*/
3941
private Configuration $config;
4042

4143
/**
42-
* @var array|\Typesense\Lib\Node[]
44+
* @var array|Node[]
4345
*/
4446
private static array $nodes;
4547

4648
/**
47-
* @var \Typesense\Lib\Node|null
49+
* @var Node|null
4850
*/
4951
private static ?Node $nearestNode;
5052

@@ -56,7 +58,7 @@ class ApiCall
5658
/**
5759
* ApiCall constructor.
5860
*
59-
* @param \Typesense\Lib\Configuration $config
61+
* @param Configuration $config
6062
*/
6163
public function __construct(Configuration $config)
6264
{
@@ -88,8 +90,8 @@ private function initializeNodes(): void
8890
* @param bool $asJson
8991
*
9092
* @return string|array
91-
* @throws \Typesense\Exceptions\TypesenseClientError
92-
* @throws \Exception|\GuzzleHttp\Exception\GuzzleException
93+
* @throws TypesenseClientError
94+
* @throws Exception|GuzzleException
9395
*/
9496
public function get(string $endPoint, array $params, bool $asJson = true)
9597
{
@@ -105,8 +107,8 @@ public function get(string $endPoint, array $params, bool $asJson = true)
105107
* @param bool $asJson
106108
*
107109
* @return array|string
108-
* @throws \Typesense\Exceptions\TypesenseClientError
109-
* @throws \GuzzleHttp\Exception\GuzzleException
110+
* @throws TypesenseClientError
111+
* @throws GuzzleException
110112
*/
111113
public function post(string $endPoint, $body, bool $asJson = true)
112114
{
@@ -120,7 +122,7 @@ public function post(string $endPoint, $body, bool $asJson = true)
120122
* @param array $body
121123
*
122124
* @return array
123-
* @throws \Typesense\Exceptions\TypesenseClientError|\GuzzleHttp\Exception\GuzzleException
125+
* @throws TypesenseClientError|GuzzleException
124126
*/
125127
public function put(string $endPoint, array $body): array
126128
{
@@ -133,7 +135,7 @@ public function put(string $endPoint, array $body): array
133135
* @param string $endPoint
134136
*
135137
* @return array
136-
* @throws \Typesense\Exceptions\TypesenseClientError|\GuzzleHttp\Exception\GuzzleException
138+
* @throws TypesenseClientError|GuzzleException
137139
*/
138140
public function delete(string $endPoint): array
139141
{
@@ -149,13 +151,13 @@ public function delete(string $endPoint): array
149151
* @param array $options
150152
*
151153
* @return string|array
152-
* @throws \Typesense\Exceptions\TypesenseClientError|\GuzzleHttp\Exception\GuzzleException
153-
* @throws \Exception
154+
* @throws TypesenseClientError|GuzzleException
155+
* @throws Exception
154156
*/
155157
private function makeRequest(string $method, string $endPoint, bool $asJson, array $options)
156158
{
157-
$numRetries = 0;
158-
$last_exception = null;
159+
$numRetries = 0;
160+
$lastException = null;
159161
while ($numRetries < $this->config->getNumRetries() + 1) {
160162
$numRetries++;
161163
$node = $this->getNode();
@@ -209,15 +211,15 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
209211
} catch (TypesenseClientError $exception) {
210212
$this->setNodeHealthCheck($node, false);
211213
throw $exception;
212-
} catch (\Exception $exception) {
214+
} catch (Exception $exception) {
213215
$this->setNodeHealthCheck($node, false);
214-
$last_exception = $exception;
216+
$lastException = $exception;
215217
sleep($this->config->getRetryIntervalSeconds());
216218
}
217219
}
218220

219-
if ($last_exception) {
220-
throw $last_exception;
221+
if ($lastException) {
222+
throw $lastException;
221223
}
222224
}
223225

@@ -235,23 +237,18 @@ private function getRequestOptions(): array
235237
}
236238

237239
/**
238-
* @param \Typesense\Lib\Node $node
240+
* @param Node $node
239241
*
240242
* @return bool
241243
*/
242244
private function nodeDueForHealthCheck(Node $node): bool
243245
{
244246
$currentTimestamp = time();
245-
$checkNode = ($currentTimestamp - $node->getLastAccessTs()) > $this->config->getHealthCheckIntervalSeconds();
246-
if ($checkNode) {
247-
//
248-
}
249-
250-
return $checkNode;
247+
return ($currentTimestamp - $node->getLastAccessTs()) > $this->config->getHealthCheckIntervalSeconds();
251248
}
252249

253250
/**
254-
* @param \Typesense\Lib\Node $node
251+
* @param Node $node
255252
* @param bool $isHealthy
256253
*/
257254
public function setNodeHealthCheck(Node $node, bool $isHealthy): void
@@ -264,7 +261,7 @@ public function setNodeHealthCheck(Node $node, bool $isHealthy): void
264261
* Returns a healthy host from the pool in a round-robin fashion
265262
* Might return an unhealthy host periodically to check for recovery.
266263
*
267-
* @return \Typesense\Lib\Node
264+
* @return Node
268265
*/
269266
public function getNode(): Lib\Node
270267
{
@@ -293,7 +290,7 @@ public function getNode(): Lib\Node
293290
/**
294291
* @param int $httpCode
295292
*
296-
* @return \Typesense\Exceptions\TypesenseClientError
293+
* @return TypesenseClientError
297294
*/
298295
public function getException(int $httpCode): TypesenseClientError
299296
{

src/Client.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Typesense;
44

5+
use Typesense\Exceptions\ConfigError;
56
use Typesense\Lib\Configuration;
67

78
/**
@@ -15,58 +16,65 @@ class Client
1516
{
1617

1718
/**
18-
* @var \Typesense\Lib\Configuration
19+
* @var Configuration
1920
*/
2021
private Configuration $config;
2122

2223
/**
23-
* @var \Typesense\Collections
24+
* @var Collections
2425
*/
2526
public Collections $collections;
2627

2728
/**
28-
* @var \Typesense\Aliases
29+
* @var Aliases
2930
*/
3031
public Aliases $aliases;
3132

3233
/**
33-
* @var \Typesense\Keys
34+
* @var Keys
3435
*/
3536
public Keys $keys;
3637

38+
/**
39+
* @var ApiCall
40+
*/
41+
private ApiCall $apiCall;
42+
3743
/**
3844
* Client constructor.
3945
*
4046
* @param array $config
4147
*
42-
* @throws \Typesense\Exceptions\ConfigError
48+
* @throws ConfigError
4349
*/
4450
public function __construct(array $config)
4551
{
46-
$this->config = new Configuration($config);
47-
$this->collections = new Collections($this->config);
48-
$this->aliases = new Aliases($this->config);
49-
$this->keys = new Keys($this->config);
52+
$this->config = new Configuration($config);
53+
$this->apiCall = new ApiCall($this->config);
54+
55+
$this->collections = new Collections($this->apiCall);
56+
$this->aliases = new Aliases($this->apiCall);
57+
$this->keys = new Keys($this->apiCall);
5058
}
5159

5260
/**
53-
* @return \Typesense\Collections
61+
* @return Collections
5462
*/
5563
public function getCollections(): Collections
5664
{
5765
return $this->collections;
5866
}
5967

6068
/**
61-
* @return \Typesense\Aliases
69+
* @return Aliases
6270
*/
6371
public function getAliases(): Aliases
6472
{
6573
return $this->aliases;
6674
}
6775

6876
/**
69-
* @return \Typesense\Keys
77+
* @return Keys
7078
*/
7179
public function getKeys(): Keys
7280
{

0 commit comments

Comments
 (0)