Skip to content

Commit 037df8a

Browse files
committed
Timestamp is now optional parameter when deleting an interaction. cascadeCreate and scenario parameters for recommendations. Choosing of HTTP/HTTPS protocol for requests.
1 parent fb717dc commit 037df8a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+264
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ or
1515
```
1616
{
1717
"require": {
18-
"recombee/php-api-client": ">=1.1"
18+
"recombee/php-api-client": ">=1.2"
1919
}
2020
}
2121
```

src/RecommApi/Client.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ class Client{
1919
protected $account;
2020
protected $token;
2121
protected $request;
22+
protected $protocol;
2223

2324
/**
2425
* @ignore
2526
*/
26-
const BASE_URI = 'https://rapi.recombee.com';
27+
const BASE_URI = 'rapi.recombee.com';
2728

2829
/**
2930
* Create the client
3031
* @param string $account Name of your account at Recombee
3132
* @param string $token Secret token
33+
* @param string $protocol Default protocol for sending requests. Possible values: 'http', 'https'.
3234
*/
33-
public function __construct($account, $token) {
35+
public function __construct($account, $token, $protocol = 'http') {
3436
$this->account = $account;
3537
$this->token = $token;
38+
$this->protocol = $protocol;
3639
}
3740

3841
/**
@@ -44,27 +47,30 @@ public function __construct($account, $token) {
4447
public function send(Requests\Request $request) {
4548
$this->request = $request;
4649
$path = Util::sliceDbName($request->getPath());
47-
$uri = $path . $this->paramsToStr($request->getQueryParameters());
50+
$request_url = $path . $this->paramsToStr($request->getQueryParameters());
51+
$signed_url = $this->signUrl($request_url);
52+
$protocol = ($request->getEnsureHttps()) ? 'https' : $this->protocol;
53+
$uri = $protocol . '://' . Client::BASE_URI . $signed_url;
4854
$timeout = $request->getTimeout() / 1000;
4955
$result = null;
5056

5157
try {
5258
switch ($request->getMethod()) {
5359
case Requests\Request::HTTP_GET:
54-
$result = $this->hmacGet($uri, $timeout);
60+
$result = $this->get($uri, $timeout);
5561
break;
5662

5763
case Requests\Request::HTTP_PUT:
58-
$result = $this->hmacPut($uri, $timeout);
64+
$result = $this->put($uri, $timeout);
5965
break;
6066

6167
case Requests\Request::HTTP_DELETE:
62-
$result = $this->hmacDelete($uri, $timeout);
68+
$result = $this->delete($uri, $timeout);
6369
break;
6470

6571
case Requests\Request::HTTP_POST:
6672
$json = json_encode($request->getBodyParameters(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
67-
$result = $this->hmacPost($uri, $timeout, $json);
73+
$result = $this->post($uri, $timeout, $json);
6874
break;
6975
}
7076
}
@@ -80,31 +86,27 @@ public function send(Requests\Request $request) {
8086

8187
/* ----------------------- Send requests ----------------------- */
8288
//TODO remove ceils when Requests 1.7 is released as it support timeout in milliseconds
83-
protected function hmacPut($uri, $timeout) {
84-
$signed_url = $this->signUrl($uri);
85-
$response = \Requests::put($signed_url, array(), ['timeout' => ceil($timeout)]);
89+
protected function put($uri, $timeout) {
90+
$response = \Requests::put($uri, array(), ['timeout' => ceil($timeout)]);
8691
$this->checkErrors($response);
8792
return $response->body;
8893
}
8994

90-
protected function hmacGet($uri, $timeout) {
91-
$signed_url = $this->signUrl($uri);
92-
$response = \Requests::get($signed_url, array(), ['timeout' => ceil($timeout)]);
95+
protected function get($uri, $timeout) {
96+
$response = \Requests::get($uri, array(), ['timeout' => ceil($timeout)]);
9397
$this->checkErrors($response);
9498
return json_decode($response->body, true);
9599
}
96100

97-
protected function hmacDelete($uri, $timeout) {
98-
$signed_url = $this->signUrl($uri);
99-
$response = \Requests::delete($signed_url, array(), ['timeout' => ceil($timeout)]);
101+
protected function delete($uri, $timeout) {
102+
$response = \Requests::delete($uri, array(), ['timeout' => ceil($timeout)]);
100103
$this->checkErrors($response);
101104
return $response->body;
102105
}
103106

104-
protected function hmacPost($uri, $timeout, $body) {
105-
$signed_url = $this->signUrl($uri);
107+
protected function post($uri, $timeout, $body) {
106108
$headers = array('Content-Type' => 'application/json');
107-
$response = \Requests::post($signed_url, $headers, $body, ['timeout' => ceil($timeout)]);
109+
$response = \Requests::post($uri, $headers, $body, ['timeout' => ceil($timeout)]);
108110
$this->checkErrors($response);
109111

110112
$json = json_decode($response->body, true);
@@ -126,7 +128,7 @@ protected function signUrl($uri) {
126128
$uri = '/' . $this->account . '/' . $uri;
127129
$time_str = $this->hmacTime($uri);
128130
$sign = $this->hmacSign($uri, $time_str);
129-
return Client::BASE_URI . $uri . $time_str . '&hmac_sign=' . $sign;
131+
return $uri . $time_str . '&hmac_sign=' . $sign;
130132
}
131133

132134
protected function hmacTime($uri) {

src/RecommApi/Requests/AddBookmark.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function __construct($user_id, $item_id, $optional = array()) {
6262
throw new UnknownOptionalParameterException($key);
6363
}
6464
$this->timeout = 1000;
65+
$this->ensure_https = false;
6566
}
6667

6768
/**

src/RecommApi/Requests/AddCartAddition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function __construct($user_id, $item_id, $optional = array()) {
6262
throw new UnknownOptionalParameterException($key);
6363
}
6464
$this->timeout = 1000;
65+
$this->ensure_https = false;
6566
}
6667

6768
/**

src/RecommApi/Requests/AddDetailView.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function __construct($user_id, $item_id, $optional = array()) {
7070
throw new UnknownOptionalParameterException($key);
7171
}
7272
$this->timeout = 1000;
73+
$this->ensure_https = false;
7374
}
7475

7576
/**

src/RecommApi/Requests/AddGroup.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class AddGroup extends Request {
2626
public function __construct($group_id) {
2727
$this->group_id = $group_id;
2828
$this->timeout = 1000;
29+
$this->ensure_https = false;
2930
}
3031

3132
/**

src/RecommApi/Requests/AddItem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AddItem extends Request {
2727
public function __construct($item_id) {
2828
$this->item_id = $item_id;
2929
$this->timeout = 1000;
30+
$this->ensure_https = false;
3031
}
3132

3233
/**

src/RecommApi/Requests/AddItemProperty.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct($property_name, $type) {
3232
$this->property_name = $property_name;
3333
$this->type = $type;
3434
$this->timeout = 1000;
35+
$this->ensure_https = false;
3536
}
3637

3738
/**

src/RecommApi/Requests/AddPurchase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function __construct($user_id, $item_id, $optional = array()) {
6262
throw new UnknownOptionalParameterException($key);
6363
}
6464
$this->timeout = 1000;
65+
$this->ensure_https = false;
6566
}
6667

6768
/**

src/RecommApi/Requests/AddRating.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function __construct($user_id, $item_id, $rating, $optional = array()) {
6868
throw new UnknownOptionalParameterException($key);
6969
}
7070
$this->timeout = 1000;
71+
$this->ensure_https = false;
7172
}
7273

7374
/**

0 commit comments

Comments
 (0)