Skip to content

Commit 5391649

Browse files
author
Jan Petr
authored
Merge pull request #807 from algolia/new_php_client
New version of PHP API client
2 parents a0200a3 + 54dfc2a commit 5391649

File tree

9 files changed

+736
-128
lines changed

9 files changed

+736
-128
lines changed

lib/AlgoliaSearch/Client.php

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Client
3737
const CAINFO = 'cainfo';
3838
const CURLOPT = 'curloptions';
3939
const PLACES_ENABLED = 'placesEnabled';
40+
const FAILING_HOSTS_CACHE = 'failingHostsCache';
4041

4142
/**
4243
* @var ClientContext
@@ -94,12 +95,18 @@ public function __construct($applicationID, $apiKey, $hostsArray = null, $option
9495
case self::PLACES_ENABLED:
9596
$this->placesEnabled = (bool) $value;
9697
break;
98+
case self::FAILING_HOSTS_CACHE:
99+
if (! $value instanceof FailingHostsCache) {
100+
throw new \InvalidArgumentException('failingHostsCache must be an instance of \AlgoliaSearch\FailingHostsCache.');
101+
}
102+
break;
97103
default:
98104
throw new \Exception('Unknown option: '.$option);
99105
}
100106
}
101107

102-
$this->context = new ClientContext($applicationID, $apiKey, $hostsArray, $this->placesEnabled);
108+
$failingHostsCache = isset($options[self::FAILING_HOSTS_CACHE]) ? $options[self::FAILING_HOSTS_CACHE] : null;
109+
$this->context = new ClientContext($applicationID, $apiKey, $hostsArray, $this->placesEnabled, $failingHostsCache);
103110
}
104111

105112
/**
@@ -408,13 +415,13 @@ public function initIndex($indexName)
408415
}
409416

410417
/**
411-
* List all existing user keys with their associated ACLs.
418+
* List all existing API keys with their associated ACLs.
412419
*
413420
* @return mixed
414421
*
415422
* @throws AlgoliaException
416423
*/
417-
public function listUserKeys()
424+
public function listApiKeys()
418425
{
419426
return $this->request(
420427
$this->context,
@@ -429,13 +436,22 @@ public function listUserKeys()
429436
}
430437

431438
/**
432-
* Get ACL of a user key.
439+
* @return mixed
440+
* @deprecated use listApiKeys instead
441+
*/
442+
public function listUserKeys()
443+
{
444+
return $this->listApiKeys();
445+
}
446+
447+
/**
448+
* Get ACL of a API key.
433449
*
434450
* @param string $key
435451
*
436452
* @return mixed
437453
*/
438-
public function getUserKeyACL($key)
454+
public function getApiKey($key)
439455
{
440456
return $this->request(
441457
$this->context,
@@ -450,13 +466,23 @@ public function getUserKeyACL($key)
450466
}
451467

452468
/**
453-
* Delete an existing user key.
469+
* @param $key
470+
* @return mixed
471+
* @deprecated use getApiKey instead
472+
*/
473+
public function getUserKeyACL($key)
474+
{
475+
return $this->getApiKey($key);
476+
}
477+
478+
/**
479+
* Delete an existing API key.
454480
*
455481
* @param string $key
456482
*
457483
* @return mixed
458484
*/
459-
public function deleteUserKey($key)
485+
public function deleteApiKey($key)
460486
{
461487
return $this->request(
462488
$this->context,
@@ -471,7 +497,17 @@ public function deleteUserKey($key)
471497
}
472498

473499
/**
474-
* Create a new user key.
500+
* @param $key
501+
* @return mixed
502+
* @deprecated use deleteApiKey instead
503+
*/
504+
public function deleteUserKey($key)
505+
{
506+
return $this->deleteApiKey($key);
507+
}
508+
509+
/**
510+
* Create a new API key.
475511
*
476512
* @param array $obj can be two different parameters:
477513
* The list of parameters for this key. Defined by an array that
@@ -504,7 +540,7 @@ public function deleteUserKey($key)
504540
*
505541
* @throws AlgoliaException
506542
*/
507-
public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
543+
public function addApiKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
508544
{
509545
if ($obj !== array_values($obj)) { // is dict of value
510546
$params = $obj;
@@ -537,7 +573,21 @@ public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $ma
537573
}
538574

539575
/**
540-
* Update a user key.
576+
* @param $obj
577+
* @param int $validity
578+
* @param int $maxQueriesPerIPPerHour
579+
* @param int $maxHitsPerQuery
580+
* @param null $indexes
581+
* @return mixed
582+
* @deprecated use addApiKey instead
583+
*/
584+
public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
585+
{
586+
return $this->addApiKey($obj, $validity, $maxQueriesPerIPPerHour, $maxHitsPerQuery, $indexes);
587+
}
588+
589+
/**
590+
* Update an API key.
541591
*
542592
* @param string $key
543593
* @param array $obj can be two different parameters:
@@ -571,7 +621,7 @@ public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $ma
571621
*
572622
* @throws AlgoliaException
573623
*/
574-
public function updateUserKey(
624+
public function updateApiKey(
575625
$key,
576626
$obj,
577627
$validity = 0,
@@ -608,6 +658,27 @@ public function updateUserKey(
608658
);
609659
}
610660

661+
/**
662+
* @param $key
663+
* @param $obj
664+
* @param int $validity
665+
* @param int $maxQueriesPerIPPerHour
666+
* @param int $maxHitsPerQuery
667+
* @param null $indexes
668+
* @return mixed
669+
* @deprecated use updateApiKey instead
670+
*/
671+
public function updateUserKey(
672+
$key,
673+
$obj,
674+
$validity = 0,
675+
$maxQueriesPerIPPerHour = 0,
676+
$maxHitsPerQuery = 0,
677+
$indexes = null
678+
) {
679+
return $this->updateApiKey($key, $obj, $validity, $maxQueriesPerIPPerHour, $maxHitsPerQuery, $indexes);
680+
}
681+
611682
/**
612683
* Send a batch request targeting multiple indices.
613684
*
@@ -918,7 +989,7 @@ public function doRequest(
918989
curl_close($curlHandle);
919990

920991
if (intval($http_status / 100) == 4) {
921-
throw new AlgoliaException(isset($answer['message']) ? $answer['message'] : $http_status . ' error');
992+
throw new AlgoliaException(isset($answer['message']) ? $answer['message'] : $http_status.' error');
922993
} elseif (intval($http_status / 100) != 2) {
923994
throw new \Exception($http_status.': '.$response);
924995
}

lib/AlgoliaSearch/ClientContext.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,20 @@ class ClientContext
7777
public $connectTimeout;
7878

7979
/**
80-
* @var array
80+
* @var FailingHostsCache
8181
*/
82-
private static $failingHosts = array();
82+
private $failingHostsCache;
8383

8484
/**
85-
* ClientContext constructor.
86-
*
87-
* @param string $applicationID
88-
* @param string $apiKey
89-
* @param array $hostsArray
90-
* @param bool $placesEnabled
85+
* @param string $applicationID
86+
* @param string $apiKey
87+
* @param array $hostsArray
88+
* @param bool $placesEnabled
89+
* @param FailingHostsCache $failingHostsCache
9190
*
9291
* @throws Exception
9392
*/
94-
public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled = false)
93+
public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled = false, FailingHostsCache $failingHostsCache = null)
9594
{
9695
// connect timeout of 1s by default
9796
$this->connectTimeout = 1;
@@ -113,8 +112,6 @@ public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled
113112
$this->writeHostsArray = $this->getDefaultWriteHosts();
114113
}
115114

116-
$this->rotateHosts();
117-
118115
if ($this->applicationID == null || mb_strlen($this->applicationID) == 0) {
119116
throw new Exception('AlgoliaSearch requires an applicationID.');
120117
}
@@ -129,6 +126,14 @@ public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled
129126
$this->algoliaUserToken = null;
130127
$this->rateLimitAPIKey = null;
131128
$this->headers = array();
129+
130+
if ($failingHostsCache === null) {
131+
$this->failingHostsCache = new InMemoryFailingHostsCache();
132+
} else {
133+
$this->failingHostsCache = $failingHostsCache;
134+
}
135+
136+
$this->rotateHosts();
132137
}
133138

134139
/**
@@ -182,7 +187,7 @@ private function getDefaultWriteHosts()
182187
*/
183188
public function __destruct()
184189
{
185-
if ($this->curlMHandle != null) {
190+
if (is_resource($this->curlMHandle)) {
186191
curl_multi_close($this->curlMHandle);
187192
}
188193
}
@@ -194,7 +199,7 @@ public function __destruct()
194199
*/
195200
public function getMHandle($curlHandle)
196201
{
197-
if ($this->curlMHandle == null) {
202+
if (!is_resource($this->curlMHandle)) {
198203
$this->curlMHandle = curl_multi_init();
199204
}
200205
curl_multi_add_handle($this->curlMHandle, $curlHandle);
@@ -258,15 +263,20 @@ public function setExtraHeader($key, $value)
258263
}
259264

260265
/**
261-
* @param $host
266+
* @param string $host
262267
*/
263-
public static function addFailingHost($host)
268+
public function addFailingHost($host)
264269
{
265-
if (! in_array($host, self::$failingHosts)) {
266-
self::$failingHosts[] = $host;
267-
}
270+
$this->failingHostsCache->addFailingHost($host);
268271
}
269272

273+
/**
274+
* @return FailingHostsCache
275+
*/
276+
public function getFailingHostsCache()
277+
{
278+
return $this->failingHostsCache;
279+
}
270280
/**
271281
* This method is called to pass on failing hosts.
272282
* If the host is first either in the failingHosts array, we
@@ -276,14 +286,15 @@ public static function addFailingHost($host)
276286
*/
277287
public function rotateHosts()
278288
{
289+
$failingHosts = $this->failingHostsCache->getFailingHosts();
279290
$i = 0;
280-
while ($i <= count($this->readHostsArray) && in_array($this->readHostsArray[0], self::$failingHosts)) {
291+
while ($i <= count($this->readHostsArray) && in_array($this->readHostsArray[0], $failingHosts)) {
281292
$i++;
282293
$this->readHostsArray[] = array_shift($this->readHostsArray);
283294
}
284295

285296
$i = 0;
286-
while ($i <= count($this->writeHostsArray) && in_array($this->writeHostsArray[0], self::$failingHosts)) {
297+
while ($i <= count($this->writeHostsArray) && in_array($this->writeHostsArray[0], $failingHosts)) {
287298
$i++;
288299
$this->writeHostsArray[] = array_shift($this->writeHostsArray);
289300
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace AlgoliaSearch;
3+
4+
interface FailingHostsCache
5+
{
6+
/**
7+
* @param string $host
8+
*/
9+
public function addFailingHost($host);
10+
11+
/**
12+
* Get failing hosts from cache. This method should also handle cache invalidation if required.
13+
* The TTL of the failed hosts cache should be 5 minutes.
14+
*
15+
* @return array
16+
*/
17+
public function getFailingHosts();
18+
19+
/**
20+
* Invalidates the cache.
21+
*/
22+
public function flushFailingHostsCache();
23+
}

0 commit comments

Comments
 (0)