1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yandex PHP Library |
4
|
|
|
* |
5
|
|
|
* @copyright NIX Solutions Ltd. |
6
|
|
|
* @link https://github.com/nixsolutions/yandex-php-library |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @namespace |
11
|
|
|
*/ |
12
|
|
|
namespace Yandex\DataSync; |
13
|
|
|
|
14
|
|
|
use Psr\Http\Message\UriInterface; |
15
|
|
|
use Yandex\Common\AbstractServiceClient; |
16
|
|
|
use GuzzleHttp\Psr7\Response; |
17
|
|
|
use GuzzleHttp\Exception\ClientException; |
18
|
|
|
use Yandex\Common\Exception\ForbiddenException; |
19
|
|
|
use Yandex\Common\Exception\IncorrectDataFormatException; |
20
|
|
|
use Yandex\DataSync\Exception\IncorrectRevisionNumberException; |
21
|
|
|
use Yandex\Common\Exception\InvalidArgumentException; |
22
|
|
|
use Yandex\DataSync\Exception\MaxDatabasesCountException; |
23
|
|
|
use Yandex\Common\Exception\NotFoundException; |
24
|
|
|
use Yandex\DataSync\Exception\RevisionOnServerOverCurrentException; |
25
|
|
|
use Yandex\DataSync\Exception\RevisionTooOldException; |
26
|
|
|
use Yandex\Common\Exception\TooManyRequestsException; |
27
|
|
|
use Yandex\Common\Exception\UnauthorizedException; |
28
|
|
|
use Yandex\Common\Exception\UnavailableResourceException; |
29
|
|
|
use Yandex\DataSync\Exception\DataSyncException; |
30
|
|
|
use Yandex\DataSync\Models\Database; |
31
|
|
|
use Yandex\DataSync\Responses\DatabaseDeltasResponse; |
32
|
|
|
use Yandex\DataSync\Responses\DatabaseSnapshotResponse; |
33
|
|
|
use Yandex\DataSync\Responses\DatabasesResponse; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class DataSyncClient |
37
|
|
|
* |
38
|
|
|
* @category Yandex |
39
|
|
|
* @package DataSync |
40
|
|
|
* |
41
|
|
|
* @author Alexander Khaylo <[email protected]> |
42
|
|
|
* @created 01.03.15 12:07 |
43
|
|
|
*/ |
44
|
|
|
class DataSyncClient extends AbstractServiceClient |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* DB app context. |
48
|
|
|
*/ |
49
|
|
|
const CONTEXT_APP = 'app'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* DB user context. |
53
|
|
|
*/ |
54
|
|
|
const CONTEXT_USER = 'user'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Requested version of API |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $version = 'v1'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* API domain |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $serviceDomain = 'cloud-api.yandex.net'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $context; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
private $databaseId; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
* @throws InvalidArgumentException |
83
|
|
|
*/ |
84
|
27 |
|
public function getDatabaseId() |
85
|
|
|
{ |
86
|
27 |
|
if (!$this->databaseId) { |
87
|
1 |
|
throw new InvalidArgumentException('Empty database id'); |
88
|
|
|
} |
89
|
|
|
|
90
|
26 |
|
return $this->databaseId; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $databaseId |
95
|
|
|
*/ |
96
|
26 |
|
public function setDatabaseId($databaseId) |
97
|
|
|
{ |
98
|
26 |
|
$this->databaseId = $databaseId; |
99
|
26 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
* @throws InvalidArgumentException |
104
|
|
|
*/ |
105
|
29 |
|
public function getContext() |
106
|
|
|
{ |
107
|
29 |
|
if (!$this->context) { |
108
|
1 |
|
throw new InvalidArgumentException('Empty context'); |
109
|
|
|
} |
110
|
28 |
|
return $this->context; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $context |
115
|
|
|
* |
116
|
|
|
* @throws InvalidArgumentException |
117
|
|
|
*/ |
118
|
29 |
|
public function setContext($context) |
119
|
|
|
{ |
120
|
29 |
|
if ($context === self::CONTEXT_APP || $context === self::CONTEXT_USER) { |
121
|
28 |
|
$this->context = $context; |
122
|
28 |
|
} else { |
123
|
1 |
|
throw new InvalidArgumentException('Incorrect context'); |
124
|
|
|
} |
125
|
28 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $token access token |
129
|
|
|
* @param null $context |
130
|
|
|
* @param null $databaseId |
131
|
|
|
*/ |
132
|
31 |
|
public function __construct($token = '', $context = null, $databaseId = null) |
133
|
|
|
{ |
134
|
31 |
|
$this->setAccessToken($token); |
135
|
|
|
|
136
|
31 |
|
if ($context) { |
137
|
2 |
|
$this->setContext($context); |
138
|
1 |
|
} |
139
|
|
|
|
140
|
30 |
|
if ($databaseId) { |
141
|
1 |
|
$this->setDatabaseId($databaseId); |
142
|
1 |
|
} |
143
|
30 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param null|string $context |
147
|
|
|
* @param array $fields |
148
|
|
|
* @param null $limit |
149
|
|
|
* @param null $offset |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
* @throws InvalidArgumentException |
153
|
|
|
*/ |
154
|
2 |
|
protected function getDatabasesUrl($context = null, $fields = [], $limit = null, $offset = null) |
155
|
|
|
{ |
156
|
2 |
|
if ($context) { |
|
|
|
|
157
|
2 |
|
$this->setContext($context); |
158
|
2 |
|
} |
159
|
|
|
|
160
|
2 |
|
$params = []; |
161
|
2 |
|
if ($limit) { |
162
|
1 |
|
$params['limit'] = $limit; |
163
|
1 |
|
} |
164
|
2 |
|
if ($offset) { |
165
|
1 |
|
$params['offset'] = $offset; |
166
|
1 |
|
} |
167
|
2 |
|
if ($fields) { |
|
|
|
|
168
|
1 |
|
$params['fields'] = implode(',', $fields); |
169
|
1 |
|
} |
170
|
|
|
|
171
|
2 |
|
return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/' |
172
|
2 |
|
. $this->getContext() . '/databases/?' . http_build_query($params); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param null|string $databaseId |
177
|
|
|
* @param null|string $context |
178
|
|
|
* @param array $fields |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
* @throws InvalidArgumentException |
182
|
|
|
*/ |
183
|
19 |
|
protected function getDatabaseUrl($databaseId = null, $context = null, $fields = []) |
184
|
|
|
{ |
185
|
19 |
|
if ($context) { |
|
|
|
|
186
|
19 |
|
$this->setContext($context); |
187
|
19 |
|
} |
188
|
19 |
|
if ($databaseId) { |
|
|
|
|
189
|
19 |
|
$this->setDatabaseId($databaseId); |
190
|
19 |
|
} |
191
|
|
|
|
192
|
19 |
|
$params = []; |
193
|
19 |
|
if ($fields) { |
|
|
|
|
194
|
1 |
|
$params['fields'] = implode(',', $fields); |
195
|
1 |
|
} |
196
|
|
|
|
197
|
19 |
|
return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/' |
198
|
19 |
|
. $this->getContext() . '/databases/' . $this->getDatabaseId() . '/?' . http_build_query($params); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param null|string $databaseId |
203
|
|
|
* @param null|string $context |
204
|
|
|
* @param null $collectionId |
205
|
|
|
* @param array $fields |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
* @throws InvalidArgumentException |
209
|
|
|
*/ |
210
|
2 |
|
protected function getDatabaseSnapshotUrl($databaseId = null, $context = null, $collectionId = null, $fields = []) |
211
|
|
|
{ |
212
|
2 |
|
if ($context) { |
|
|
|
|
213
|
2 |
|
$this->setContext($context); |
214
|
2 |
|
} |
215
|
2 |
|
if ($databaseId) { |
|
|
|
|
216
|
2 |
|
$this->setDatabaseId($databaseId); |
217
|
2 |
|
} |
218
|
|
|
|
219
|
2 |
|
$params = []; |
220
|
2 |
|
if ($collectionId) { |
221
|
1 |
|
$params['collection_id'] = $collectionId; |
222
|
1 |
|
} |
223
|
2 |
|
if ($fields) { |
|
|
|
|
224
|
1 |
|
$params['fields'] = implode(',', $fields); |
225
|
1 |
|
} |
226
|
|
|
|
227
|
2 |
|
return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/' |
228
|
2 |
|
. $this->getContext() . '/databases/' . $this->getDatabaseId() . '/snapshot/?' . http_build_query($params); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param null|string $databaseId |
233
|
|
|
* @param null|string $context |
234
|
|
|
* @param array $fields |
235
|
|
|
* @param int $baseRevision |
236
|
|
|
* @param int $limit |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
* @throws InvalidArgumentException |
240
|
|
|
*/ |
241
|
4 |
|
protected function getDatabaseDeltasUrl( |
242
|
|
|
$databaseId = null, |
243
|
|
|
$context = null, |
244
|
|
|
$fields = [], |
245
|
|
|
$baseRevision = null, |
246
|
|
|
$limit = null |
247
|
|
|
) { |
248
|
4 |
|
if ($context) { |
|
|
|
|
249
|
4 |
|
$this->setContext($context); |
250
|
4 |
|
} |
251
|
4 |
|
if ($databaseId) { |
|
|
|
|
252
|
4 |
|
$this->setDatabaseId($databaseId); |
253
|
4 |
|
} |
254
|
4 |
|
$params = []; |
255
|
4 |
|
if ($baseRevision !== null) { |
256
|
2 |
|
$params['base_revision'] = $baseRevision; |
257
|
2 |
|
} |
258
|
4 |
|
if ($limit) { |
|
|
|
|
259
|
2 |
|
$params['limit'] = $limit; |
260
|
2 |
|
} |
261
|
4 |
|
if ($fields) { |
|
|
|
|
262
|
2 |
|
$params['fields'] = implode(',', $fields); |
263
|
2 |
|
} |
264
|
|
|
|
265
|
4 |
|
return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/' |
266
|
4 |
|
. $this->getContext() . '/databases/' . $this->getDatabaseId() . '/deltas?' . http_build_query($params); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Sends a request |
271
|
|
|
* |
272
|
|
|
* @param string $method HTTP method |
273
|
|
|
* @param UriInterface|string $uri URI object or string. |
274
|
|
|
* @param array $options Request options to apply. |
275
|
|
|
* |
276
|
|
|
* @return Response|\Psr\Http\Message\ResponseInterface |
277
|
|
|
* @throws DataSyncException |
278
|
|
|
* @throws ForbiddenException |
279
|
|
|
* @throws IncorrectDataFormatException |
280
|
|
|
* @throws IncorrectRevisionNumberException |
281
|
|
|
* @throws InvalidArgumentException |
282
|
|
|
* @throws MaxDatabasesCountException |
283
|
|
|
* @throws NotFoundException |
284
|
|
|
* @throws RevisionOnServerOverCurrentException |
285
|
|
|
* @throws RevisionTooOldException |
286
|
|
|
* @throws TooManyRequestsException |
287
|
|
|
* @throws UnauthorizedException |
288
|
|
|
* @throws UnavailableResourceException |
289
|
|
|
*/ |
290
|
14 |
|
protected function sendRequest($method, $uri, array $options = []) |
291
|
|
|
{ |
292
|
|
|
try { |
293
|
14 |
|
$response = $this->getClient()->request($method, $uri, $options); |
|
|
|
|
294
|
14 |
|
} catch (ClientException $ex) { |
295
|
13 |
|
$result = $ex->getResponse(); |
296
|
13 |
|
$code = $result->getStatusCode(); |
297
|
13 |
|
$message = $result->getReasonPhrase(); |
298
|
|
|
|
299
|
|
|
switch ($code) { |
300
|
13 |
|
case 400: |
301
|
1 |
|
throw new InvalidArgumentException($message); |
302
|
12 |
|
case 401: |
303
|
1 |
|
throw new UnauthorizedException($message); |
304
|
11 |
|
case 403: |
305
|
1 |
|
throw new ForbiddenException($message); |
306
|
10 |
|
case 404: |
307
|
1 |
|
throw new NotFoundException($message); |
308
|
9 |
|
case 406: |
309
|
1 |
|
throw new IncorrectDataFormatException($message); |
310
|
8 |
|
case 409: |
311
|
1 |
|
throw new RevisionOnServerOverCurrentException($message); |
312
|
7 |
|
case 410: |
313
|
1 |
|
throw new RevisionTooOldException($message); |
314
|
6 |
|
case 412: |
315
|
1 |
|
throw new IncorrectRevisionNumberException($message); |
316
|
5 |
|
case 415: |
317
|
1 |
|
throw new IncorrectDataFormatException($message); |
318
|
4 |
|
case 423: |
319
|
1 |
|
throw new UnavailableResourceException($message); |
320
|
3 |
|
case 429: |
321
|
1 |
|
throw new TooManyRequestsException($message); |
322
|
2 |
|
case 507: |
323
|
1 |
|
throw new MaxDatabasesCountException($message); |
324
|
|
|
} |
325
|
|
|
|
326
|
1 |
|
throw new DataSyncException( |
327
|
1 |
|
'Service responded with error code: "' . $code . '" and message: "' . $message . '"', |
328
|
|
|
$code |
329
|
14 |
|
); |
330
|
|
|
} |
331
|
|
|
|
332
|
1 |
|
return $response; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param null|string $context |
337
|
|
|
* @param array $fields |
338
|
|
|
* @param null $limit |
339
|
|
|
* @param null $offset |
340
|
|
|
* |
341
|
|
|
* @return DatabasesResponse |
342
|
|
|
* @throws DataSyncException |
343
|
|
|
* @throws ForbiddenException |
344
|
|
|
* @throws IncorrectDataFormatException |
345
|
|
|
* @throws InvalidArgumentException |
346
|
|
|
* @throws MaxDatabasesCountException |
347
|
|
|
* @throws NotFoundException |
348
|
|
|
* @throws TooManyRequestsException |
349
|
|
|
* @throws UnauthorizedException |
350
|
|
|
* @throws UnavailableResourceException |
351
|
|
|
*/ |
352
|
2 |
|
public function getDatabases($context = null, $fields = [], $limit = null, $offset = null) |
353
|
|
|
{ |
354
|
2 |
|
$response = $this->sendRequest('GET', $this->getDatabasesUrl($context, $fields, $limit, $offset)); |
355
|
2 |
|
$decodedResponseBody = $this->getDecodedBody($response->getBody()); |
356
|
2 |
|
$databasesResponse = new DatabasesResponse($decodedResponseBody); |
357
|
2 |
|
if ($databasesResponse->getItems()) { |
358
|
1 |
|
$databases = $databasesResponse->getItems()->getAll(); |
359
|
1 |
|
foreach ($databases as $database) { |
360
|
1 |
|
$database->setContext($this->getContext()); |
361
|
1 |
|
} |
362
|
1 |
|
} |
363
|
|
|
|
364
|
2 |
|
return $databasesResponse; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param null|string $databaseId |
369
|
|
|
* @param null|string $context |
370
|
|
|
* @param array $fields |
371
|
|
|
* |
372
|
|
|
* @return Database |
373
|
|
|
* @throws DataSyncException |
374
|
|
|
* @throws ForbiddenException |
375
|
|
|
* @throws IncorrectDataFormatException |
376
|
|
|
* @throws InvalidArgumentException |
377
|
|
|
* @throws MaxDatabasesCountException |
378
|
|
|
* @throws NotFoundException |
379
|
|
|
* @throws TooManyRequestsException |
380
|
|
|
* @throws UnauthorizedException |
381
|
|
|
* @throws UnavailableResourceException |
382
|
|
|
*/ |
383
|
1 |
View Code Duplication |
public function createDatabase($databaseId = null, $context = null, $fields = []) |
|
|
|
|
384
|
|
|
{ |
385
|
1 |
|
$response = $this->sendRequest('PUT', $this->getDatabaseUrl($databaseId, $context, $fields)); |
386
|
1 |
|
$decodedResponseBody = $this->getDecodedBody($response->getBody()); |
387
|
1 |
|
$database = new Database($decodedResponseBody); |
388
|
1 |
|
$database->setContext($this->getContext()); |
389
|
1 |
|
return $database; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* @param null|string $databaseId |
394
|
|
|
* @param null|string $context |
395
|
|
|
* @param array $fields |
396
|
|
|
* |
397
|
|
|
* @return Database |
398
|
|
|
* @throws DataSyncException |
399
|
|
|
* @throws ForbiddenException |
400
|
|
|
* @throws IncorrectDataFormatException |
401
|
|
|
* @throws InvalidArgumentException |
402
|
|
|
* @throws MaxDatabasesCountException |
403
|
|
|
* @throws NotFoundException |
404
|
|
|
* @throws TooManyRequestsException |
405
|
|
|
* @throws UnauthorizedException |
406
|
|
|
* @throws UnavailableResourceException |
407
|
|
|
*/ |
408
|
16 |
View Code Duplication |
public function getDatabase($databaseId = null, $context = null, $fields = []) |
|
|
|
|
409
|
|
|
{ |
410
|
16 |
|
$response = $this->sendRequest('GET', $this->getDatabaseUrl($databaseId, $context, $fields)); |
411
|
3 |
|
$decodedResponseBody = $this->getDecodedBody($response->getBody()); |
412
|
3 |
|
$database = new Database($decodedResponseBody); |
413
|
3 |
|
$database->setContext($this->getContext()); |
414
|
3 |
|
return $database; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @param null|string $databaseId |
419
|
|
|
* @param null|string $context |
420
|
|
|
* |
421
|
|
|
* @return bool |
422
|
|
|
* |
423
|
|
|
* @throws DataSyncException |
424
|
|
|
* @throws ForbiddenException |
425
|
|
|
* @throws IncorrectDataFormatException |
426
|
|
|
* @throws InvalidArgumentException |
427
|
|
|
* @throws MaxDatabasesCountException |
428
|
|
|
* @throws NotFoundException |
429
|
|
|
* @throws TooManyRequestsException |
430
|
|
|
* @throws UnauthorizedException |
431
|
|
|
* @throws UnavailableResourceException |
432
|
|
|
*/ |
433
|
1 |
|
public function deleteDatabase($databaseId = null, $context = null) |
434
|
|
|
{ |
435
|
1 |
|
$response = $this->sendRequest('DELETE', $this->getDatabaseUrl($databaseId, $context)); |
436
|
1 |
|
return $response->getStatusCode() === 204; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @param string $title |
441
|
|
|
* @param null $databaseId |
442
|
|
|
* @param null $context |
443
|
|
|
* @param array $fields |
444
|
|
|
* |
445
|
|
|
* @return Database |
446
|
|
|
* @throws DataSyncException |
447
|
|
|
* @throws ForbiddenException |
448
|
|
|
* @throws IncorrectDataFormatException |
449
|
|
|
* @throws InvalidArgumentException |
450
|
|
|
* @throws MaxDatabasesCountException |
451
|
|
|
* @throws NotFoundException |
452
|
|
|
* @throws TooManyRequestsException |
453
|
|
|
* @throws UnauthorizedException |
454
|
|
|
* @throws UnavailableResourceException |
455
|
|
|
*/ |
456
|
1 |
|
public function updateDatabaseTitle($title, $databaseId = null, $context = null, $fields = []) |
457
|
|
|
{ |
458
|
1 |
|
$response = $this->sendRequest( |
459
|
1 |
|
'PATCH', |
460
|
1 |
|
$this->getDatabaseUrl($databaseId, $context, $fields), |
461
|
|
|
[ |
462
|
1 |
|
'json' => ['title' => $title] |
463
|
1 |
|
] |
464
|
1 |
|
); |
465
|
1 |
|
$decodedResponseBody = $this->getDecodedBody($response->getBody()); |
466
|
1 |
|
$database = new Database($decodedResponseBody); |
467
|
1 |
|
$database->setContext($this->getContext()); |
468
|
1 |
|
return $database; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* @param null|string $databaseId |
473
|
|
|
* @param null|string $context |
474
|
|
|
* @param null $collectionId |
475
|
|
|
* @param array $fields |
476
|
|
|
* |
477
|
|
|
* @return DatabaseSnapshotResponse |
478
|
|
|
* @throws DataSyncException |
479
|
|
|
* @throws ForbiddenException |
480
|
|
|
* @throws IncorrectDataFormatException |
481
|
|
|
* @throws InvalidArgumentException |
482
|
|
|
* @throws MaxDatabasesCountException |
483
|
|
|
* @throws NotFoundException |
484
|
|
|
* @throws TooManyRequestsException |
485
|
|
|
* @throws UnauthorizedException |
486
|
|
|
* @throws UnavailableResourceException |
487
|
|
|
*/ |
488
|
2 |
View Code Duplication |
public function getDatabaseSnapshot($databaseId = null, $context = null, $collectionId = null, $fields = []) |
|
|
|
|
489
|
|
|
{ |
490
|
2 |
|
$response = $this->sendRequest( |
491
|
2 |
|
'GET', |
492
|
2 |
|
$this->getDatabaseSnapshotUrl($databaseId, $context, $collectionId, $fields) |
493
|
2 |
|
); |
494
|
2 |
|
$decodedResponseBody = $this->getDecodedBody($response->getBody()); |
495
|
2 |
|
$result = new DatabaseSnapshotResponse($decodedResponseBody); |
496
|
2 |
|
return $result; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* @param array $data |
501
|
|
|
* @param int $revision |
502
|
|
|
* @param null|string $databaseId |
503
|
|
|
* @param null|string $context |
504
|
|
|
* @param array $fields |
505
|
|
|
* |
506
|
|
|
* @return array |
507
|
|
|
* @throws DataSyncException |
508
|
|
|
* @throws ForbiddenException |
509
|
|
|
* @throws IncorrectDataFormatException |
510
|
|
|
* @throws IncorrectRevisionNumberException |
511
|
|
|
* @throws InvalidArgumentException |
512
|
|
|
* @throws MaxDatabasesCountException |
513
|
|
|
* @throws NotFoundException |
514
|
|
|
* @throws RevisionOnServerOverCurrentException |
515
|
|
|
* @throws RevisionTooOldException |
516
|
|
|
* @throws TooManyRequestsException |
517
|
|
|
* @throws UnauthorizedException |
518
|
|
|
* @throws UnavailableResourceException |
519
|
|
|
* |
520
|
|
|
* @see https://tech.yandex.ru/datasync/http/doc/tasks/add-changes-docpage/ |
521
|
|
|
*/ |
522
|
2 |
|
public function saveDelta($data, $revision = 0, $databaseId = null, $context = null, $fields = []) |
523
|
|
|
{ |
524
|
|
|
$options = [ |
525
|
|
|
'headers' => [ |
526
|
2 |
|
'If-Match' => $revision, |
527
|
2 |
|
], |
528
|
|
|
'json' => $data |
529
|
2 |
|
]; |
530
|
2 |
|
$response = $this->sendRequest( |
531
|
2 |
|
'POST', |
532
|
2 |
|
$this->getDatabaseDeltasUrl($databaseId, $context, $fields), |
533
|
|
|
$options |
534
|
2 |
|
); |
535
|
2 |
|
$decodedResponseBody = $this->getDecodedBody($response->getBody()); |
536
|
2 |
|
if ($response->getHeader('ETag') |
537
|
2 |
|
&& is_array($response->getHeader('ETag')) |
538
|
2 |
|
&& count($response->getHeader('ETag')) > 0 |
539
|
2 |
|
) { |
540
|
1 |
|
$decodedResponseBody['revision'] = $response->getHeader('ETag')[0]; |
541
|
1 |
|
} |
542
|
|
|
|
543
|
2 |
|
return $decodedResponseBody; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* @param int $baseRevision |
548
|
|
|
* @param null|string $databaseId |
549
|
|
|
* @param null|string $context |
550
|
|
|
* @param array $fields |
551
|
|
|
* @param null|int $limit |
552
|
|
|
* |
553
|
|
|
* @return DatabaseDeltasResponse |
554
|
|
|
* @throws DataSyncException |
555
|
|
|
* @throws ForbiddenException |
556
|
|
|
* @throws IncorrectDataFormatException |
557
|
|
|
* @throws IncorrectRevisionNumberException |
558
|
|
|
* @throws InvalidArgumentException |
559
|
|
|
* @throws MaxDatabasesCountException |
560
|
|
|
* @throws NotFoundException |
561
|
|
|
* @throws RevisionOnServerOverCurrentException |
562
|
|
|
* @throws RevisionTooOldException |
563
|
|
|
* @throws TooManyRequestsException |
564
|
|
|
* @throws UnauthorizedException |
565
|
|
|
* @throws UnavailableResourceException |
566
|
|
|
*/ |
567
|
2 |
View Code Duplication |
public function getDelta($baseRevision = 0, $databaseId = null, $context = null, $fields = [], $limit = null) |
|
|
|
|
568
|
|
|
{ |
569
|
2 |
|
$response = $this->sendRequest( |
570
|
2 |
|
'GET', |
571
|
2 |
|
$this->getDatabaseDeltasUrl($databaseId, $context, $fields, $baseRevision, $limit) |
572
|
2 |
|
); |
573
|
2 |
|
$decodedResponseBody = $this->getDecodedBody($response->getBody()); |
574
|
2 |
|
$result = new DatabaseDeltasResponse($decodedResponseBody); |
575
|
2 |
|
return $result; |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: