31
31
import com .google .common .base .Strings ;
32
32
import com .google .common .collect .ImmutableList ;
33
33
import com .google .common .collect .ImmutableMap ;
34
+ import com .google .firebase .ErrorCode ;
34
35
import com .google .firebase .FirebaseApp ;
35
36
import com .google .firebase .ImplFirebaseTrampolines ;
37
+ import com .google .firebase .IncomingHttpResponse ;
36
38
import com .google .firebase .internal .ApiClientUtils ;
37
39
import com .google .firebase .internal .CallableOperation ;
38
40
import java .nio .charset .StandardCharsets ;
@@ -56,7 +58,6 @@ class FirebaseProjectManagementServiceImpl implements AndroidAppService, IosAppS
56
58
private final FirebaseApp app ;
57
59
private final Sleeper sleeper ;
58
60
private final Scheduler scheduler ;
59
- private final HttpRequestFactory requestFactory ;
60
61
private final HttpHelper httpHelper ;
61
62
62
63
private final CreateAndroidAppFromAppIdFunction createAndroidAppFromAppIdFunction =
@@ -78,15 +79,9 @@ class FirebaseProjectManagementServiceImpl implements AndroidAppService, IosAppS
78
79
this .app = checkNotNull (app );
79
80
this .sleeper = checkNotNull (sleeper );
80
81
this .scheduler = checkNotNull (scheduler );
81
- this .requestFactory = checkNotNull (requestFactory );
82
82
this .httpHelper = new HttpHelper (app .getOptions ().getJsonFactory (), requestFactory );
83
83
}
84
84
85
- @ VisibleForTesting
86
- HttpRequestFactory getRequestFactory () {
87
- return requestFactory ;
88
- }
89
-
90
85
@ VisibleForTesting
91
86
void setInterceptor (HttpResponseInterceptor interceptor ) {
92
87
httpHelper .setInterceptor (interceptor );
@@ -318,14 +313,14 @@ protected String execute() throws FirebaseProjectManagementException {
318
313
payloadBuilder .put ("display_name" , displayName );
319
314
}
320
315
OperationResponse operationResponseInstance = new OperationResponse ();
321
- httpHelper .makePostRequest (
316
+ IncomingHttpResponse response = httpHelper .makePostRequest (
322
317
url , payloadBuilder .build (), operationResponseInstance , projectId , "Project ID" );
323
318
if (Strings .isNullOrEmpty (operationResponseInstance .name )) {
324
- throw HttpHelper . createFirebaseProjectManagementException (
319
+ String message = buildMessage (
325
320
namespace ,
326
321
"Bundle ID" ,
327
- "Unable to create App: server returned null operation name." ,
328
- /* cause= */ null );
322
+ "Unable to create App: server returned null operation name." );
323
+ throw new FirebaseProjectManagementException ( ErrorCode . INTERNAL , message , response );
329
324
}
330
325
return operationResponseInstance .name ;
331
326
}
@@ -341,27 +336,29 @@ private String pollOperation(String projectId, String operationName)
341
336
* Math .pow (POLL_EXPONENTIAL_BACKOFF_FACTOR , currentAttempt ));
342
337
sleepOrThrow (projectId , delayMillis );
343
338
OperationResponse operationResponseInstance = new OperationResponse ();
344
- httpHelper .makeGetRequest (url , operationResponseInstance , projectId , "Project ID" );
339
+ IncomingHttpResponse response = httpHelper .makeGetRequest (
340
+ url , operationResponseInstance , projectId , "Project ID" );
345
341
if (!operationResponseInstance .done ) {
346
342
continue ;
347
343
}
348
344
// The Long Running Operation API guarantees that when done == true, exactly one of 'response'
349
345
// or 'error' is set.
350
346
if (operationResponseInstance .response == null
351
347
|| Strings .isNullOrEmpty (operationResponseInstance .response .appId )) {
352
- throw HttpHelper . createFirebaseProjectManagementException (
348
+ String message = buildMessage (
353
349
projectId ,
354
350
"Project ID" ,
355
- "Unable to create App: internal server error." ,
356
- /* cause= */ null );
351
+ "Unable to create App: internal server error." );
352
+ throw new FirebaseProjectManagementException ( ErrorCode . INTERNAL , message , response );
357
353
}
358
354
return operationResponseInstance .response .appId ;
359
355
}
360
- throw HttpHelper .createFirebaseProjectManagementException (
356
+
357
+ String message = buildMessage (
361
358
projectId ,
362
359
"Project ID" ,
363
- "Unable to create App: deadline exceeded." ,
364
- /* cause= */ null );
360
+ "Unable to create App: deadline exceeded." );
361
+ throw new FirebaseProjectManagementException ( ErrorCode . DEADLINE_EXCEEDED , message , null );
365
362
}
366
363
367
364
/**
@@ -420,19 +417,22 @@ private WaitOperationRunnable(
420
417
public void run () {
421
418
String url = String .format ("%s/v1/%s" , FIREBASE_PROJECT_MANAGEMENT_URL , operationName );
422
419
OperationResponse operationResponseInstance = new OperationResponse ();
420
+ IncomingHttpResponse httpResponse ;
423
421
try {
424
- httpHelper .makeGetRequest (url , operationResponseInstance , projectId , "Project ID" );
422
+ httpResponse = httpHelper .makeGetRequest (
423
+ url , operationResponseInstance , projectId , "Project ID" );
425
424
} catch (FirebaseProjectManagementException e ) {
426
425
settableFuture .setException (e );
427
426
return ;
428
427
}
429
428
if (!operationResponseInstance .done ) {
430
429
if (numberOfPreviousPolls + 1 >= MAXIMUM_POLLING_ATTEMPTS ) {
431
- settableFuture .setException (HttpHelper .createFirebaseProjectManagementException (
432
- projectId ,
430
+ String message = buildMessage (projectId ,
433
431
"Project ID" ,
434
- "Unable to create App: deadline exceeded." ,
435
- /* cause= */ null ));
432
+ "Unable to create App: deadline exceeded." );
433
+ FirebaseProjectManagementException exception = new FirebaseProjectManagementException (
434
+ ErrorCode .DEADLINE_EXCEEDED , message , httpResponse );
435
+ settableFuture .setException (exception );
436
436
} else {
437
437
long delayMillis = (long ) (
438
438
POLL_BASE_WAIT_TIME_MILLIS
@@ -451,11 +451,12 @@ public void run() {
451
451
// or 'error' is set.
452
452
if (operationResponseInstance .response == null
453
453
|| Strings .isNullOrEmpty (operationResponseInstance .response .appId )) {
454
- settableFuture .setException (HttpHelper .createFirebaseProjectManagementException (
455
- projectId ,
454
+ String message = buildMessage (projectId ,
456
455
"Project ID" ,
457
- "Unable to create App: internal server error." ,
458
- /* cause= */ null ));
456
+ "Unable to create App: internal server error." );
457
+ FirebaseProjectManagementException exception = new FirebaseProjectManagementException (
458
+ ErrorCode .INTERNAL , message , httpResponse );
459
+ settableFuture .setException (exception );
459
460
} else {
460
461
settableFuture .set (operationResponseInstance .response .appId );
461
462
}
@@ -765,14 +766,17 @@ private void sleepOrThrow(String projectId, long delayMillis)
765
766
try {
766
767
sleeper .sleep (delayMillis );
767
768
} catch (InterruptedException e ) {
768
- throw HttpHelper .createFirebaseProjectManagementException (
769
- projectId ,
769
+ String message = buildMessage (projectId ,
770
770
"Project ID" ,
771
- "Unable to create App: exponential backoff interrupted." ,
772
- /* cause= */ null );
771
+ "Unable to create App: exponential backoff interrupted." );
772
+ throw new FirebaseProjectManagementException ( ErrorCode . ABORTED , message , null );
773
773
}
774
774
}
775
775
776
+ private String buildMessage (String resourceId , String resourceIdName , String description ) {
777
+ return String .format ("%s \" %s\" : %s" , resourceIdName , resourceId , description );
778
+ }
779
+
776
780
/* Helper types. */
777
781
778
782
private interface CreateAppFromAppIdFunction <T > extends ApiFunction <String , T > {}
0 commit comments