Skip to content

Commit

Permalink
Merge pull request #16 from doofinder/small-rf
Browse files Browse the repository at this point in the history
Small rf & #14
  • Loading branch information
carlosescri authored Feb 13, 2017
2 parents af7b800 + b2eb47b commit b4703e7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Management/Errors/IndexingInProgress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Doofinder\Api\Management\Errors;

class IndexingInProgress extends \Exception {}
8 changes: 6 additions & 2 deletions src/Management/Errors/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/Management/SearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions src/Search/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.");
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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');
Expand Down

0 comments on commit b4703e7

Please sign in to comment.