Skip to content

Commit 03a4762

Browse files
committed
Support setting the region of your DB. Added Update / Delete More Items endpoints.
Breaking changes: Client constructor: protocol parameter (HTTP/HTTPS) moved to options. Removed deprecated endpoints Item Based Recommendation and User Based Recommendation.
1 parent 9ea2b6c commit 03a4762

21 files changed

+305
-692
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ or
1717
```
1818
{
1919
"require": {
20-
"recombee/php-api-client": "^3.2.0"
20+
"recombee/php-api-client": "^4.0.0"
2121
}
2222
}
2323
```
@@ -30,7 +30,7 @@ use Recombee\RecommApi\Client;
3030
use Recombee\RecommApi\Requests as Reqs;
3131
use Recombee\RecommApi\Exceptions as Ex;
3232

33-
$client = new Client('--my-database-id--', '--db-private-token--');
33+
$client = new Client('--my-database-id--', '--db-private-token--', ['region' => 'us-west']);
3434

3535
const NUM = 100;
3636
const PROBABILITY_PURCHASED = 0.1;
@@ -77,7 +77,7 @@ use Recombee\RecommApi\Exceptions as Ex;
7777
const NUM = 100;
7878
const PROBABILITY_PURCHASED = 0.1;
7979

80-
$client = new Client('--my-database-id--', '--db-private-token--');
80+
$client = new Client('--my-database-id--', '--db-private-token--', ['region' => 'ap-se']);
8181
$client->send(new Reqs\ResetDatabase()); // Clear everything from the database
8282

