Skip to content

Commit bb90ea6

Browse files
authored
Merge pull request #2159 from skaut/fix-empty-batch
Fixed an error with empty batch request when trying to select root folder from a list of drives
2 parents 384aa7e + cdd374f commit bb90ea6

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

src/php/admin/settings-pages/basic/root-selection/class-list-ajax-endpoint.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ public static function ajax_handler_body() {
7070
}
7171

7272
$path_ids = GET_Helpers::get_array_variable( 'path' );
73-
API_Client::preamble();
74-
75-
$promise = Utils::all(
73+
$promise = Utils::all(
7674
array(
7775
'path' => self::path_ids_to_names( $path_ids ),
7876
'path_ids' => $path_ids,

src/php/class-api-client.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static function get_drive_client() {
144144
*
145145
* @return void
146146
*/
147-
public static function preamble() {
147+
public static function initialize_batch() {
148148
if ( ! is_null( self::$current_batch ) ) {
149149
return;
150150
}
@@ -163,7 +163,7 @@ public static function preamble() {
163163
*
164164
* @return PromiseInterface A promise that will be resolved in `$callback`.
165165
*
166-
* @throws Internal_Exception The method was called without the preamble.
166+
* @throws Internal_Exception The method was called without an initialized batch.
167167
*/
168168
public static function async_request( $request, $transform, $rejection_handler = null ) {
169169
if ( null === self::$current_batch ) {
@@ -205,7 +205,7 @@ public static function async_paginated_request(
205205
/**
206206
* Gets one page.
207207
*
208-
* @throws Internal_Exception The method was called without the preamble.
208+
* @throws Internal_Exception The method was called without an initialized batch.
209209
*
210210
* phpcs:disable SlevomatCodingStandard.PHP.DisallowReference.DisallowedInheritingVariableByReference
211211
*/
@@ -261,21 +261,36 @@ public static function async_paginated_request(
261261
}
262262

263263
/**
264-
* Executes all requests and resolves all promises.
264+
* Executes all queued requests and resolves all promises repeatedly until there is nothing to be done.
265265
*
266266
* @param array<int|string, PromiseInterface> $promises The promises to resolve and throw exceptions if they reject.
267267
*
268268
* @return array<int|string, mixed> A list of results from the promises. Is in the same format as the parameter `$promises`, i.e. if an associative array of promises is passed, an associative array of results will be returned.
269269
*/
270270
public static function execute( $promises = array() ) {
271-
if ( is_null( self::$current_batch ) ) {
272-
Utils::queue()->run();
271+
self::execute_current_batch();
272+
Utils::queue()->run();
273+
274+
if ( count( self::$pending_requests ) > 0 ) {
275+
self::execute();
276+
}
277+
278+
return Utils::all( $promises )->wait();
279+
}
273280

274-
return Utils::all( $promises )->wait();
281+
/**
282+
* Executes the current batch request and calls its callbacks
283+
*
284+
* @return void
285+
*/
286+
private static function execute_current_batch() {
287+
$batch = self::$current_batch;
288+
289+
if ( is_null( $batch ) ) {
290+
return;
275291
}
276292

277-
$batch = self::$current_batch;
278-
self::$current_batch = self::get_drive_client()->createBatch();
293+
self::$current_batch = null;
279294
/**
280295
* The closure executes the batch and throws the exception if it is a rate limit exceeded exception (this is needed by the task runner).
281296
*
@@ -287,14 +302,14 @@ public static function execute( $promises = array() ) {
287302
),
288303
'Batch Drive call',
289304
static function () use ( $batch ) {
290-
// @phan-suppress-next-line PhanPossiblyNonClassMethodCall
291305
$ret = $batch->execute();
292306

293307
foreach ( $ret as $response ) {
294308
$exception = self::wrap_response_exception( $response );
295309

296310
if (
297-
$response instanceof Google_Service_Exception && $exception instanceof API_Rate_Limit_Exception
311+
$response instanceof Google_Service_Exception &&
312+
$exception instanceof API_Rate_Limit_Exception
298313
) {
299314
throw $response;
300315
}
@@ -304,22 +319,12 @@ static function () use ( $batch ) {
304319
}
305320
);
306321
$responses = $task->run();
322+
self::get_drive_client()->getClient()->setUseBatch( false );
307323

308324
foreach ( $responses as $key => $response ) {
309325
call_user_func( self::$pending_requests[ $key ], $response );
310326
unset( self::$pending_requests[ $key ] );
311327
}
312-
313-
Utils::queue()->run();
314-
315-
if ( count( self::$pending_requests ) > 0 ) {
316-
self::execute();
317-
}
318-
319-
self::$current_batch = null;
320-
self::get_drive_client()->getClient()->setUseBatch( false );
321-
322-
return Utils::all( $promises )->wait();
323328
}
324329

325330
/**

src/php/class-api-facade.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class API_Facade {
3636
* @throws API_Exception|API_Rate_Limit_Exception A problem with the API.
3737
*/
3838
public static function get_directory_id( $parent_id, $name ) {
39-
API_Client::preamble();
39+
API_Client::initialize_batch();
4040
$params = array(
4141
'fields' => 'files(id, name, mimeType, shortcutDetails(targetId))',
4242
'includeItemsFromAllDrives' => true,
@@ -84,7 +84,7 @@ static function ( $response ) use ( $name ) {
8484
* @SuppressWarnings(PHPMD.ShortVariable)
8585
*/
8686
public static function get_drive_name( $id ) {
87-
API_Client::preamble();
87+
API_Client::initialize_batch();
8888

8989
return API_Client::async_request(
9090
// @phan-suppress-next-line PhanTypeMismatchArgument
@@ -119,7 +119,7 @@ static function ( $exception ) {
119119
* @SuppressWarnings(PHPMD.ShortVariable)
120120
*/
121121
public static function get_file_name( $id ) {
122-
API_Client::preamble();
122+
API_Client::initialize_batch();
123123

124124
/**
125125
* `$transform` transforms the raw Google API response into the structured response this function returns.
@@ -163,7 +163,7 @@ static function ( $exception ) {
163163
* @SuppressWarnings(PHPMD.ShortVariable)
164164
*/
165165
public static function check_directory_in_directory( $id, $parent_id ) {
166-
API_Client::preamble();
166+
API_Client::initialize_batch();
167167

168168
return API_Client::async_request(
169169
// @phan-suppress-next-line PhanTypeMismatchArgument
@@ -218,7 +218,7 @@ static function ( $exception ) {
218218
* @throws API_Exception|API_Rate_Limit_Exception A problem with the API.
219219
*/
220220
public static function list_drives( $pagination_helper ) {
221-
API_Client::preamble();
221+
API_Client::initialize_batch();
222222

223223
return API_Client::async_paginated_request(
224224
static function ( $page_token ) {
@@ -316,7 +316,7 @@ public static function list_videos( $parent_id, $fields, $pagination_helper, $or
316316
* @throws Unsupported_Value_Exception A field that is not supported was passed in `$fields`.
317317
*/
318318
private static function list_files( $parent_id, $fields, $order_by, $pagination_helper, $mime_type_prefix ) {
319-
API_Client::preamble();
319+
API_Client::initialize_batch();
320320

321321
if ( ! $fields->check(
322322
array(

0 commit comments

Comments
 (0)