@@ -25,11 +25,13 @@ @interface KIONetwork ()
25
25
@param response The response from the server.
26
26
@param responseData The data returned from the server.
27
27
@param query The query that was passed to the Keen API.
28
+ @param the error returned with the response.
28
29
*/
29
30
- (void )handleQueryAPIResponse : (NSURLResponse *)response
30
31
andData : (NSData *)responseData
31
32
andQuery : (KIOQuery *)query
32
- andProjectID : (NSString *)projectID ;
33
+ andProjectID : (NSString *)projectID
34
+ andError : (NSError *)error ;
33
35
34
36
- (NSString *)getProjectURL : (NSString *)projectID ;
35
37
@@ -39,7 +41,7 @@ - (NSString *)getProjectURL:(NSString *)projectID;
39
41
40
42
// Internal read/write versions of proxy host and port
41
43
@property (nonatomic , readwrite ) NSString *proxyHost;
42
- @property (nonatomic , readwrite ) NSString *proxyPort;
44
+ @property (nonatomic , readwrite ) NSNumber *proxyPort;
43
45
44
46
@end
45
47
@@ -77,7 +79,7 @@ - (instancetype)initWithURLSessionFactory:(id<KIONSURLSessionFactory>)urlSession
77
79
return self;
78
80
}
79
81
80
- - (BOOL )setProxy : (NSString *)host port : (NSString *)port {
82
+ - (BOOL )setProxy : (NSString *)host port : (NSNumber *)port {
81
83
BOOL success = NO ;
82
84
83
85
if ((nil == host) != (nil == port)) {
@@ -136,26 +138,20 @@ - (void)executeRequest:(NSURLRequest *)request completionHandler:(AnalysisComple
136
138
NSURLSession *session;
137
139
// Use proxy if one has been configured
138
140
if (self.proxyHost && self.proxyPort ) {
139
- NSDictionary *proxyDict = @{
140
- (__bridge NSString *)kCFNetworkProxiesHTTPEnable : @(YES ),
141
- (__bridge NSString *)kCFNetworkProxiesHTTPProxy : self.proxyHost ,
142
- (__bridge NSString *)kCFNetworkProxiesHTTPPort : self.proxyPort ,
143
-
144
- #if !TARGET_OS_IPHONE
145
- (__bridge NSString *)kCFNetworkProxiesHTTPSEnable : @(YES ),
146
- (__bridge NSString *)kCFNetworkProxiesHTTPSProxy : self.proxyHost ,
147
- (__bridge NSString *)kCFNetworkProxiesHTTPSPort : self.proxyPort ,
148
- #endif
149
- };
150
-
141
+ // Create an NSURLSessionConfiguration that uses the proxy
151
142
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration ];
152
- configuration.connectionProxyDictionary = proxyDict;
153
-
143
+ configuration.connectionProxyDictionary = @{
144
+ @" HTTPEnable" : @(YES ),
145
+ (NSString *)kCFStreamPropertyHTTPProxyHost : self.proxyHost ,
146
+ (NSString *)kCFStreamPropertyHTTPProxyPort : self.proxyPort ,
147
+ @" HTTPSEnable" : @(YES ),
148
+ (NSString *)kCFStreamPropertyHTTPSProxyHost : self.proxyHost ,
149
+ (NSString *)kCFStreamPropertyHTTPSProxyPort : self.proxyPort ,
150
+ };
154
151
session = [self .urlSessionFactory sessionWithConfiguration: configuration];
155
152
} else {
156
153
session = [self .urlSessionFactory session ];
157
154
}
158
-
159
155
[[session dataTaskWithRequest: request completionHandler: completionHandler] resume ];
160
156
}
161
157
@@ -215,18 +211,25 @@ - (void)runQuery:(KIOQuery *)keenQuery
215
211
NSString *projectID = config.projectID ;
216
212
[self executeRequest: request
217
213
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
218
- [self handleQueryAPIResponse: response andData: data andQuery: keenQuery andProjectID: projectID];
219
- completionHandler (data, response, error);
214
+ [self handleQueryAPIResponse: response
215
+ andData: data
216
+ andQuery: keenQuery
217
+ andProjectID: projectID
218
+ andError: error];
219
+ if (nil != completionHandler) {
220
+ completionHandler (data, response, error);
221
+ }
220
222
}];
221
223
}
222
224
223
225
- (void )handleQueryAPIResponse : (NSURLResponse *)response
224
226
andData : (NSData *)responseData
225
227
andQuery : (KIOQuery *)query
226
- andProjectID : (NSString *)projectID {
228
+ andProjectID : (NSString *)projectID
229
+ andError : (NSError *)error {
227
230
// Check if call to the Query API failed
228
- if (! responseData) {
229
- KCLogError (@" responseData was nil for some reason. That's not great. " );
231
+ if (nil == responseData || nil != error ) {
232
+ KCLogError (@" Error reading response. error: %@ \n response data: %@ " , error, responseData );
230
233
KCLogError (@" response status code: %ld " , (long )[((NSHTTPURLResponse *)response)statusCode]);
231
234
return ;
232
235
}
0 commit comments