-
Notifications
You must be signed in to change notification settings - Fork 36
/
Client.php
324 lines (287 loc) · 8.42 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
namespace GetStream\Stream;
use DateTime;
use Exception;
const VERSION = '2.6.0';
class Client implements ClientInterface
{
const API_ENDPOINT = 'stream-io-api.com/api';
/**
* @var string
*/
protected $api_key;
/**
* @var string
*/
protected $api_secret;
/**
* @var string
*/
protected $location;
/**
* @var string
*/
protected $protocol;
/**
* @var Signer
*/
public $signer;
/**
* @var string
*/
public $api_version;
/**
* @var float
*/
public $timeout;
/**
* @param string $api_key
* @param string $api_secret
* @param string $api_version
* @param string $location
* @param float $timeout
*/
public function __construct($api_key, $api_secret, $api_version='v1.0', $location='', $timeout=3.0)
{
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->signer = new Signer($api_key, $api_secret);
$this->api_version = $api_version;
$this->timeout = $timeout;
$this->location = $location;
$this->protocol = 'https';
}
/**
* @param string|null $url
* @return Client
* @throws Exception
*/
public static function herokuConnect($url = null)
{
if ($url === null) {
$url = getenv('STREAM_URL');
}
$parsed_url = parse_url($url);
$api_key = $parsed_url['user'];
$api_secret = $parsed_url['pass'];
if ($api_key == '' || $api_secret == '') {
throw new Exception('url malformed');
}
$client = new static($api_key, $api_secret);
$location = explode('stream-io-api.com', $parsed_url['host'])[0];
$location = str_replace('.', '', $location);
$client->setLocation($location);
return $client;
}
/**
* @param string $protocol
*/
public function setProtocol($protocol)
{
$this->protocol = $protocol;
}
/**
* @param string $location
*/
public function setLocation($location)
{
$this->location = $location;
}
/**
* @param string $user_id
* @param array $extra_data
* @return string
*/
public function createUserSessionToken($user_id, array $extra_data=null)
{
if(is_null($extra_data)){
$extra_data = array();
}
return $this->createUserToken($user_id, $extra_data);
}
/**
* @param string $user_id
* @param array $extra_data
* @return string
*/
public function createUserToken($user_id, array $extra_data=null)
{
if(is_null($extra_data)){
$extra_data = array();
}
return $this->signer->jwtUserSessionToken($user_id, $extra_data);
}
/**
* @param BaseFeedInterface $feed
* @param string $resource
* @param string $action
* @return string
*/
public function createFeedJWTToken($feed, $resource, $action)
{
$feedId = "{$feed->getSlug()}{$feed->getUserId()}";
return $this->signer->jwtScopeToken($feedId, $resource, $action);
}
/**
* @param string $feed_slug
* @param string $user_id
* @param string|null $token
* @return FeedInterface
*/
public function feed($feed_slug, $user_id, $token = null)
{
if (null === $token) {
$token = $this->signer->signature($feed_slug . $user_id);
}
return new Feed($this, $feed_slug, $user_id, $this->api_key, $token);
}
/**
* @return Batcher
*/
public function batcher()
{
return new Batcher($this, $this->signer, $this->api_key);
}
/**
* @return Personalization
*/
public function personalization()
{
return new Personalization($this, $this->api_key, $this->api_secret);
}
/**
* @return Collections
*/
public function collections()
{
return new Collections($this, $this->api_key, $this->api_secret);
}
/**
* @return Reactions
*/
public function reactions()
{
return new Reactions($this, $this->api_key, $this->api_secret);
}
/**
* @return Users
*/
public function users()
{
return new Users($this, $this->api_key, $this->api_secret);
}
/**
* @return string
*/
public function getBaseUrl()
{
$baseUrl = getenv('STREAM_BASE_URL');
if (!$baseUrl) {
$api_endpoint = static::API_ENDPOINT;
$localPort = getenv('STREAM_LOCAL_API_PORT');
if ($localPort) {
$baseUrl = "http://localhost:$localPort/api";
} else {
if ($this->location) {
$subdomain = "{$this->location}-api";
} else {
$subdomain = 'api';
}
$baseUrl = "{$this->protocol}://{$subdomain}." . $api_endpoint;
}
}
return $baseUrl;
}
/**
* @param string $uri
* @return string
*/
public function buildRequestUrl($uri)
{
$baseUrl = $this->getBaseUrl();
return "{$baseUrl}/{$this->api_version}/{$uri}";
}
public function getActivities($ids=null, $foreign_id_times=null)
{
if($ids!==null){
$query_params = ["ids" => join(',', $ids)];
} else {
$fids = [];
$times = [];
foreach($foreign_id_times as $fit){
$fids[] = $fit[0];
try {
$times[] = $fit[1]->format(DateTime::ISO8601);
} catch(Exception $e) {
// assume it's in the right format already
$times[] = $fit[1];
}
}
$query_params = [
"foreign_ids" => join(',', $fids),
"timestamps" => join(',', $times)
];
}
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
$activities = new Activities($this, $this->api_key, $token);
return $activities->_getActivities($query_params);
}
public function batchPartialActivityUpdate($data)
{
if(count($data) > 100){
throw new Exception("Max 100 activities allowed in batch update");
}
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
$activityUpdateOp = new ActivityUpdateOperation($this, $this->api_key, $token);
return $activityUpdateOp->partiallyUpdateActivity(["changes" => $data]);
}
public function doPartialActivityUpdate($id=null, $foreign_id=null, $time=null, $set=null, $unset=null)
{
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
if($id === null && ($foreign_id === null || $time === null)){
throw new Exception(
"The id or foreign_id+time parameters must be provided and not be None"
);
}
if($id !== null && ($foreign_id !== null || $time !== null)){
throw new Exception(
"Only one of the id or the foreign_id+time parameters can be provided"
);
}
$data = ["set" => $set, "unset" => $unset];
if($id !== null){
$data["id"] = $id;
} else {
$data["foreign_id"] = $foreign_id;
$data["time"] = $time;
}
$activityUpdateOp = new ActivityUpdateOperation($this, $this->api_key, $token);
return $activityUpdateOp->partiallyUpdateActivity($data);
}
public function updateActivities($activities)
{
if (empty($activities)) {
return;
}
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
$activityUpdateOp = new ActivityUpdateOperation($this, $this->api_key, $token);
return $activityUpdateOp->updateActivities($activities);
}
public function updateActivity($activity)
{
return $this->updateActivities([$activity]);
}
/**
* Creates a redirect url for tracking the given events in the context of
* getstream.io/personalization
* @param string $targetUrl
* @param array $events
* @return string
*/
public function createRedirectUrl($targetUrl, $events)
{
$token = $this->signer->jwtScopeToken('*', 'redirect_and_track', '*');
$analytics = new Analytics($this, $this->api_key, $token);
return $analytics->createRedirectUrl($targetUrl, $events);
}
}