Skip to content
This repository has been archived by the owner on Jan 25, 2021. It is now read-only.

Commit

Permalink
Update Algolia library to v1.18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Aug 25, 2017
1 parent 933a3ba commit 32bf9c5
Show file tree
Hide file tree
Showing 8 changed files with 655 additions and 57 deletions.
3 changes: 3 additions & 0 deletions vendor/algolia-client/algoliasearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@
require_once 'src/AlgoliaSearch/SynonymType.php';
require_once 'src/AlgoliaSearch/Version.php';
require_once 'src/AlgoliaSearch/Json.php';
require_once 'src/AlgoliaSearch/FailingHostsCache.php';
require_once 'src/AlgoliaSearch/FileFailingHostsCache.php';
require_once 'src/AlgoliaSearch/InMemoryFailingHostsCache.php';
103 changes: 87 additions & 16 deletions vendor/algolia-client/src/AlgoliaSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Client
const CAINFO = 'cainfo';
const CURLOPT = 'curloptions';
const PLACES_ENABLED = 'placesEnabled';
const FAILING_HOSTS_CACHE = 'failingHostsCache';

/**
* @var ClientContext
Expand Down Expand Up @@ -94,12 +95,18 @@ public function __construct($applicationID, $apiKey, $hostsArray = null, $option
case self::PLACES_ENABLED:
$this->placesEnabled = (bool) $value;
break;
case self::FAILING_HOSTS_CACHE:
if (! $value instanceof FailingHostsCache) {
throw new \InvalidArgumentException('failingHostsCache must be an instance of \AlgoliaSearch\FailingHostsCache.');
}
break;
default:
throw new \Exception('Unknown option: '.$option);
}
}

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

/**
Expand Down Expand Up @@ -408,13 +415,13 @@ public function initIndex($indexName)
}

/**
* List all existing user keys with their associated ACLs.
* List all existing API keys with their associated ACLs.
*
* @return mixed
*
* @throws AlgoliaException
*/
public function listUserKeys()
public function listApiKeys()
{
return $this->request(
$this->context,
Expand All @@ -429,13 +436,22 @@ public function listUserKeys()
}

/**
* Get ACL of a user key.
* @return mixed
* @deprecated use listApiKeys instead
*/
public function listUserKeys()
{
return $this->listApiKeys();
}

/**
* Get ACL of a API key.
*
* @param string $key
*
* @return mixed
*/
public function getUserKeyACL($key)
public function getApiKey($key)
{
return $this->request(
$this->context,
Expand All @@ -450,13 +466,23 @@ public function getUserKeyACL($key)
}

/**
* Delete an existing user key.
* @param $key
* @return mixed
* @deprecated use getApiKey instead
*/
public function getUserKeyACL($key)
{
return $this->getApiKey($key);
}

/**
* Delete an existing API key.
*
* @param string $key
*
* @return mixed
*/
public function deleteUserKey($key)
public function deleteApiKey($key)
{
return $this->request(
$this->context,
Expand All @@ -471,7 +497,17 @@ public function deleteUserKey($key)
}

/**
* Create a new user key.
* @param $key
* @return mixed
* @deprecated use deleteApiKey instead
*/
public function deleteUserKey($key)
{
return $this->deleteApiKey($key);
}

/**
* Create a new API key.
*
* @param array $obj can be two different parameters:
* The list of parameters for this key. Defined by an array that
Expand Down Expand Up @@ -504,7 +540,7 @@ public function deleteUserKey($key)
*
* @throws AlgoliaException
*/
public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
public function addApiKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
{
if ($obj !== array_values($obj)) { // is dict of value
$params = $obj;
Expand Down Expand Up @@ -537,7 +573,21 @@ public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $ma
}

/**
* Update a user key.
* @param $obj
* @param int $validity
* @param int $maxQueriesPerIPPerHour
* @param int $maxHitsPerQuery
* @param null $indexes
* @return mixed
* @deprecated use addApiKey instead
*/
public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $maxHitsPerQuery = 0, $indexes = null)
{
return $this->addApiKey($obj, $validity, $maxQueriesPerIPPerHour, $maxHitsPerQuery, $indexes);
}

/**
* Update an API key.
*
* @param string $key
* @param array $obj can be two different parameters:
Expand Down Expand Up @@ -571,7 +621,7 @@ public function addUserKey($obj, $validity = 0, $maxQueriesPerIPPerHour = 0, $ma
*
* @throws AlgoliaException
*/
public function updateUserKey(
public function updateApiKey(
$key,
$obj,
$validity = 0,
Expand Down Expand Up @@ -608,6 +658,27 @@ public function updateUserKey(
);
}

/**
* @param $key
* @param $obj
* @param int $validity
* @param int $maxQueriesPerIPPerHour
* @param int $maxHitsPerQuery
* @param null $indexes
* @return mixed
* @deprecated use updateApiKey instead
*/
public function updateUserKey(
$key,
$obj,
$validity = 0,
$maxQueriesPerIPPerHour = 0,
$maxHitsPerQuery = 0,
$indexes = null
) {
return $this->updateApiKey($key, $obj, $validity, $maxQueriesPerIPPerHour, $maxHitsPerQuery, $indexes);
}

/**
* Send a batch request targeting multiple indices.
*
Expand Down Expand Up @@ -1013,14 +1084,14 @@ private function getPlacesIndex()
}

/**
* @param string $appId
* @param string $apiKey
* @param array $hostsArray
* @param array $options
* @param string|null $appId
* @param string|null $apiKey
* @param array|null $hostsArray
* @param array $options
*
* @return PlacesIndex
*/
public static function initPlaces($appId, $apiKey, $hostsArray = null, $options = array())
public static function initPlaces($appId = null, $apiKey = null, $hostsArray = null, $options = array())
{
$options['placesEnabled'] = true;
$client = new static($appId, $apiKey, $hostsArray, $options);
Expand Down
51 changes: 31 additions & 20 deletions vendor/algolia-client/src/AlgoliaSearch/ClientContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,20 @@ class ClientContext
public $connectTimeout;

/**
* @var array
* @var FailingHostsCache
*/
private static $failingHosts = array();
private $failingHostsCache;

/**
* ClientContext constructor.
*
* @param string $applicationID
* @param string $apiKey
* @param array $hostsArray
* @param bool $placesEnabled
* @param string|null $applicationID
* @param string|null $apiKey
* @param array|null $hostsArray
* @param bool $placesEnabled
* @param FailingHostsCache|null $failingHostsCache
*
* @throws Exception
*/
public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled = false)
public function __construct($applicationID = null, $apiKey = null, $hostsArray = null, $placesEnabled = false, FailingHostsCache $failingHostsCache = null)
{
// connect timeout of 1s by default
$this->connectTimeout = 1;
Expand All @@ -113,13 +112,11 @@ public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled
$this->writeHostsArray = $this->getDefaultWriteHosts();
}

$this->rotateHosts();

if ($this->applicationID == null || mb_strlen($this->applicationID) == 0) {
if (($this->applicationID == null || mb_strlen($this->applicationID) == 0) && $placesEnabled === false) {
throw new Exception('AlgoliaSearch requires an applicationID.');
}

if ($this->apiKey == null || mb_strlen($this->apiKey) == 0) {
if (($this->apiKey == null || mb_strlen($this->apiKey) == 0) && $placesEnabled === false) {
throw new Exception('AlgoliaSearch requires an apiKey.');
}

Expand All @@ -129,6 +126,14 @@ public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled
$this->algoliaUserToken = null;
$this->rateLimitAPIKey = null;
$this->headers = array();

if ($failingHostsCache === null) {
$this->failingHostsCache = new InMemoryFailingHostsCache();
} else {
$this->failingHostsCache = $failingHostsCache;
}

$this->rotateHosts();
}

/**
Expand Down Expand Up @@ -258,15 +263,20 @@ public function setExtraHeader($key, $value)
}

/**
* @param $host
* @param string $host
*/
public static function addFailingHost($host)
public function addFailingHost($host)
{
if (!in_array($host, self::$failingHosts)) {
self::$failingHosts[] = $host;
}
$this->failingHostsCache->addFailingHost($host);
}

/**
* @return FailingHostsCache
*/
public function getFailingHostsCache()
{
return $this->failingHostsCache;
}
/**
* This method is called to pass on failing hosts.
* If the host is first either in the failingHosts array, we
Expand All @@ -276,14 +286,15 @@ public static function addFailingHost($host)
*/
public function rotateHosts()
{
$failingHosts = $this->failingHostsCache->getFailingHosts();
$i = 0;
while ($i <= count($this->readHostsArray) && in_array($this->readHostsArray[0], self::$failingHosts)) {
while ($i <= count($this->readHostsArray) && in_array($this->readHostsArray[0], $failingHosts)) {
$i++;
$this->readHostsArray[] = array_shift($this->readHostsArray);
}

$i = 0;
while ($i <= count($this->writeHostsArray) && in_array($this->writeHostsArray[0], self::$failingHosts)) {
while ($i <= count($this->writeHostsArray) && in_array($this->writeHostsArray[0], $failingHosts)) {
$i++;
$this->writeHostsArray[] = array_shift($this->writeHostsArray);
}
Expand Down
23 changes: 23 additions & 0 deletions vendor/algolia-client/src/AlgoliaSearch/FailingHostsCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace AlgoliaSearch;

interface FailingHostsCache
{
/**
* @param string $host
*/
public function addFailingHost($host);

/**
* Get failing hosts from cache. This method should also handle cache invalidation if required.
* The TTL of the failed hosts cache should be 5 minutes.
*
* @return array
*/
public function getFailingHosts();

/**
* Invalidates the cache.
*/
public function flushFailingHostsCache();
}
Loading

0 comments on commit 32bf9c5

Please sign in to comment.