FIRFADApiService.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. #import "FIRFADApiService+Private.h"
  16. #import "FIRFADLogger+Private.h"
  17. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  18. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  19. NSString *const kFIRFADApiErrorDomain = @"com.firebase.appdistribution.api";
  20. NSString *const kFIRFADApiErrorDetailsKey = @"details";
  21. NSString *const kHTTPGet = @"GET";
  22. // The App Distribution Tester API endpoint used to retrieve releases
  23. NSString *const kReleasesEndpointURLTemplate =
  24. @"https://firebaseapptesters.googleapis.com/v1alpha/devices/"
  25. @"-/testerApps/%@/installations/%@/releases";
  26. NSString *const kInstallationAuthHeader = @"X-Goog-Firebase-Installations-Auth";
  27. NSString *const kApiHeaderKey = @"X-Goog-Api-Key";
  28. NSString *const kResponseReleasesKey = @"releases";
  29. @implementation FIRFADApiService
  30. + (void)generateAuthTokenWithCompletion:(FIRFADGenerateAuthTokenCompletion)completion {
  31. FIRInstallations *installations = [FIRInstallations installations];
  32. // Get a FIS Authentication Token.
  33. [installations authTokenWithCompletion:^(
  34. FIRInstallationsAuthTokenResult *_Nullable authTokenResult,
  35. NSError *_Nullable error) {
  36. if (error) {
  37. FIRFADErrorLog(@"Error getting fresh auth tokens. Error: %@", [error localizedDescription]);
  38. [self handleError:&error
  39. description:@"Failed to generate Firebase Installation Auth Token."
  40. code:FIRFADApiTokenGenerationFailure];
  41. completion(nil, nil, error);
  42. return;
  43. }
  44. [installations installationIDWithCompletion:^(NSString *__nullable identifier,
  45. NSError *__nullable error) {
  46. if (error) {
  47. FIRFADErrorLog(@"Error getting installation id. Error: %@", [error localizedDescription]);
  48. [self handleError:&error
  49. description:@"Failed to fetch Firebase Installation ID."
  50. code:FIRFADApiInstallationIdentifierError];
  51. completion(nil, nil, error);
  52. return;
  53. }
  54. completion(identifier, authTokenResult, nil);
  55. }];
  56. }];
  57. }
  58. + (NSMutableURLRequest *)createHTTPRequest:(NSString *)method
  59. withUrl:(NSString *)urlString
  60. withAuthToken:(FIRInstallationsAuthTokenResult *)authTokenResult {
  61. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  62. FIRFADInfoLog(@"Requesting releases for app id - %@", [[FIRApp defaultApp] options].googleAppID);
  63. [request setURL:[NSURL URLWithString:urlString]];
  64. [request setHTTPMethod:method];
  65. [request setValue:authTokenResult.authToken forHTTPHeaderField:kInstallationAuthHeader];
  66. [request setValue:[[FIRApp defaultApp] options].APIKey forHTTPHeaderField:kApiHeaderKey];
  67. return request;
  68. }
  69. + (NSArray *)handleReleaseResponse:(NSData *)data
  70. response:(NSURLResponse *)response
  71. error:(NSError **_Nullable)error {
  72. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  73. FIRFADInfoLog(@"HTTPResonse status code %ld response %@", (long)[httpResponse statusCode],
  74. httpResponse);
  75. if (*error || !httpResponse) {
  76. [self handleError:error
  77. description:@"Unknown http error occurred"
  78. code:FIRApiErrorUnknownFailure];
  79. FIRFADErrorLog(@"App Tester API service error - %@", [*error localizedDescription]);
  80. return nil;
  81. }
  82. if ([httpResponse statusCode] != 200) {
  83. [self handleErrorWithStatusCode:[httpResponse statusCode] error:error];
  84. return nil;
  85. }
  86. return [self parseApiResponseWithData:data error:error];
  87. }
  88. + (void)fetchReleasesWithCompletion:(FIRFADFetchReleasesCompletion)completion {
  89. void (^executeFetch)(NSString *_Nullable, FIRInstallationsAuthTokenResult *, NSError *_Nullable) =
  90. ^(NSString *_Nullable identifier, FIRInstallationsAuthTokenResult *authTokenResult,
  91. NSError *_Nullable error) {
  92. NSString *urlString =
  93. [NSString stringWithFormat:kReleasesEndpointURLTemplate,
  94. [[FIRApp defaultApp] options].googleAppID, identifier];
  95. NSMutableURLRequest *request = [self createHTTPRequest:@"GET"
  96. withUrl:urlString
  97. withAuthToken:authTokenResult];
  98. FIRFADInfoLog(@"Url : %@, Auth token: %@ API KEY: %@", urlString, authTokenResult.authToken,
  99. [[FIRApp defaultApp] options].APIKey);
  100. NSURLSessionDataTask *listReleasesDataTask = [[NSURLSession sharedSession]
  101. dataTaskWithRequest:request
  102. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  103. NSArray *releases = [self handleReleaseResponse:data
  104. response:response
  105. error:&error];
  106. dispatch_async(dispatch_get_main_queue(), ^{
  107. completion(releases, error);
  108. });
  109. }];
  110. [listReleasesDataTask resume];
  111. };
  112. [self generateAuthTokenWithCompletion:executeFetch];
  113. }
  114. + (void)handleErrorWithStatusCode:(NSInteger)statusCode error:(NSError **_Nullable)error {
  115. if (statusCode == 401) {
  116. [self handleError:error
  117. description:@"Tester not authenticated."
  118. code:FIRFADApiErrorUnauthenticated];
  119. return;
  120. }
  121. if (statusCode == 403 || statusCode == 400) {
  122. [self handleError:error description:@"Tester not authorized." code:FIRFADApiErrorUnauthorized];
  123. return;
  124. }
  125. if (statusCode == 404) {
  126. [self handleError:error
  127. description:@"Tester or releases not found"
  128. code:FIRFADApiErrorUnauthorized];
  129. return;
  130. }
  131. if (statusCode == 408 || statusCode == 504) {
  132. [self handleError:error description:@"Request timeout." code:FIRFADApiErrorTimeout];
  133. return;
  134. }
  135. FIRFADErrorLog(@"Encountered unmapped status code: %ld", (long)statusCode);
  136. NSString *description =
  137. (*error).userInfo[NSLocalizedDescriptionKey]
  138. ? (*error).userInfo[NSLocalizedDescriptionKey]
  139. : [NSString stringWithFormat:@"Unknown status code: %ld", (long)statusCode];
  140. [self handleError:error description:description code:FIRApiErrorUnknownFailure];
  141. }
  142. + (void)handleError:(NSError **_Nullable)error
  143. description:(NSString *)description
  144. code:(FIRFADApiError)code {
  145. if (error) {
  146. NSDictionary *userInfo = @{NSLocalizedDescriptionKey : description};
  147. *error = [NSError errorWithDomain:kFIRFADApiErrorDomain code:code userInfo:userInfo];
  148. }
  149. }
  150. + (NSArray *_Nullable)parseApiResponseWithData:(NSData *)data error:(NSError **_Nullable)error {
  151. NSDictionary *serializedResponse = [NSJSONSerialization JSONObjectWithData:data
  152. options:0
  153. error:error];
  154. if (*error) {
  155. FIRFADErrorLog(@"Tester API - Error deserializing json response");
  156. NSString *description = (*error).userInfo[NSLocalizedDescriptionKey]
  157. ? (*error).userInfo[NSLocalizedDescriptionKey]
  158. : @"Failed to parse response";
  159. [self handleError:error description:description code:FIRApiErrorParseFailure];
  160. return nil;
  161. }
  162. NSArray *releases = [serializedResponse objectForKey:kResponseReleasesKey];
  163. return releases;
  164. }
  165. @end