From b441a0bf4fd9c22bb72509badd82501af96adeb6 Mon Sep 17 00:00:00 2001 From: Joe Z Date: Tue, 7 Feb 2017 15:01:06 -0500 Subject: [PATCH 1/3] make deleteType async friendly -sort of- --- README.md | 16 ++++++++++++++++ src/Management/Errors/Utils.php | 8 ++++++-- src/Management/SearchEngine.php | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7cabd51..f4c2383 100644 --- a/README.md +++ b/README.md @@ -475,6 +475,22 @@ $new_types = $mySearchEngine->addType('product'); // Add new type $mySearchEngine->deleteType('product'); // Remove the type and all items within it. ``` +**Warning:** Type deletion is an *asynchronous operation*, that keeps going after the `deleteType` method has returned `true`. If you try to perform `addItem`, `deleteItem`, `updateItem` or `updateItems` right after a `deleteType` operation, chances are you get an `IndexingInProgress` exception, because those ops aren't allowed while the `deleteType` is still in progress. If that happens, just wait a little and try again. + +```php +$mySearchEngine->deleteType('estudio'); // async op +$new_types = $mySearchEngine->addType('estudio'); // ok +$notReadyYet = true; +while($notReadyYet){ + try{ + $mySearchEngine->addItem('estudio', 'AAA', $someItem); // it may not be ready yet!! + $notReadyYet = false; + } catch (IndexingInProgress $ipe) { + sleep(3); // just wait a little + } +} +``` + #### Items Management ##### Single Item diff --git a/src/Management/Errors/Utils.php b/src/Management/Errors/Utils.php index ffcf5c5..2bf5b02 100644 --- a/src/Management/Errors/Utils.php +++ b/src/Management/Errors/Utils.php @@ -19,8 +19,12 @@ public static function handleErrors($statusCode, $response) { return new NotAllowed("The user hasn't provided valid authorization: ".Utils::readError($response)); case 404: return new NotFound("Not Found: ".Utils::readError($response)); - case 409: // trying to post with an already used id - return new BadRequest("Request conflict: ".Utils::readError($response)); + case 409: + if (preg_match('/indexing.*progress/i', $response) == 1) { // trying to index while indexing in progress + return new IndexingInProgress(Utils::readError($response)); + } else { + return new BadRequest("Request conflict: ".Utils::readError($response)); // trying to post with an already used id + } case 429: if (stripos($response, 'throttled')) { return new ThrottledResponse(Utils::readError($response)); diff --git a/src/Management/SearchEngine.php b/src/Management/SearchEngine.php index 632ed6b..1efb437 100644 --- a/src/Management/SearchEngine.php +++ b/src/Management/SearchEngine.php @@ -64,7 +64,7 @@ public function addType($datatype) { */ public function deleteType($datatype) { $result = $this->client->managementApiCall('DELETE', "{$this->hashid}/types/{$datatype}"); - return $result['statusCode'] == 204; + return $result['statusCode'] == 202; } public function items($datatype) { From 8a53bc7b42cce59483db2af3d6f00543715a1028 Mon Sep 17 00:00:00 2001 From: Joe Z Date: Tue, 7 Feb 2017 15:02:33 -0500 Subject: [PATCH 2/3] adding Exception --- src/Management/Errors/IndexingInProgress.php | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/Management/Errors/IndexingInProgress.php diff --git a/src/Management/Errors/IndexingInProgress.php b/src/Management/Errors/IndexingInProgress.php new file mode 100644 index 0000000..50c1da6 --- /dev/null +++ b/src/Management/Errors/IndexingInProgress.php @@ -0,0 +1,5 @@ + Date: Mon, 13 Feb 2017 10:39:17 +0100 Subject: [PATCH 3/3] Removed non-essential constants and refactored a bit. Signed-off-by: Carlos Escribano --- src/Search/Client.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Search/Client.php b/src/Search/Client.php index 98627b5..9ad3b0c 100644 --- a/src/Search/Client.php +++ b/src/Search/Client.php @@ -32,12 +32,10 @@ * Basic client to perform requests to Doofinder search servers. */ class Client { - const URL_SUFFIX = '-search.doofinder.com'; const DEFAULT_TIMEOUT = 10000; const DEFAULT_RPP = 10; const DEFAULT_PARAMS_PREFIX = 'dfParam_'; const DEFAULT_API_VERSION = '5'; - const VERSION = '5.4.3'; private $api_key = null; // Authentication private $hashid = null; // ID of the Search Engine @@ -84,7 +82,6 @@ public function __construct($hashid, $api_key, $fromParams = false, $init_option if (2 === count($zone_key_array)) { $this->api_key = $zone_key_array[1]; $this->zone = $zone_key_array[0]; - $this->url = "https://" . $this->zone . self::URL_SUFFIX; } else { throw new Error("API Key is no properly set."); } @@ -133,6 +130,14 @@ public function __construct($hashid, $api_key, $fromParams = false, $init_option } } + private function getSearchUrl($entryPoint = 'search', $params = array()) { + return sprintf("https://%s-search.doofinder.com/%s/%s?%s", + $this->zone, + $this->apiVersion, + $entryPoint, + http_build_query($this->sanitize($params))); + } + private function addPrefix($value) { return $this->paramsPrefix.$value; } @@ -146,13 +151,10 @@ private function getRequestHeaders(){ return $headers; } - private function apiCall($entry_point = 'search', $params = array()){ + private function apiCall($entryPoint = 'search', $params = array()){ $params['hashid'] = $this->hashid; - $session = curl_init(sprintf("%s/%s/%s?%s", $this->url, - $this->apiVersion, - $entry_point, - http_build_query($this->sanitize($params)))); + $session = curl_init($this->getSearchUrl($entryPoint, $params)); // Configure cURL to return response but not headers curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'GET');