8383
/*

src/RecommApi/Client.php

+47-23
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class Client{
2424
protected $options;
2525
protected $guzzle_client;
2626

27-
/**
28-
* @ignore
29-
*/
30-
const BASE_URI = 'rapi.recombee.com';
31-
3227
/**
3328
* @ignore
3429
*/
@@ -38,27 +33,54 @@ class Client{
3833
* Create the client
3934
* @param string $account Name of your account at Recombee
4035
* @param string $token Secret token
41-
* @param string $protocol Default protocol for sending requests. Possible values: 'http', 'https'.
4236
* @param array $options Other custom options
4337
*/
44-
public function __construct($account, $token, $protocol = 'https', $options= array()) {
38+
public function __construct($account, $token, $options = array()) {
4539
$this->account = $account;
4640
$this->token = $token;
47-
$this->protocol = $protocol;
48-
$this->base_uri = Client::BASE_URI;
41+
42+
if (!is_array($options)) throw new \InvalidArgumentException("options must be given as an array. $options given instead.");
4943
$this->options = $options;
5044

51-
if(getenv('RAPI_URI') !== false)
52-
$this->base_uri = getenv('RAPI_URI');
53-
else if (isset($this->options['baseUri']))
54-
$this->base_uri = $this->options['baseUri'];
45+
$this->protocol = (isset($this->options['protocol'])) ? $this->options['protocol'] : 'https';
46+
$this->base_uri = $this->getBaseUri();
5547
$this->user_agent = $this->getUserAgent();
5648

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

52+
protected function getRegionalBaseUri($region) {
53+
$uriPerRegion = [
54+
'ap-se' => 'rapi-ap-se.recombee.com',
55+
'ca-east' => 'rapi-ca-east.recombee.com',
56+
'eu-west' => 'rapi-eu-west.recombee.com',
57+
'us-west' => 'rapi-us-west.recombee.com'
58+
];
59+
$uri = $uriPerRegion[strtolower(strval($region))];
60+
if (!isset($uri)) {
61+
throw new \InvalidArgumentException("Region $region is unknown. You may need to update the version of the SDK.");
62+
}
63+
return $uri;
64+
}
65+
66+
protected function getBaseUri() {
67+
$base_uri = null;
68+
if(getenv('RAPI_URI') !== false)
69+
$base_uri = getenv('RAPI_URI');
70+
else if (isset($this->options['baseUri']))
71+
$base_uri = $this->options['baseUri'];
72+
73+
if (isset($this->options['region'])) {
74+
if (isset($base_uri)) {
75+
throw new \InvalidArgumentException('baseUri and region cannot be specified at the same time');
76+
}
77+
$base_uri = $this->getRegionalBaseUri($this->options['region']);
78+
}
79+
return (isset($base_uri)) ? $base_uri : 'rapi.recombee.com';
80+
}
81+
6082
protected function getUserAgent() {
61-
$user_agent = 'recombee-php-api-client/3.2.0';
83+
$user_agent = 'recombee-php-api-client/4.0.0';
6284
if (isset($this->options['serviceName']))
6385
$user_agent .= ' '.($this->options['serviceName']);
6486
return $user_agent;
@@ -84,6 +106,7 @@ public function send(Requests\Request $request) {
84106
$uri = $protocol . '://' . $this->base_uri . $signed_url;
85107
$timeout = $request->getTimeout() / 1000;
86108
$result = null;
109+
$json = json_encode($request->getBodyParameters(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
87110

88111
try {
89112
switch ($request->getMethod()) {
@@ -92,16 +115,14 @@ public function send(Requests\Request $request) {
92115
break;
93116

94117
case Requests\Request::HTTP_PUT:
95-
$json = json_encode($request->getBodyParameters(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
96118
$result = $this->put($uri, $timeout, $json);
97119
break;
98120

99121
case Requests\Request::HTTP_DELETE:
100-
$result = $this->delete($uri, $timeout);
122+
$result = $this->delete($uri, $timeout, $json);
101123
break;
102124

103125
case Requests\Request::HTTP_POST:
104-
$json = json_encode($request->getBodyParameters(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
105126
$result = $this->post($uri, $timeout, $json);
106127
break;
107128
}
@@ -145,7 +166,7 @@ protected function put($uri, $timeout, $body) {
145166
$headers = array_merge(array('Content-Type' => 'application/json'), $this->getHttpHeaders());
146167
$response = $this->guzzle_client->request('PUT', $uri, array_merge($options, ['body' => $body, 'headers' => $headers]));
147168
$this->checkErrors($response);
148-
return (string) $response->getBody();
169+
return $this->formatResult($response);
149170
}
150171

151172
protected function get($uri, $timeout) {
@@ -154,16 +175,16 @@ protected function get($uri, $timeout) {
154175

155176
$response = $this->guzzle_client->request('GET', $uri, array_merge($options, ['headers' => $headers]));
156177
$this->checkErrors($response);
157-
return json_decode($response->getBody(), true);
178+
return $this->formatResult($response);
158179
}
159180

160-
protected function delete($uri, $timeout) {
181+
protected function delete($uri, $timeout, $body) {
161182
$options = array_merge(array('timeout' => $timeout), $this->getRequestOptions());
162-
$headers = $this->getHttpHeaders();
183+
$headers = array_merge(array('Content-Type' => 'application/json'), $this->getHttpHeaders());
163184

164-
$response = $this->guzzle_client->request('DELETE', $uri, array_merge($options, ['headers' => $headers]));
185+
$response = $this->guzzle_client->request('DELETE', $uri, array_merge($options, ['body' => $body, 'headers' => $headers]));
165186
$this->checkErrors($response);
166-
return (string) $response->getBody();
187+
return $this->formatResult($response);
167188
}
168189

169190
protected function post($uri, $timeout, $body) {
@@ -172,7 +193,10 @@ protected function post($uri, $timeout, $body) {
172193

173194
$response = $this->guzzle_client->request('POST', $uri, array_merge($options, ['body' => $body, 'headers' => $headers]));
174195
$this->checkErrors($response);
196+
return $this->formatResult($response);
197+
}
175198

199+
protected function formatResult($response) {
176200
$json = json_decode($response->getBody(), true);
177201
if($json !== null && json_last_error() == JSON_ERROR_NONE)
178202
return $json;

src/RecommApi/Exceptions/UnknownOptionalParameterException.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
*/
1414
class UnknownOptionalParameterException extends \InvalidArgumentException {
1515

16-
/**
17-
* @var string $parameter Given invalid name
18-
*/
16+
/**
17+
* @var string $parameter Given invalid name
18+
*/
1919
public $parameter;
2020

2121
public function __construct($par, \Exception $previous = null) {

src/RecommApi/Requests/DeleteItem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Deletes an item of given `itemId` from the catalog.
1414
* If there are any *purchases*, *ratings*, *bookmarks*, *cart additions* or *detail views* of the item present in the database, they will be deleted in cascade as well. Also, if the item is present in some *series*, it will be removed from all the *series* where present.
15-
* If an item becomes obsolete/no longer available, it is often meaningful to keep it in the catalog (along with all the interaction data, which are very useful), and only exclude the item from recommendations. In such a case, use [ReQL filter](https://docs.recombee.com/reql.html) instead of deleting the item completely.
15+
* If an item becomes obsolete/no longer available, it is meaningful to keep it in the catalog (along with all the interaction data, which are very useful), and **only exclude the item from recommendations**. In such a case, use [ReQL filter](https://docs.recombee.com/reql.html) instead of deleting the item completely.
1616
*/
1717
class DeleteItem extends Request {
1818

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/*
3+
This file is auto-generated, do not edit
4+
*/
5+
6+
/**
7+
* DeleteMoreItems request
8+
*/
9+
namespace Recombee\RecommApi\Requests;
10+
use Recombee\RecommApi\Exceptions\UnknownOptionalParameterException;
11+
12+
/**
13+
* Delete all the items that pass the filter.
14+
* If an item becomes obsolete/no longer available, it is meaningful to **keep it in the catalog** (along with all the interaction data, which are very useful), and **only exclude the item from recommendations**. In such a case, use [ReQL filter](https://docs.recombee.com/reql.html) instead of deleting the item completely.
15+
*/
16+
class DeleteMoreItems extends Request {
17+
18+
/**
19+
* @var string $filter A [ReQL](https://docs.recombee.com/reql.html) expression, which return `true` for the items that shall be updated.
20+
*/
21+
protected $filter;
22+
23+
/**
24+
* Construct the request
25+
* @param string $filter A [ReQL](https://docs.recombee.com/reql.html) expression, which return `true` for the items that shall be updated.
26+
*/
27+
public function __construct($filter) {
28+
$this->filter = $filter;
29+
$this->timeout = 1000;
30+
$this->ensure_https = false;
31+
}
32+
33+
/**
34+
* Get used HTTP method
35+
* @return static Used HTTP method
36+
*/
37+
public function getMethod() {
38+
return Request::HTTP_DELETE;
39+
}
40+
41+
/**
42+
* Get URI to the endpoint
43+
* @return string URI to the endpoint
44+
*/
45+
public function getPath() {
46+
return "/{databaseId}/more-items/";
47+
}
48+
49+
/**
50+
* Get query parameters
51+
* @return array Values of query parameters (name of parameter => value of the parameter)
52+
*/
53+
public function getQueryParameters() {
54+
$params = array();
55+
return $params;
56+
}
57+
58+
/**
59+
* Get body parameters
60+
* @return array Values of body parameters (name of parameter => value of the parameter)
61+
*/
62+
public function getBodyParameters() {
63+
$p = array();
64+
$p['filter'] = $this->filter;
65+
return $p;
66+
}
67+
68+
}
69+
?>

src/RecommApi/Requests/DeleteUser.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
class DeleteUser extends Request {
1717

1818
/**
19-
* @var string $user_id ID of the user to be added.
19+
* @var string $user_id ID of the user to be deleted.
2020
*/
2121
protected $user_id;
2222

2323
/**
2424
* Construct the request
25-
* @param string $user_id ID of the user to be added.
25+
* @param string $user_id ID of the user to be deleted.
2626
*/
2727
public function __construct($user_id) {
2828
$this->user_id = $user_id;

0 commit comments

Comments
 (0)