Skip to content

Commit a9fcf9c

Browse files
committed
Support RecommendNextItems endpoint. Use guzzle as HTTP client.
1 parent 089b7e8 commit a9fcf9c

31 files changed

+330
-153
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ or
1717
```
1818
{
1919
"require": {
20-
"recombee/php-api-client": "^3.0.0"
20+
"recombee/php-api-client": "^3.1.0"
2121
}
2222
}
2323
```
@@ -55,9 +55,12 @@ try
5555
$res = $client->send(new Reqs\Batch($purchase_requests)); //Use Batch for faster processing of larger data
5656

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

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

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

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=5.3.0",
14-
"rmccue/requests": "^1.7.0"
13+
"php": ">=7.2.0",
14+
"guzzlehttp/guzzle": "^7.2.0"
1515
},
1616
"require-dev": {
17-
"phpunit/phpunit": "^6.1.0",
17+
"phpunit/phpunit": "^9.2.2",
1818
"phpdocumentor/phpdocumentor": "2.*"
1919
},
2020
"autoload": {

src/RecommApi/Client.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Client{
2222
protected $protocol;
2323
protected $base_uri;
2424
protected $options;
25+
protected $guzzle_client;
2526

2627
/**
2728
* @ignore
@@ -52,10 +53,12 @@ public function __construct($account, $token, $protocol = 'https', $options= arr
5253
else if (isset($this->options['baseUri']))
5354
$this->base_uri = $this->options['baseUri'];
5455
$this->user_agent = $this->getUserAgent();
56+
57+
$this->guzzle_client = new \GuzzleHttp\Client();
5558
}
5659

5760
protected function getUserAgent() {
58-
$user_agent = 'recombee-php-api-client/3.0.0';
61+
$user_agent = 'recombee-php-api-client/3.1.0';
5962
if (isset($this->options['serviceName']))
6063
$user_agent .= ' '.($this->options['serviceName']);
6164
return $user_agent;
@@ -103,7 +106,11 @@ public function send(Requests\Request $request) {
103106
break;
104107
}
105108
}
106-
catch(\Requests_Exception $e)
109+
catch(\GuzzleHttp\Exception\ConnectException $e)
110+
{
111+
throw new ApiTimeoutException($request);
112+
}
113+
catch(\GuzzleHttp\Exception\GuzzleException $e)
107114
{
108115
if(strpos($e->getMessage(), 'cURL error 28') !== false) throw new ApiTimeoutException($request);
109116
if(strpos($e->getMessage(), 'timed out') !== false) throw new ApiTimeoutException($request);
@@ -125,56 +132,58 @@ protected function getHttpHeaders() {
125132
return array_merge(array('User-Agent' => $this->user_agent), $this->getOptionalHttpHeaders());
126133
}
127134

128-
protected function getOptionalRequestOptions() {
135+
protected function getRequestOptions() {
136+
$options = array('http_errors' => false);
129137
if (isset($this->options['requestsOptions']))
130-
return $this->options['requestsOptions'];
131-
return array();
138+
$options = array_merge($options, $this->options['requestsOptions']);
139+
return $options;
132140
}
133141

142+
134143
protected function put($uri, $timeout, $body) {
135-
$options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions());
144+
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
136145
$headers = array_merge(array('Content-Type' => 'application/json'), $this->getHttpHeaders());
137-
138-
$response = \Requests::put($uri, $headers, $body, $options);
146+
$response = $this->guzzle_client->request('PUT', $uri, array_merge($options, ['body' => $body, 'headers' => $headers]));
139147
$this->checkErrors($response);
140-
return $response->body;
148+
return (string) $response->getBody();
141149
}
142150

143151
protected function get($uri, $timeout) {
144-
$options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions());
152+
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
145153
$headers = $this->getHttpHeaders();
146154

147-
$response = \Requests::get($uri, $headers, $options);
155+
$response = $this->guzzle_client->request('GET', $uri, array_merge($options, ['headers' => $headers]));
148156
$this->checkErrors($response);
149-
return json_decode($response->body, true);
157+
return json_decode($response->getBody(), true);
150158
}
151159

152160
protected function delete($uri, $timeout) {
153-
$options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions());
161+
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
154162
$headers = $this->getHttpHeaders();
155163

156-
$response = \Requests::delete($uri, $headers, $options);
164+
$response = $this->guzzle_client->request('DELETE', $uri, array_merge($options, ['headers' => $headers]));
157165
$this->checkErrors($response);
158-
return $response->body;
166+
return (string) $response->getBody();
159167
}
160168

161169
protected function post($uri, $timeout, $body) {
162-
$options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions());
170+
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
163171
$headers = array_merge(array('Content-Type' => 'application/json'), $this->getHttpHeaders());
164-
$response = \Requests::post($uri, $headers, $body, $options);
172+
173+
$response = $this->guzzle_client->request('POST', $uri, array_merge($options, ['body' => $body, 'headers' => $headers]));
165174
$this->checkErrors($response);
166175

167-
$json = json_decode($response->body, true);
176+
$json = json_decode($response->getBody(), true);
168177
if($json !== null && json_last_error() == JSON_ERROR_NONE)
169178
return $json;
170179
else
171-
return $response->body;
180+
return (string) $response->getBody();
172181
}
173182

174183
protected function checkErrors($response) {
175-
$status_code = $response->status_code;
184+
$status_code = $response->getStatusCode();
176185
if($status_code == 200 || $status_code == 201) return;
177-
throw new ResponseException($this->request, $status_code, $response->body);
186+
throw new ResponseException($this->request, $status_code, $response->getBody());
178187
}
179188

180189
protected function sendMultipartBatch($request) {

src/RecommApi/Requests/AddBookmark.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AddBookmark extends Request {
3535
*/
3636
protected $recomm_id;
3737
/**
38-
* @var $additional_data A dictionary of additional data for the interaction.
38+
* @var array $additional_data A dictionary of additional data for the interaction.
3939
*/
4040
protected $additional_data;
4141
/**
@@ -59,7 +59,7 @@ class AddBookmark extends Request {
5959
* - Type: string
6060
* - Description: If this bookmark is based on a recommendation request, `recommId` is the id of the clicked recommendation.
6161
* - *additionalData*
62-
* - Type:
62+
* - Type: array
6363
* - Description: A dictionary of additional data for the interaction.
6464
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
6565
*/

src/RecommApi/Requests/AddCartAddition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class AddCartAddition extends Request {
4343
*/
4444
protected $recomm_id;
4545
/**
46-
* @var $additional_data A dictionary of additional data for the interaction.
46+
* @var array $additional_data A dictionary of additional data for the interaction.
4747
*/
4848
protected $additional_data;
4949
/**
@@ -73,7 +73,7 @@ class AddCartAddition extends Request {
7373
* - Type: string
7474
* - Description: If this cart addition is based on a recommendation request, `recommId` is the id of the clicked recommendation.
7575
* - *additionalData*
76-
* - Type:
76+
* - Type: array
7777
* - Description: A dictionary of additional data for the interaction.
7878
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
7979
*/

src/RecommApi/Requests/AddDetailView.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AddDetailView extends Request {
3939
*/
4040
protected $recomm_id;
4141
/**
42-
* @var $additional_data A dictionary of additional data for the interaction.
42+
* @var array $additional_data A dictionary of additional data for the interaction.
4343
*/
4444
protected $additional_data;
4545
/**
@@ -66,7 +66,7 @@ class AddDetailView extends Request {
6666
* - Type: string
6767
* - Description: If this detail view is based on a recommendation request, `recommId` is the id of the clicked recommendation.
6868
* - *additionalData*
69-
* - Type:
69+
* - Type: array
7070
* - Description: A dictionary of additional data for the interaction.
7171
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
7272
*/

src/RecommApi/Requests/AddPurchase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class AddPurchase extends Request {
4747
*/
4848
protected $recomm_id;
4949
/**
50-
* @var $additional_data A dictionary of additional data for the interaction.
50+
* @var array $additional_data A dictionary of additional data for the interaction.
5151
*/
5252
protected $additional_data;
5353
/**
@@ -80,7 +80,7 @@ class AddPurchase extends Request {
8080
* - Type: string
8181
* - Description: If this purchase is based on a recommendation request, `recommId` is the id of the clicked recommendation.
8282
* - *additionalData*
83-
* - Type:
83+
* - Type: array
8484
* - Description: A dictionary of additional data for the interaction.
8585
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
8686
*/

src/RecommApi/Requests/AddRating.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AddRating extends Request {
3939
*/
4040
protected $recomm_id;
4141
/**
42-
* @var $additional_data A dictionary of additional data for the interaction.
42+
* @var array $additional_data A dictionary of additional data for the interaction.
4343
*/
4444
protected $additional_data;
4545
/**
@@ -64,7 +64,7 @@ class AddRating extends Request {
6464
* - Type: string
6565
* - Description: If this rating is based on a recommendation request, `recommId` is the id of the clicked recommendation.
6666
* - *additionalData*
67-
* - Type:
67+
* - Type: array
6868
* - Description: A dictionary of additional data for the interaction.
6969
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
7070
*/

src/RecommApi/Requests/ItemBasedRecommendation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class ItemBasedRecommendation extends Request {
116116
*/
117117
protected $rotation_time;
118118
/**
119-
* @var $expert_settings Dictionary of custom options.
119+
* @var array $expert_settings Dictionary of custom options.
120120
*/
121121
protected $expert_settings;
122122
/**
@@ -209,7 +209,7 @@ class ItemBasedRecommendation extends Request {
209209
* - Type: float
210210
* - 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`.
211211
* - *expertSettings*
212-
* - Type:
212+
* - Type: array
213213
* - Description: Dictionary of custom options.
214214
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
215215
*/

0 commit comments

Comments
 (0)