1 |
efrain |
1 |
// Copyright 2019 Google
|
|
|
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 |
|
|
|
15 |
#import <Foundation/Foundation.h>
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Type to indicate response status
|
|
|
19 |
*/
|
|
|
20 |
typedef NS_ENUM(NSInteger, FIRCLSNetworkClientResponseType) {
|
|
|
21 |
FIRCLSNetworkClientResponseSuccess,
|
|
|
22 |
FIRCLSNetworkClientResponseInvalid,
|
|
|
23 |
FIRCLSNetworkClientResponseFailure,
|
|
|
24 |
FIRCLSNetworkClientResponseRetry,
|
|
|
25 |
FIRCLSNetworkClientResponseBackOff
|
|
|
26 |
};
|
|
|
27 |
|
|
|
28 |
typedef NS_ENUM(NSInteger, FIRCLSNetworkErrorType) {
|
|
|
29 |
FIRCLSNetworkErrorUnknown = -1,
|
|
|
30 |
FIRCLSNetworkErrorFailedToStartOperation = -3,
|
|
|
31 |
FIRCLSNetworkErrorResponseInvalid = -4,
|
|
|
32 |
FIRCLSNetworkErrorRequestFailed = -5,
|
|
|
33 |
FIRCLSNetworkErrorMaximumAttemptsReached = -6,
|
|
|
34 |
};
|
|
|
35 |
|
|
|
36 |
extern NSInteger const FIRCLSNetworkErrorUnknownURLCancelReason;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* This block is an input parameter to handleCompletedResponse: and handleCompletedTask: methods of
|
|
|
40 |
* this class.
|
|
|
41 |
* @param retryMightSucceed is YES if the request should be retried.
|
|
|
42 |
* @param error is the error received back in response.
|
|
|
43 |
*/
|
|
|
44 |
typedef void (^FIRCLSNetworkResponseCompletionHandlerBlock)(BOOL retryMightSucceed, NSError *error);
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Error domain for Crashlytics network errors
|
|
|
48 |
*/
|
|
|
49 |
extern NSString *const FIRCLSNetworkErrorDomain;
|
|
|
50 |
/**
|
|
|
51 |
* This class handles network responses.
|
|
|
52 |
*/
|
|
|
53 |
@interface FIRCLSNetworkResponseHandler : NSObject
|
|
|
54 |
/**
|
|
|
55 |
* Returns the header in the given NSURLResponse with name as key
|
|
|
56 |
*/
|
|
|
57 |
+ (NSString *)headerForResponse:(NSURLResponse *)response withKey:(NSString *)key;
|
|
|
58 |
/**
|
|
|
59 |
* Returns Retry-After header value in response, and if absent returns a default retry value
|
|
|
60 |
*/
|
|
|
61 |
+ (NSTimeInterval)retryValueForResponse:(NSURLResponse *)response;
|
|
|
62 |
/**
|
|
|
63 |
* Checks if the content type for response matches the request
|
|
|
64 |
*/
|
|
|
65 |
+ (BOOL)contentTypeForResponse:(NSURLResponse *)response matchesRequest:(NSURLRequest *)request;
|
|
|
66 |
|
|
|
67 |
+ (NSInteger)cancelReasonFromURLError:(NSError *)error;
|
|
|
68 |
|
|
|
69 |
+ (BOOL)retryableURLError:(NSError *)error;
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Convenience method that calls back the input block with FIRCLSNetworkClientResponseType after
|
|
|
73 |
* checking the response code in response
|
|
|
74 |
*/
|
|
|
75 |
+ (void)clientResponseType:(NSURLResponse *)response
|
|
|
76 |
handler:(void (^)(FIRCLSNetworkClientResponseType type,
|
|
|
77 |
NSInteger statusCode))responseTypeAndStatusCodeHandlerBlock;
|
|
|
78 |
/**
|
|
|
79 |
* Handles a completed response for request and calls back input block. Populates error even if
|
|
|
80 |
* error was nil, but response code indicated an error.
|
|
|
81 |
*/
|
|
|
82 |
+ (void)handleCompletedResponse:(NSURLResponse *)response
|
|
|
83 |
forOriginalRequest:(NSURLRequest *)originalRequest
|
|
|
84 |
error:(NSError *)error
|
|
|
85 |
block:(FIRCLSNetworkResponseCompletionHandlerBlock)completionHandlerBlock;
|
|
|
86 |
|
|
|
87 |
@end
|