/
PhotosLibraryClient.php
285 lines (254 loc) · 9.45 KB
/
PhotosLibraryClient.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
<?php
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @experimental
*/
namespace Google\Photos\Library\V1;
use Closure;
use Google\ApiCore\ApiException;
use Google\Photos\Library\V1\Gapic\PhotosLibraryGapicClient;
use Google\Photos\Types\Album;
use Google\Photos\Types\MediaItem;
use Google\Protobuf\FieldMask;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
/**
* {@inheritdoc}
*/
class PhotosLibraryClient extends PhotosLibraryGapicClient
{
private $httpClient;
private $uploadRetrySettings;
const UPLOAD_URL = 'https://photoslibrary.googleapis.com/v1/uploads';
/**
* Returns a function that determines if a given request should be retried.
*
* The returned function returns true if the status was not okay, the error was in one of the
* acceptable retry codes, and the maximum number of retries has not been exceeded.
* @param UploadRetrySettings $uploadRetrySettings
* @return Closure
*/
private function retryDecider($uploadRetrySettings)
{
return function ($retries, $request, $response, $error) use ($uploadRetrySettings) {
if ($retries > $uploadRetrySettings->maxNumRetries) {
return false;
}
if (null !== $error) {
foreach ($uploadRetrySettings->retryableExceptions as $retryableException) {
if ($error instanceof $retryableException) {
return true;
}
}
return false;
}
// The response body is either an upload token, or a JSON representation of the
// status. If it decodes into JSON, then it is not an upload token.
$status = json_decode($response->getBody());
if (null == $status) {
// Don't retry if an upload token was received.
return false;
}
// Otherwise, only retry on 'retryable codes'.
return in_array($status['code'], $uploadRetrySettings['retryableCodes']);
};
}
/**
* Returns a function that determines the delay before a request is retried.
*
* Implements exponential backoff, with some maximum delay.
* @param UploadRetrySettings $uploadRetrySettings
* @return Closure
*/
private function retryDelay($uploadRetrySettings)
{
return function ($retries, $response) use ($uploadRetrySettings) {
return min(
$uploadRetrySettings->maxRetryDelayMillis,
$uploadRetrySettings->initialRetryDelayMillis * pow(
$uploadRetrySettings->retryDelayMultiplier,
$retries
)
);
};
}
/**
* {@inheritdoc}
*/
public function __construct($options = [])
{
parent::__construct($options);
if (array_key_exists('uploadRetrySettings', $options)) {
$this->uploadRetrySettings = new UploadRetrySettings($options['uploadRetrySettings']);
} else {
$this->uploadRetrySettings = new UploadRetrySettings(['retriesEnabled' => false]);
}
$stack = HandlerStack::create();
if (null != $this->uploadRetrySettings && $this->uploadRetrySettings->retriesEnabled) {
$stack->push(Middleware::retry(
$this->retryDecider($this->uploadRetrySettings),
$this->retryDelay($this->uploadRetrySettings)
));
}
if (isset($options['httpClient'])) {
$this->httpClient = $options['httpClient'];
} else {
$this->httpClient = new Client(['handler' => $stack]);
}
}
/**
* Uploads a media item to Google. This won't create a media item in the user's Google Photos
* library.
*
* @param string $rawFile The raw bytes of the file, obtained by something like
* file_get_contents().
* @param string $fileName The name of the file to be uploaded. This is no longer recommended
* Filenames should be set in the batchCreate call instead.
* @param string $mimeType The MIME type of the file to be uploaded. For example, text/html.
* @return string An upload token
* @throws GuzzleException
*/
public function upload($rawFile, $fileName = '', $mimeType = '')
{
$headers = [
'Content-type' => 'application/octet-stream',
'Authorization' => $this->getCredentialsWrapper()->getBearerString(),
'X-Goog-Upload-Protocol' => 'raw',
];
if ($fileName) {
$headers['X-Goog-Upload-File-Name'] = $fileName;
}
if ($mimeType) {
$headers['X-Goog-Upload-Content-Type'] = $mimeType;
}
$response = $this->httpClient->request(
'POST',
self::UPLOAD_URL,
[
'headers' => $headers,
'body' => $rawFile,
'timeout' => $this->uploadRetrySettings->retriesEnabled
? $this->uploadRetrySettings->singleTimeoutMillis
: 0
]
);
return (string)$response->getBody();
}
/**
* Updates the album with a new title.
*
* The album must have been created by the developer via the API and
* must be owned by the user.
*
* @param string $albumId Identifier of the [Album][google.photos.types.Album] to update
* @param string $newTitle Required. The new title of the album.
* @param array $optionalArgs
*
* @return Album
*
* @throws ApiException if the remote call fails
* @see updateAlbum
*/
public function updateAlbumTitle($albumId, $newTitle, array $optionalArgs = [])
{
$newAlbum = new Album();
$newAlbum->setId($albumId);
$newAlbum->setTitle($newTitle);
$updateMask = new FieldMask(['paths' => ['title']]);
return parent::updateAlbum($newAlbum, $updateMask, $optionalArgs);
}
/**
* Updates the album with a new cover photo.
*
* The album must have been created by the developer via the API and
* must be owned by the user.
*
* The $newCoverMediaItemId must be the identifier of a media item contained within the
* album.
*
* @param string $albumId Identifier of the [Album][google.photos.types.Album] to update
* @param string $newCoverMediaItemId Required. The identifier of the new media item cover photo.
* @param array $optionalArgs
*
* @return Album
*
* @throws ApiException if the remote call fails
* @see updateAlbum
*
*/
public function updateAlbumCoverPhoto($albumId, $newCoverMediaItemId, array $optionalArgs = [])
{
$newAlbum = new Album();
$newAlbum->setId($albumId);
$newAlbum->setCoverPhotoMediaItemId($newCoverMediaItemId);
$updateMask = new FieldMask(['paths' => ['cover_photo_media_item_id']]);
return parent::updateAlbum($newAlbum, $updateMask, $optionalArgs);
}
/**
* Updates the media item with a new description.
*
* The media item must have been created by the developer via the API and
* must be owned by the user.
*
* @param string $mediaItemId Required. Identifier of the [MediaItem][google.photos.types.MediaItem] to update.
* @param string $newDescription The new description for the media item.
* @param array $optionalArgs
*
* @return MediaItem
*
* @throws ApiException if the remote call fails
*/
public function updateMediaItemDescription($mediaItemId, $newDescription, array $optionalArgs = [])
{
$newItem = new MediaItem();
$newItem->setId($mediaItemId);
$newItem->setDescription($newDescription);
$updateMask = new FieldMask(['paths' => ['description']]);
return parent::updateMediaItem($newItem, $updateMask, $optionalArgs);
}
/**
* List of supported content categories for media item searches with a content filter.
*
* @return string[] Content categories supported by the API.
*/
public static function contentCategories()
{
return ['NONE', 'LANDSCAPES', 'RECEIPTS', 'CITYSCAPES', 'LANDMARKS', 'SELFIES', 'PEOPLE',
'PETS', 'WEDDINGS', 'BIRTHDAYS', 'DOCUMENTS', 'TRAVEL', 'ANIMALS', 'FOOD', 'SPORT',
'NIGHT', 'PERFORMANCES', 'WHITEBOARDS', 'SCREENSHOTS', 'UTILITY'];
}
/**
* List of supported media types for media item searches with a media type filter.
*
* @return string[] Media types supported by the API.
*/
public static function mediaTypes()
{
return ['ALL_MEDIA', 'PHOTO', 'VIDEO'];
}
/**
* List of supported feature attributes for media item searches with a feature filter.
*
* @return string[] Feature attributes supported by the API.
*/
public static function features()
{
return ['NONE', 'FAVORITES'];
}
}