Skip to content

Commit

Permalink
Support RecommendNextItems endpoint. Use guzzle as HTTP client.
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraFiedler committed Jan 14, 2021
1 parent 089b7e8 commit a9fcf9c
Show file tree
Hide file tree
Showing 31 changed files with 330 additions and 153 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ or
```
{
"require": {
"recombee/php-api-client": "^3.0.0"
"recombee/php-api-client": "^3.1.0"
}
}
```
Expand Down Expand Up @@ -55,9 +55,12 @@ try
$res = $client->send(new Reqs\Batch($purchase_requests)); //Use Batch for faster processing of larger data

// Get 5 recommendations for user 'user-25'
$recommended = $client->send(new Reqs\RecommendItemsToUser('user-25', 5));
$response = $client->send(new Reqs\RecommendItemsToUser('user-25', 5));
echo 'Recommended items: ' . json_encode($response, JSON_PRETTY_PRINT) . "\n";

echo 'Recommended items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n";
// User scrolled down - get next 3 recommended items
$response = $client->send(new Reqs\RecommendNextItems($response['recommId'], 3));
echo 'Next recommended items: ' . json_encode($response, JSON_PRETTY_PRINT) . "\n";
}
catch(Ex\ApiException $e)
{
Expand Down Expand Up @@ -157,7 +160,7 @@ $recommended = $client->send(

// Perform personalized full-text search with a user's search query (e.g. 'computers')
$matches = $client->send(
new Reqs\SearchItems('user-42', 'computers', 5)
new Reqs\SearchItems('user-42', 'computers', 5, ['scenario' => 'search_top'])
);
echo 'Matched items: ' . json_encode($matches, JSON_PRETTY_PRINT) . "\n";

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
}
],
"require": {
"php": ">=5.3.0",
"rmccue/requests": "^1.7.0"
"php": ">=7.2.0",
"guzzlehttp/guzzle": "^7.2.0"
},
"require-dev": {
"phpunit/phpunit": "^6.1.0",
"phpunit/phpunit": "^9.2.2",
"phpdocumentor/phpdocumentor": "2.*"
},
"autoload": {
Expand Down
51 changes: 30 additions & 21 deletions src/RecommApi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Client{
protected $protocol;
protected $base_uri;
protected $options;
protected $guzzle_client;

/**
* @ignore
Expand Down Expand Up @@ -52,10 +53,12 @@ public function __construct($account, $token, $protocol = 'https', $options= arr
else if (isset($this->options['baseUri']))
$this->base_uri = $this->options['baseUri'];
$this->user_agent = $this->getUserAgent();

$this->guzzle_client = new \GuzzleHttp\Client();
}

protected function getUserAgent() {
$user_agent = 'recombee-php-api-client/3.0.0';
$user_agent = 'recombee-php-api-client/3.1.0';
if (isset($this->options['serviceName']))
$user_agent .= ' '.($this->options['serviceName']);
return $user_agent;
Expand Down Expand Up @@ -103,7 +106,11 @@ public function send(Requests\Request $request) {
break;
}
}
catch(\Requests_Exception $e)
catch(\GuzzleHttp\Exception\ConnectException $e)
{
throw new ApiTimeoutException($request);
}
catch(\GuzzleHttp\Exception\GuzzleException $e)
{
if(strpos($e->getMessage(), 'cURL error 28') !== false) throw new ApiTimeoutException($request);
if(strpos($e->getMessage(), 'timed out') !== false) throw new ApiTimeoutException($request);
Expand All @@ -125,56 +132,58 @@ protected function getHttpHeaders() {
return array_merge(array('User-Agent' => $this->user_agent), $this->getOptionalHttpHeaders());
}

protected function getOptionalRequestOptions() {
protected function getRequestOptions() {
$options = array('http_errors' => false);
if (isset($this->options['requestsOptions']))
return $this->options['requestsOptions'];
return array();
$options = array_merge($options, $this->options['requestsOptions']);
return $options;
}


protected function put($uri, $timeout, $body) {
$options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions());
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
$headers = array_merge(array('Content-Type' => 'application/json'), $this->getHttpHeaders());

$response = \Requests::put($uri, $headers, $body, $options);
$response = $this->guzzle_client->request('PUT', $uri, array_merge($options, ['body' => $body, 'headers' => $headers]));
$this->checkErrors($response);
return $response->body;
return (string) $response->getBody();
}

protected function get($uri, $timeout) {
$options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions());
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
$headers = $this->getHttpHeaders();

$response = \Requests::get($uri, $headers, $options);
$response = $this->guzzle_client->request('GET', $uri, array_merge($options, ['headers' => $headers]));
$this->checkErrors($response);
return json_decode($response->body, true);
return json_decode($response->getBody(), true);
}

protected function delete($uri, $timeout) {
$options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions());
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
$headers = $this->getHttpHeaders();

$response = \Requests::delete($uri, $headers, $options);
$response = $this->guzzle_client->request('DELETE', $uri, array_merge($options, ['headers' => $headers]));
$this->checkErrors($response);
return $response->body;
return (string) $response->getBody();
}

protected function post($uri, $timeout, $body) {
$options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions());
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
$headers = array_merge(array('Content-Type' => 'application/json'), $this->getHttpHeaders());
$response = \Requests::post($uri, $headers, $body, $options);

$response = $this->guzzle_client->request('POST', $uri, array_merge($options, ['body' => $body, 'headers' => $headers]));
$this->checkErrors($response);

$json = json_decode($response->body, true);
$json = json_decode($response->getBody(), true);
if($json !== null && json_last_error() == JSON_ERROR_NONE)
return $json;
else
return $response->body;
return (string) $response->getBody();
}

protected function checkErrors($response) {
$status_code = $response->status_code;
$status_code = $response->getStatusCode();
if($status_code == 200 || $status_code == 201) return;
throw new ResponseException($this->request, $status_code, $response->body);
throw new ResponseException($this->request, $status_code, $response->getBody());
}

protected function sendMultipartBatch($request) {
Expand Down
4 changes: 2 additions & 2 deletions src/RecommApi/Requests/AddBookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AddBookmark extends Request {
*/
protected $recomm_id;
/**
* @var $additional_data A dictionary of additional data for the interaction.
* @var array $additional_data A dictionary of additional data for the interaction.
*/
protected $additional_data;
/**
Expand All @@ -59,7 +59,7 @@ class AddBookmark extends Request {
* - Type: string
* - Description: If this bookmark is based on a recommendation request, `recommId` is the id of the clicked recommendation.
* - *additionalData*
* - Type:
* - Type: array
* - Description: A dictionary of additional data for the interaction.
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
*/
Expand Down
4 changes: 2 additions & 2 deletions src/RecommApi/Requests/AddCartAddition.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AddCartAddition extends Request {
*/
protected $recomm_id;
/**
* @var $additional_data A dictionary of additional data for the interaction.
* @var array $additional_data A dictionary of additional data for the interaction.
*/
protected $additional_data;
/**
Expand Down Expand Up @@ -73,7 +73,7 @@ class AddCartAddition extends Request {
* - Type: string
* - Description: If this cart addition is based on a recommendation request, `recommId` is the id of the clicked recommendation.
* - *additionalData*
* - Type:
* - Type: array
* - Description: A dictionary of additional data for the interaction.
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
*/
Expand Down
4 changes: 2 additions & 2 deletions src/RecommApi/Requests/AddDetailView.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AddDetailView extends Request {
*/
protected $recomm_id;
/**
* @var $additional_data A dictionary of additional data for the interaction.
* @var array $additional_data A dictionary of additional data for the interaction.
*/
protected $additional_data;
/**
Expand All @@ -66,7 +66,7 @@ class AddDetailView extends Request {
* - Type: string
* - Description: If this detail view is based on a recommendation request, `recommId` is the id of the clicked recommendation.
* - *additionalData*
* - Type:
* - Type: array
* - Description: A dictionary of additional data for the interaction.
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
*/
Expand Down
4 changes: 2 additions & 2 deletions src/RecommApi/Requests/AddPurchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AddPurchase extends Request {
*/
protected $recomm_id;
/**
* @var $additional_data A dictionary of additional data for the interaction.
* @var array $additional_data A dictionary of additional data for the interaction.
*/
protected $additional_data;
/**
Expand Down Expand Up @@ -80,7 +80,7 @@ class AddPurchase extends Request {
* - Type: string
* - Description: If this purchase is based on a recommendation request, `recommId` is the id of the clicked recommendation.
* - *additionalData*
* - Type:
* - Type: array
* - Description: A dictionary of additional data for the interaction.
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
*/
Expand Down
4 changes: 2 additions & 2 deletions src/RecommApi/Requests/AddRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AddRating extends Request {
*/
protected $recomm_id;
/**
* @var $additional_data A dictionary of additional data for the interaction.
* @var array $additional_data A dictionary of additional data for the interaction.
*/
protected $additional_data;
/**
Expand All @@ -64,7 +64,7 @@ class AddRating extends Request {
* - Type: string
* - Description: If this rating is based on a recommendation request, `recommId` is the id of the clicked recommendation.
* - *additionalData*
* - Type:
* - Type: array
* - Description: A dictionary of additional data for the interaction.
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
*/
Expand Down
4 changes: 2 additions & 2 deletions src/RecommApi/Requests/ItemBasedRecommendation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ItemBasedRecommendation extends Request {
*/
protected $rotation_time;
/**
* @var $expert_settings Dictionary of custom options.
* @var array $expert_settings Dictionary of custom options.
*/
protected $expert_settings;
/**
Expand Down Expand Up @@ -209,7 +209,7 @@ class ItemBasedRecommendation extends Request {
* - Type: float
* - Description: **Expert option** If the *targetUserId* is provided: Taking *rotationRate* into account, specifies how long time it takes to an item to recover from the penalization. For example, `rotationTime=7200.0` means that items recommended less than 2 hours ago are penalized. Default: `7200.0`.
* - *expertSettings*
* - Type:
* - Type: array
* - Description: Dictionary of custom options.
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
*/
Expand Down
Loading

0 comments on commit a9fcf9c

Please sign in to comment.