@@ -92,17 +92,21 @@ - (instancetype)initWithURLSession:(NSURLSession *)URLSession
92
92
#pragma mark - Public
93
93
94
94
- (FBLPromise<FIRInstallationsItem *> *)registerInstallation : (FIRInstallationsItem *)installation {
95
- NSURLRequest *request = [self registerRequestWithInstallation: installation];
96
- return [self sendURLRequest: request].then (
97
- ^id _Nullable (FIRInstallationsURLSessionResponse *response) {
95
+ return [self registerRequestWithInstallation: installation]
96
+ .then (^id _Nullable (NSURLRequest *_Nullable request) {
97
+ return [self sendURLRequest: request];
98
+ })
99
+ .then (^id _Nullable (FIRInstallationsURLSessionResponse *response) {
98
100
return [self registeredInstallationWithInstallation: installation serverResponse: response];
99
101
});
100
102
}
101
103
102
104
- (FBLPromise<FIRInstallationsItem *> *)refreshAuthTokenForInstallation :
103
105
(FIRInstallationsItem *)installation {
104
- NSURLRequest *request = [self authTokenRequestWithInstallation: installation];
105
- return [self sendURLRequest: request]
106
+ return [self authTokenRequestWithInstallation: installation]
107
+ .then (^id _Nullable (NSURLRequest *_Nullable request) {
108
+ return [self sendURLRequest: request];
109
+ })
106
110
.then (^FBLPromise<FIRInstallationsStoredAuthToken *> *(
107
111
FIRInstallationsURLSessionResponse *response) {
108
112
return [self authTokenWithServerResponse: response];
@@ -115,17 +119,20 @@ - (instancetype)initWithURLSession:(NSURLSession *)URLSession
115
119
}
116
120
117
121
- (FBLPromise<FIRInstallationsItem *> *)deleteInstallation : (FIRInstallationsItem *)installation {
118
- NSURLRequest *request = [self deleteInstallationRequestWithInstallation: installation];
119
- return [[self sendURLRequest: request]
120
- then: ^id _Nullable (FIRInstallationsURLSessionResponse *_Nullable value) {
122
+ return [self deleteInstallationRequestWithInstallation: installation]
123
+ .then (^id _Nullable (NSURLRequest *_Nullable request) {
124
+ return [self sendURLRequest: request];
125
+ })
126
+ .then (^id _Nullable (FIRInstallationsURLSessionResponse *_Nullable value) {
121
127
// Return the original installation on success.
122
128
return installation;
123
- }] ;
129
+ }) ;
124
130
}
125
131
126
132
#pragma mark - Register Installation
127
133
128
- - (NSURLRequest *)registerRequestWithInstallation : (FIRInstallationsItem *)installation {
134
+ - (FBLPromise<NSURLRequest *> *)registerRequestWithInstallation :
135
+ (FIRInstallationsItem *)installation {
129
136
NSString *URLString = [NSString stringWithFormat: @" %@ /v1/projects/%@ /installations/" ,
130
137
kFIRInstallationsAPIBaseURL , self .projectID];
131
138
NSURL *URL = [NSURL URLWithString: URLString];
@@ -176,7 +183,8 @@ - (NSURLRequest *)registerRequestWithInstallation:(FIRInstallationsItem *)instal
176
183
177
184
#pragma mark - Auth token
178
185
179
- - (NSURLRequest *)authTokenRequestWithInstallation : (FIRInstallationsItem *)installation {
186
+ - (FBLPromise<NSURLRequest *> *)authTokenRequestWithInstallation :
187
+ (FIRInstallationsItem *)installation {
180
188
NSString *URLString =
181
189
[NSString stringWithFormat: @" %@ /v1/projects/%@ /installations/%@ /authTokens:generate" ,
182
190
kFIRInstallationsAPIBaseURL , self .projectID,
@@ -216,7 +224,8 @@ - (NSURLRequest *)authTokenRequestWithInstallation:(FIRInstallationsItem *)insta
216
224
217
225
#pragma mark - Delete Installation
218
226
219
- - (NSURLRequest *)deleteInstallationRequestWithInstallation : (FIRInstallationsItem *)installation {
227
+ - (FBLPromise<NSURLRequest *> *)deleteInstallationRequestWithInstallation :
228
+ (FIRInstallationsItem *)installation {
220
229
NSString *URLString = [NSString stringWithFormat: @" %@ /v1/projects/%@ /installations/%@ /" ,
221
230
kFIRInstallationsAPIBaseURL , self .projectID,
222
231
installation.firebaseInstallationID];
@@ -229,45 +238,52 @@ - (NSURLRequest *)deleteInstallationRequestWithInstallation:(FIRInstallationsIte
229
238
}
230
239
231
240
#pragma mark - URL Request
232
- - (NSURLRequest *)requestWithURL : (NSURL *)requestURL
233
- HTTPMethod : (NSString *)HTTPMethod
234
- bodyDict : (NSDictionary *)bodyDict
235
- refreshToken : (nullable NSString *)refreshToken {
241
+ - (FBLPromise< NSURLRequest *> *)requestWithURL : (NSURL *)requestURL
242
+ HTTPMethod : (NSString *)HTTPMethod
243
+ bodyDict : (NSDictionary *)bodyDict
244
+ refreshToken : (nullable NSString *)refreshToken {
236
245
return [self requestWithURL: requestURL
237
246
HTTPMethod: HTTPMethod
238
247
bodyDict: bodyDict
239
248
refreshToken: refreshToken
240
249
additionalHeaders: nil ];
241
250
}
242
251
243
- - (NSURLRequest *)requestWithURL : (NSURL *)requestURL
244
- HTTPMethod : (NSString *)HTTPMethod
245
- bodyDict : (NSDictionary *)bodyDict
246
- refreshToken : (nullable NSString *)refreshToken
247
- additionalHeaders :
248
- (nullable NSDictionary <NSString *, NSString *> *)additionalHeaders {
249
- __block NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: requestURL];
250
- request.HTTPMethod = HTTPMethod;
251
- NSString *bundleIdentifier = [[NSBundle mainBundle ] bundleIdentifier ];
252
- [request addValue: self .APIKey forHTTPHeaderField: kFIRInstallationsAPIKey ];
253
- [request addValue: bundleIdentifier forHTTPHeaderField: kFIRInstallationsBundleId ];
254
- [self setJSONHTTPBody: bodyDict forRequest: request];
255
- if (refreshToken) {
256
- NSString *authHeader = [NSString stringWithFormat: @" FIS_v2 %@ " , refreshToken];
257
- [request setValue: authHeader forHTTPHeaderField: @" Authorization" ];
258
- }
259
- // User agent Header.
260
- [request setValue: [FIRApp firebaseUserAgent ] forHTTPHeaderField: kFIRInstallationsUserAgentKey ];
261
- // Heartbeat Header.
262
- [request setValue: @([FIRHeartbeatInfo heartbeatCodeForTag: kFIRInstallationsHeartbeatTag ])
263
- .stringValue
264
- forHTTPHeaderField: kFIRInstallationsHeartbeatKey ];
265
- [additionalHeaders enumerateKeysAndObjectsUsingBlock: ^(
266
- NSString *_Nonnull key, NSString *_Nonnull obj, BOOL *_Nonnull stop) {
267
- [request setValue: obj forHTTPHeaderField: key];
268
- }];
269
-
270
- return [request copy ];
252
+ - (FBLPromise<NSURLRequest *> *)requestWithURL : (NSURL *)requestURL
253
+ HTTPMethod : (NSString *)HTTPMethod
254
+ bodyDict : (NSDictionary *)bodyDict
255
+ refreshToken : (nullable NSString *)refreshToken
256
+ additionalHeaders : (nullable NSDictionary <NSString *, NSString *> *)
257
+ additionalHeaders {
258
+ return [FBLPromise
259
+ onQueue: dispatch_get_global_queue (QOS_CLASS_UTILITY, 0 )
260
+ do: ^id _Nullable {
261
+ __block NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: requestURL];
262
+ request.HTTPMethod = HTTPMethod;
263
+ NSString *bundleIdentifier = [[NSBundle mainBundle ] bundleIdentifier ];
264
+ [request addValue: self .APIKey forHTTPHeaderField: kFIRInstallationsAPIKey ];
265
+ [request addValue: bundleIdentifier forHTTPHeaderField: kFIRInstallationsBundleId ];
266
+ [self setJSONHTTPBody: bodyDict forRequest: request];
267
+ if (refreshToken) {
268
+ NSString *authHeader = [NSString stringWithFormat: @" FIS_v2 %@ " , refreshToken];
269
+ [request setValue: authHeader forHTTPHeaderField: @" Authorization" ];
270
+ }
271
+ // User agent Header.
272
+ [request setValue: [FIRApp firebaseUserAgent ]
273
+ forHTTPHeaderField: kFIRInstallationsUserAgentKey ];
274
+ // Heartbeat Header.
275
+ [request setValue: @([FIRHeartbeatInfo
276
+ heartbeatCodeForTag: kFIRInstallationsHeartbeatTag ])
277
+ .stringValue
278
+ forHTTPHeaderField: kFIRInstallationsHeartbeatKey ];
279
+ [additionalHeaders
280
+ enumerateKeysAndObjectsUsingBlock: ^(NSString *_Nonnull key, NSString *_Nonnull obj,
281
+ BOOL *_Nonnull stop) {
282
+ [request setValue: obj forHTTPHeaderField: key];
283
+ }];
284
+
285
+ return [request copy ];
286
+ }];
271
287
}
272
288
273
289
- (FBLPromise<FIRInstallationsURLSessionResponse *> *)URLRequestPromise : (NSURLRequest *)request {
0 commit comments