Skip to content

Commit e6fc058

Browse files
committed
Added distinctRecomms parameter to batch request
1 parent 8ba155a commit e6fc058

File tree

9 files changed

+43
-11
lines changed

9 files changed

+43
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ or
1717
```
1818
{
1919
"require": {
20-
"recombee/php-api-client": ">=1.2.3"
20+
"recombee/php-api-client": ">=1.2.4"
2121
}
2222
}
2323
```

src/RecommApi/Requests/Batch.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Recombee\RecommApi\Util\Util;
1111

1212
/**
13-
* In many cases, it may be desirable to execute multiple requests at once. By example, when synchronizing the catalog of items in periodical manner, you would have to execute a sequence of thousands of separate POST requests, which is very ineffective and may take a very long time to complete. Most notably, network latencies can make execution of such a sequence very slow and even if executed in multiple parallel threads, there will still be unreasonable overhead caused by the HTTP(s). To avoid the problems mentioned, batch processing may be used, encapsulating a sequence of requests into a single HTTP request.
13+
* In many cases, it may be desirable to execute multiple requests at once. For example, when synchronizing the catalog of items in a periodical manner, you would have to execute a sequence of thousands of separate POST requests, which is very ineffective and may take a very long time to complete. Most notably, network latencies can make execution of such a sequence very slow and even if executed in multiple parallel threads, there will still be unreasonable overhead caused by the HTTP(s). To avoid the problems mentioned, batch processing may be used, encapsulating a sequence of requests into a single HTTP request.
1414
* Batch processing allows you to submit arbitrary sequence of requests in form of JSON array. Any type of request from the above documentation may be used in the batch, and the batch may combine different types of requests arbitrarily as well.
1515
* Note that:
1616
* - executing the requests in a batch is equivalent as if they were executed one-by-one individually; there are, however, many optimizations to make batch execution as fast as possible,
@@ -21,16 +21,30 @@
2121
class Batch extends Request {
2222

2323
/**
24-
* @var Requst[] Requests contained in the batch
24+
* @var Request[] Requests contained in the batch
2525
*/
2626
public $requests;
2727

2828
/**
2929
* Construct the Batch request
3030
* @param Request[] $requests Array of Requests.
31+
* @param array $optional Optional parameters given as an array containing pairs name of the parameter => value
32+
* - Allowed parameters:
33+
* - *distinctRecomms*
34+
* - Type: bool
35+
* - Description: Makes all the recommended items for a certain user distinct among multiple recommendation requests in the batch.
36+
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
3137
*/
32-
public function __construct($requests) {
38+
public function __construct($requests, $optional=array()) {
3339
$this->requests = $requests;
40+
$this->optional = $optional;
41+
42+
$existing_optional = array('distinctRecomms');
43+
foreach ($this->optional as $key => $value) {
44+
if (!in_array($key, $existing_optional))
45+
throw new UnknownOptionalParameterException($key);
46+
}
47+
3448
$this->timeout = null;
3549
$this->ensure_https = true;
3650
}
@@ -84,7 +98,13 @@ public function getBodyParameters() {
8498
foreach ($this->requests as $r) {
8599
array_push($reqs, $this->requestToBatchArray($r));
86100
}
87-
return ['requests' => $reqs];
101+
102+
$result = ['requests' => $reqs];
103+
104+
if(isset($this->optional['distinctRecomms']))
105+
$result['distinctRecomms'] = $this->optional['distinctRecomms'];
106+
107+
return $result;
88108
}
89109

90110
protected function requestToBatchArray($req) {

src/RecommApi/Requests/ListGroups.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ListGroups extends Request {
1919
* Construct the request
2020
*/
2121
public function __construct() {
22-
$this->timeout = 1000;
22+
$this->timeout = 30000;
2323
$this->ensure_https = false;
2424
}
2525

src/RecommApi/Requests/ListItems.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct($optional = array()) {
4141
if (!in_array($key, $existing_optional))
4242
throw new UnknownOptionalParameterException($key);
4343
}
44-
$this->timeout = 1000;
44+
$this->timeout = 30000;
4545
$this->ensure_https = false;
4646
}
4747

src/RecommApi/Requests/ListSeries.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ListSeries extends Request {
1919
* Construct the request
2020
*/
2121
public function __construct() {
22-
$this->timeout = 1000;
22+
$this->timeout = 30000;
2323
$this->ensure_https = false;
2424
}
2525

src/RecommApi/Requests/ListUsers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ListUsers extends Request {
1919
* Construct the request
2020
*/
2121
public function __construct() {
22-
$this->timeout = 1000;
22+
$this->timeout = 30000;
2323
$this->ensure_https = false;
2424
}
2525

src/RecommApi/Requests/MergeUsers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
/**
1313
* Merges purchases, ratings, bookmarks, and detail views of two different users under a single user ID. This is especially useful for online e-commerce applications working with anonymous users identified by unique tokens such as the session ID. In such applications, it may often happen that a user owns a persistent account, yet accesses the system anonymously while, e.g., putting items into a shopping cart. At some point in time, such as when the user wishes to confirm the purchase, (s)he logs into the system using his/her username and password. The interactions made under anonymous session ID then become connected with the persistent account, and merging these two together becomes desirable.
14-
* Merging happens between two users referred to as the *source* and the *target*. After the merge, all the interactions of the source user are attributed to the target user, and the source user is **deleted** unless special parameter `keepSourceUser` is set `true`.
14+
* Merging happens between two users referred to as the *target* and the *source*. After the merge, all the interactions of the source user are attributed to the target user, and the source user is **deleted** unless special parameter `keepSourceUser` is set `true`.
1515
*/
1616
class MergeUsers extends Request {
1717

tests/AddInteractionTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ public function testAddInteraction() {
2727
$resp = $this->client->send($req);
2828

2929
//it fails with nonexisting item id
30+
$req = $this->createRequest('entity_id','nonex_id');
31+
try {
32+
33+
$this->client->send($req);
34+
throw new \Exception('Exception was not thrown');
35+
}
36+
catch(Exc\ResponseException $e)
37+
{
38+
$this->assertEquals(404, $e->status_code);
39+
}
40+
41+
//it fails with nonexisting user id
3042
$req = $this->createRequest('nonex_id','entity_id');
3143
try {
3244

tests/BatchTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testBatch() {
3232
'allowNonexistent' => true])
3333
];
3434

35-
$repl = $this->client->send(new Reqs\Batch($reqs));
35+
$repl = $this->client->send(new Reqs\Batch($reqs, ['distinctRecomms' => true]));
3636

3737
$codes = array();
3838
foreach ($repl as $r) {

0 commit comments

Comments
 (0)