FIRFADApiService.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "FirebaseAppDistribution/Sources/FIRFADApiService.h"
  15. #import <Foundation/Foundation.h>
  16. #import "FirebaseAppDistribution/Sources/FIRFADLogger.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 ([self handleError:&error
  37. description:@"Failed to generate Firebase Installation Auth Token."
  38. code:FIRFADApiTokenGenerationFailure]) {
  39. FIRFADErrorLog(@"Error getting fresh auth tokens. Error: %@", [error localizedDescription]);
  40. completion(nil, nil, error);
  41. return;
  42. }
  43. [installations installationIDWithCompletion:^(NSString *__nullable identifier,
  44. NSError *__nullable error) {
  45. if ([self handleError:&error
  46. description:@"Failed to fetch Firebase Installation ID."
  47. code:FIRFADApiInstallationIdentifierError]) {
  48. FIRFADErrorLog(@"Error getting installation id. Error: %@", [error localizedDescription]);
  49. completion(nil, nil, error);
  50. return;
  51. }
  52. completion(identifier, authTokenResult, nil);
  53. }];
  54. }];
  55. }
  56. + (NSMutableURLRequest *)createHTTPRequest:(NSString *)method
  57. withUrl:(NSString *)urlString
  58. withAuthToken:(FIRInstallationsAuthTokenResult *)authTokenResult {
  59. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  60. FIRFADInfoLog(@"Requesting releases for app id - %@", [[FIRApp defaultApp] options].googleAppID);
  61. [request setURL:[NSURL URLWithString:urlString]];
  62. [request setHTTPMethod:method];
  63. [request setValue:authTokenResult.authToken forHTTPHeaderField:kInstallationAuthHeader];
  64. [request setValue:[[FIRApp defaultApp] options].APIKey forHTTPHeaderField:kApiHeaderKey];
  65. return request;
  66. }
  67. + (NSArray *)handleReleaseResponse:(NSData *)data
  68. response:(NSURLResponse *)response
  69. error:(NSError **_Nullable)error {
  70. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  71. FIRFADInfoLog(@"HTTPResonse status code %ld response %@", (long)[httpResponse statusCode],
  72. httpResponse);
  73. if ([self handleHttpResponseError:httpResponse error:error]) {
  74. FIRFADErrorLog(@"App Tester API service error - %@", [*error localizedDescription]);
  75. return nil;
  76. }
  77. return [self parseApiResponseWithData:data error:error];
  78. }
  79. + (void)fetchReleasesWithCompletion:(FIRFADFetchReleasesCompletion)completion {
  80. void (^executeFetch)(NSString *_Nullable, FIRInstallationsAuthTokenResult *, NSError *_Nullable) =
  81. ^(NSString *_Nullable identifier, FIRInstallationsAuthTokenResult *authTokenResult,
  82. NSError *_Nullable error) {
  83. NSString *urlString =
  84. [NSString stringWithFormat:kReleasesEndpointURLTemplate,
  85. [[FIRApp defaultApp] options].googleAppID, identifier];
  86. NSMutableURLRequest *request = [self createHTTPRequest:@"GET"
  87. withUrl:urlString
  88. withAuthToken:authTokenResult];
  89. FIRFADInfoLog(@"Url : %@, Auth token: %@ API KEY: %@", urlString, authTokenResult.authToken,
  90. [[FIRApp defaultApp] options].APIKey);
  91. NSURLSessionDataTask *listReleasesDataTask = [[NSURLSession sharedSession]
  92. dataTaskWithRequest:request
  93. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  94. NSArray *releases = [self handleReleaseResponse:data
  95. response:response
  96. error:&error];
  97. dispatch_async(dispatch_get_main_queue(), ^{
  98. completion(releases, error);
  99. });
  100. }];
  101. [listReleasesDataTask resume];
  102. };
  103. [self generateAuthTokenWithCompletion:executeFetch];
  104. }
  105. + (BOOL)handleHttpResponseError:(NSHTTPURLResponse *)httpResponse error:(NSError **_Nullable)error {
  106. if (*error || !httpResponse) {
  107. return [self handleError:error
  108. description:@"Unknown http error occurred"
  109. code:FIRApiErrorUnknownFailure];
  110. ;
  111. }
  112. if ([httpResponse statusCode] != 200) {
  113. *error = [self createErrorFromStatusCode:[httpResponse statusCode]];
  114. return YES;
  115. }
  116. return NO;
  117. }
  118. + (NSError *)createErrorFromStatusCode:(NSInteger)statusCode {
  119. if (statusCode == 401) {
  120. return [self createErrorWithDescription:@"Tester not authenticated."
  121. code:FIRFADApiErrorUnauthenticated];
  122. }
  123. if (statusCode == 403 || statusCode == 400) {
  124. return [self createErrorWithDescription:@"Tester not authorized."
  125. code:FIRFADApiErrorUnauthorized];
  126. }
  127. if (statusCode == 404) {
  128. return [self createErrorWithDescription:@"Tester or releases not found"
  129. code:FIRFADApiErrorUnauthorized];
  130. }
  131. if (statusCode == 408 || statusCode == 504) {
  132. return [self createErrorWithDescription:@"Request timeout." code:FIRFADApiErrorTimeout];
  133. }
  134. FIRFADErrorLog(@"Encountered unmapped status code: %ld", (long)statusCode);
  135. NSString *description = [NSString stringWithFormat:@"Unknown status code: %ld", (long)statusCode];
  136. return [self createErrorWithDescription:description code:FIRApiErrorUnknownFailure];
  137. }
  138. + (BOOL)handleError:(NSError **_Nullable)error
  139. description:(NSString *)description
  140. code:(FIRFADApiError)code {
  141. if (*error) {
  142. *error = [self createErrorWithDescription:description code:code];
  143. return YES;
  144. }
  145. return NO;
  146. }
  147. + (NSError *)createErrorWithDescription:description code:(FIRFADApiError)code {
  148. NSDictionary *userInfo = @{NSLocalizedDescriptionKey : description};
  149. return [NSError errorWithDomain:kFIRFADApiErrorDomain code:code userInfo:userInfo];
  150. }
  151. + (NSArray *_Nullable)parseApiResponseWithData:(NSData *)data error:(NSError **_Nullable)error {
  152. NSDictionary *serializedResponse = [NSJSONSerialization JSONObjectWithData:data
  153. options:0
  154. error:error];
  155. if (*error) {
  156. FIRFADErrorLog(@"Tester API - Error deserializing json response");
  157. NSString *description = (*error).userInfo[NSLocalizedDescriptionKey]
  158. ? (*error).userInfo[NSLocalizedDescriptionKey]
  159. : @"Failed to parse response";
  160. [self handleError:error description:description code:FIRApiErrorParseFailure];
  161. return nil;
  162. }
  163. NSArray *releases = [serializedResponse objectForKey:kResponseReleasesKey];
  164. return releases;
  165. }
  166. @end