2 * Copyright 2017 Google
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #import <Foundation/Foundation.h>
19 #import "GULNetworkLoggerProtocol.h"
21 typedef void (^GULNetworkCompletionHandler)(NSHTTPURLResponse *response,
24 typedef void (^GULNetworkURLSessionCompletionHandler)(NSHTTPURLResponse *response,
28 typedef void (^GULNetworkSystemCompletionHandler)(void);
30 /// The protocol that uses NSURLSession for iOS >= 7.0 to handle requests and responses.
31 @interface GULNetworkURLSession
32 : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate>
34 /// Indicates whether the background network is enabled. Default value is NO.
35 @property(nonatomic, getter=isBackgroundNetworkEnabled) BOOL backgroundNetworkEnabled;
37 /// The logger delegate to log message, errors or warnings that occur during the network operations.
38 @property(nonatomic, weak) id<GULNetworkLoggerDelegate> loggerDelegate;
40 /// Calls the system provided completion handler after the background session is finished.
41 + (void)handleEventsForBackgroundURLSessionID:(NSString *)sessionID
42 completionHandler:(GULNetworkSystemCompletionHandler)completionHandler;
44 /// Initializes with logger delegate.
45 - (instancetype)initWithNetworkLoggerDelegate:(id<GULNetworkLoggerDelegate>)networkLoggerDelegate
46 NS_DESIGNATED_INITIALIZER;
48 - (instancetype)init NS_UNAVAILABLE;
50 /// Sends an asynchronous POST request and calls the provided completion handler when the request
51 /// completes or when errors occur, and returns an ID of the session/connection.
52 - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
53 completionHandler:(GULNetworkURLSessionCompletionHandler)handler;
55 /// Sends an asynchronous GET request and calls the provided completion handler when the request
56 /// completes or when errors occur, and returns an ID of the session.
57 - (NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
58 completionHandler:(GULNetworkURLSessionCompletionHandler)handler;