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