diff --git a/Autocomplete Snippets/DZURLSession/thenOnSuccess.codesnippet b/Autocomplete Snippets/DZURLSession/thenOnSuccess.codesnippet
index e071dcc..ab50e38 100644
--- a/Autocomplete Snippets/DZURLSession/thenOnSuccess.codesnippet
+++ b/Autocomplete Snippets/DZURLSession/thenOnSuccess.codesnippet
@@ -9,10 +9,10 @@
CodeBlock
IDECodeSnippetContents
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
-
+ .thenInBackground(^(DZResponse *responded) {
+
<#Handle response #>
-
+
});
IDECodeSnippetIdentifier
9EB916CE-5AB9-4297-8BDB-69CE8793D6E6
diff --git a/DZNetworking.xcodeproj/project.pbxproj b/DZNetworking.xcodeproj/project.pbxproj
index 7bbfa1a..13791dd 100644
--- a/DZNetworking.xcodeproj/project.pbxproj
+++ b/DZNetworking.xcodeproj/project.pbxproj
@@ -13,6 +13,8 @@
F024471E1B6125AC004C65F9 /* DZCommon.m in Sources */ = {isa = PBXBuildFile; fileRef = F024471C1B6125AC004C65F9 /* DZCommon.m */; };
F02447221B612793004C65F9 /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F02447201B61278A004C65F9 /* MobileCoreServices.framework */; };
F02447241B612873004C65F9 /* DZUploadTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F02447231B612873004C65F9 /* DZUploadTests.m */; };
+ F038A8961B6773B000349EA8 /* DZResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = F038A8941B6773B000349EA8 /* DZResponse.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ F038A8971B6773B000349EA8 /* DZResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = F038A8951B6773B000349EA8 /* DZResponse.m */; };
F04331CD1B5F931900A327C8 /* DZURLSessionProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = F04331CC1B5F931900A327C8 /* DZURLSessionProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
F078628D1B66104800E5740C /* OMGHTTPURLRQ.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F078628B1B66104800E5740C /* OMGHTTPURLRQ.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
F078628E1B66104800E5740C /* PromiseKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F078628C1B66104800E5740C /* PromiseKit.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
@@ -38,6 +40,8 @@
F024471C1B6125AC004C65F9 /* DZCommon.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DZCommon.m; path = Utilities/DZCommon.m; sourceTree = ""; };
F02447201B61278A004C65F9 /* MobileCoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MobileCoreServices.framework; path = System/Library/Frameworks/MobileCoreServices.framework; sourceTree = SDKROOT; };
F02447231B612873004C65F9 /* DZUploadTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DZUploadTests.m; sourceTree = ""; };
+ F038A8941B6773B000349EA8 /* DZResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DZResponse.h; path = Core/DZResponse.h; sourceTree = ""; };
+ F038A8951B6773B000349EA8 /* DZResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DZResponse.m; path = Core/DZResponse.m; sourceTree = ""; };
F04331CC1B5F931900A327C8 /* DZURLSessionProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DZURLSessionProtocol.h; path = Core/DZURLSessionProtocol.h; sourceTree = ""; };
F078628B1B66104800E5740C /* OMGHTTPURLRQ.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OMGHTTPURLRQ.framework; path = ../Carthage/Build/iOS/OMGHTTPURLRQ.framework; sourceTree = ""; };
F078628C1B66104800E5740C /* PromiseKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PromiseKit.framework; path = ../Carthage/Build/iOS/PromiseKit.framework; sourceTree = ""; };
@@ -157,6 +161,8 @@
F0FC8E701B4FB22900FDA46C /* Core */ = {
isa = PBXGroup;
children = (
+ F038A8941B6773B000349EA8 /* DZResponse.h */,
+ F038A8951B6773B000349EA8 /* DZResponse.m */,
F0FC8E6C1B4FB22600FDA46C /* DZURLSession.h */,
F0FC8E6D1B4FB22600FDA46C /* DZURLSession.m */,
F024470C1B612564004C65F9 /* DZUploadSession.h */,
@@ -181,6 +187,7 @@
F0D0E2431B63EA900077F186 /* DZS3UploadSession.h in Headers */,
F0D0E2471B63F0700077F186 /* DZS3CredentialsManager.h in Headers */,
F024471D1B6125AC004C65F9 /* DZCommon.h in Headers */,
+ F038A8961B6773B000349EA8 /* DZResponse.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -320,6 +327,7 @@
F024471E1B6125AC004C65F9 /* DZCommon.m in Sources */,
F0FC8E6F1B4FB22600FDA46C /* DZURLSession.m in Sources */,
F024470F1B612564004C65F9 /* DZUploadSession.m in Sources */,
+ F038A8971B6773B000349EA8 /* DZResponse.m in Sources */,
F0D0E2441B63EA900077F186 /* DZS3UploadSession.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
diff --git a/DZNetworking/Core/DZResponse.h b/DZNetworking/Core/DZResponse.h
new file mode 100644
index 0000000..1a2061b
--- /dev/null
+++ b/DZNetworking/Core/DZResponse.h
@@ -0,0 +1,19 @@
+//
+// DZResponse.h
+// DZNetworking
+//
+// Created by Nikhil Nigade on 7/28/15.
+// Copyright (c) 2015 Dezine Zync Studios LLP. All rights reserved.
+//
+
+#import
+
+@interface DZResponse : NSObject
+
+@property (nonatomic, copy, readonly) id responseObject;
+@property (nonatomic, copy, readonly) NSHTTPURLResponse *response;
+@property (nonatomic, copy, readonly) NSURLSessionTask *task;
+
+- (instancetype)initWithData:(id)responseObject :(NSHTTPURLResponse *)response :(NSURLSessionTask *)task;
+
+@end
diff --git a/DZNetworking/Core/DZResponse.m b/DZNetworking/Core/DZResponse.m
new file mode 100644
index 0000000..d2da373
--- /dev/null
+++ b/DZNetworking/Core/DZResponse.m
@@ -0,0 +1,27 @@
+//
+// DZResponse.m
+// DZNetworking
+//
+// Created by Nikhil Nigade on 7/28/15.
+// Copyright (c) 2015 Dezine Zync Studios LLP. All rights reserved.
+//
+
+#import "DZResponse.h"
+
+@implementation DZResponse
+
+- (instancetype)initWithData:(id)responseObject :(NSHTTPURLResponse *)response :(NSURLSessionTask *)task
+{
+
+ if(self = [super init])
+ {
+ _responseObject = responseObject;
+ _response = response;
+ _task = task;
+ }
+
+ return self;
+
+}
+
+@end
diff --git a/DZNetworking/Core/DZURLSession.m b/DZNetworking/Core/DZURLSession.m
index ae50384..8539806 100644
--- a/DZNetworking/Core/DZURLSession.m
+++ b/DZNetworking/Core/DZURLSession.m
@@ -112,6 +112,11 @@ - (DZPromise *)POST:(NSString *)URI
NSMutableURLRequest *req = [OMGHTTPURLRQ POST:url :params];
+ if(self.requestModifier)
+ {
+ req = [self.requestModifier(req) mutableCopy];
+ }
+
resolve([self requestWithReq:req.copy]);
}
else
@@ -143,14 +148,26 @@ - (DZPromise *)PUT:(NSString *)URI
return [DZPromise promiseWithResolverBlock:^(PMKResolver resolve) {
- NSString *url = [NSURL URLWithString:URI relativeToURL:self.baseURL].absoluteString;
-
- id queryString = OMGFormURLEncode(query);
- if (queryString) url = [url stringByAppendingFormat:@"?%@", queryString];
-
- NSMutableURLRequest *req = [OMGHTTPURLRQ PUT:url :params];
-
- resolve([self requestWithReq:req.copy]);
+ if(query)
+ {
+ NSString *url = [NSURL URLWithString:URI relativeToURL:self.baseURL].absoluteString;
+
+ id queryString = OMGFormURLEncode(query);
+ if (queryString) url = [url stringByAppendingFormat:@"?%@", queryString];
+
+ NSMutableURLRequest *req = [OMGHTTPURLRQ PUT:url :params];
+
+ if(self.requestModifier)
+ {
+ req = [self.requestModifier(req) mutableCopy];
+ }
+
+ resolve([self requestWithReq:req.copy]);
+ }
+ else
+ {
+ resolve([self requestWithURI:URI method:@"PUT" params:params]);
+ }
}]
.then(^(DZPromise *promise) {
@@ -331,47 +348,14 @@ - (DZPromise *)requestWithURI:(NSString *)URI
}
- __block NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
-
- if(error)
- {
- resolve(error);
- return;
- }
-
- NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
-
- NSError *jsonError;
- id responseObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
-
- if(res.statusCode > self.maximumSuccessStatusCode)
- {
-
- // Treat this as an error.
-
- NSDictionary *userInfo = @{DZErrorData : data,
- DZErrorTask : task};
-
- NSError *error = [NSError errorWithDomain:DZErrorDomain code:res.statusCode userInfo:userInfo];
-
- resolve(error);
- return;
-
- }
-
- if(jsonError)
- {
- resolve(jsonError);
- return;
- }
-
- resolve(PMKManifold(responseObject, res, task));
-
- }];
+ resolve(request);
- [task resume];
+ }]
+ .then(^(NSURLRequest *request) {
- }];
+ return [self requestWithReq:request];
+
+ });
}
@@ -409,10 +393,12 @@ - (DZPromise *)requestWithReq:(NSURLRequest *)request
}
- if(res.statusCode == 200)
+ if(res.statusCode == 200 && !responseObject)
{
// our request succeeded but returned no data. Treat valid.
- resolve(PMKManifold(@{}, res, task));
+ DZResponse *obj = [[DZResponse alloc] initWithData:responseObject :res :task];
+
+ resolve(obj);
return;
}
@@ -422,7 +408,9 @@ - (DZPromise *)requestWithReq:(NSURLRequest *)request
return;
}
- resolve(PMKManifold(responseObject, res, task));
+ DZResponse *obj = [[DZResponse alloc] initWithData:responseObject :res :task];
+
+ resolve(obj);
}];
@@ -441,6 +429,10 @@ - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticat
{
[self.delegate URLSession:session didReceiveChallenge:challenge completionHandler:completionHandler];
}
+ else
+ {
+ completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
+ }
}
diff --git a/DZNetworking/Core/DZUploadSession.m b/DZNetworking/Core/DZUploadSession.m
index fe73074..04c0c4e 100644
--- a/DZNetworking/Core/DZUploadSession.m
+++ b/DZNetworking/Core/DZUploadSession.m
@@ -29,11 +29,12 @@
//
#import "DZUploadSession.h"
+#import
#import
@interface DZUploadSession ()
-@property (nonatomic, strong) NSURLSession *session;
+@property (nonatomic, strong) DZURLSession *session;
@end
@@ -60,10 +61,7 @@ - (instancetype)init
if(self = [super init])
{
- NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
- defaultConfig.HTTPMaximumConnectionsPerHost = 1;
-
- _session = [NSURLSession sessionWithConfiguration:defaultConfig];
+ _session = [[DZURLSession alloc] init];
}
@@ -94,47 +92,14 @@ - (DZPromise *)UPLOAD:(NSString *)path
NSMutableURLRequest *request = [OMGHTTPURLRQ POST:URL :processed];
- __block NSURLSessionUploadTask *task = [self.session uploadTaskWithRequest:request.copy fromData:nil completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
-
- if(error)
- {
- resolve(error);
- return;
- }
-
- NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
-
- NSError *jsonError;
- id responseObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
-
- if(res.statusCode > 399)
- {
-
- // Treat this as an error.
-
- NSDictionary *userInfo = @{DZErrorData : data,
- DZErrorTask : task};
-
- NSError *error = [NSError errorWithDomain:DZErrorDomain code:res.statusCode userInfo:userInfo];
-
- resolve(error);
- return;
-
- }
-
- if(jsonError)
- {
- resolve(jsonError);
- return;
- }
-
- resolve(PMKManifold(responseObject, res, task));
-
- }];
+ resolve(request);
- [task resume];
+ }]
+ .then(^(NSURLRequest *request) {
+
+ return [self.session POST:request];
- }];
+ });
}
diff --git a/DZNetworking/DZNetworking.h b/DZNetworking/DZNetworking.h
index 6796e1e..fe9eea5 100644
--- a/DZNetworking/DZNetworking.h
+++ b/DZNetworking/DZNetworking.h
@@ -41,6 +41,7 @@ FOUNDATION_EXPORT double DZNetworkingVersionNumber;
FOUNDATION_EXPORT const unsigned char DZNetworkingVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import
+#import
#import
#import
#import
diff --git a/DZNetworking/Utilities/DZCommon.h b/DZNetworking/Utilities/DZCommon.h
index b47f80f..20f117e 100644
--- a/DZNetworking/Utilities/DZCommon.h
+++ b/DZNetworking/Utilities/DZCommon.h
@@ -11,6 +11,7 @@
#import
#import
#import
+#import
#ifdef DEBUG
diff --git a/DZNetworkingTests/DZNetworkingTests.m b/DZNetworkingTests/DZNetworkingTests.m
index 70736d2..11b1232 100644
--- a/DZNetworkingTests/DZNetworkingTests.m
+++ b/DZNetworkingTests/DZNetworkingTests.m
@@ -78,7 +78,9 @@ - (void)testRequestModifier
XCTestExpectation *expectation = [self expectationWithDescription:@"GET:/post/1"];
[_session GET:@"/posts/1" parameters:nil]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
+
+ NSURLSessionTask *task = responded.task;
if([task.response.URL.absoluteString containsString:extraQueryParams])
{
@@ -102,7 +104,9 @@ - (void)testGET
XCTestExpectation *expectation = [self expectationWithDescription:@"GET:/post/1"];
[_session GET:@"/posts/1" parameters:nil]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
+
+ id responseObject = responded.responseObject;
if(responseObject &&
[responseObject isKindOfClass:[NSDictionary class]])
@@ -136,7 +140,9 @@ - (void)testPOST
@"body": @"bar",
@"userId" : @1
}]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
+
+ id responseObject = responded.responseObject;
if(responseObject &&
[responseObject isKindOfClass:[NSDictionary class]])
@@ -169,12 +175,15 @@ - (void)testPOSTWithQuery
XCTestExpectation *expectation = [self expectationWithDescription:@"POST:/posts"];
- [_session POST:@"/posts" parameters:@{
- @"title": @"foo",
- @"body": @"bar",
- @"userId" : @1
- }]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ [_session POST:@"/posts" queryParams:@{@"foo" : @"bar"}
+ parameters:@{
+ @"title": @"foo",
+ @"body": @"bar",
+ @"userId" : @1
+ }]
+ .thenInBackground(^(DZResponse *responded) {
+
+ id responseObject = responded.responseObject;
if(responseObject &&
[responseObject isKindOfClass:[NSDictionary class]])
@@ -214,7 +223,9 @@ - (void)testPUT
@"userId" : @1,
@"id" : @1
}]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
+
+ id responseObject = responded.responseObject;
if(responseObject &&
[responseObject isKindOfClass:[NSDictionary class]])
@@ -248,7 +259,9 @@ - (void)testPUTWithQuery
@"userId" : @1,
@"id" : @1
}]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
+
+ id responseObject = responded.responseObject;
if(responseObject &&
[responseObject isKindOfClass:[NSDictionary class]])
@@ -278,7 +291,9 @@ - (void)testPATCH
[_session PUT:@"/posts/1"
queryParams:@{@"foo" : @"bar"}
parameters:@{@"title": @"foo"}]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
+
+ id responseObject = responded.responseObject;
if(responseObject &&
[responseObject isKindOfClass:[NSDictionary class]])
@@ -305,7 +320,9 @@ - (void)testDELETE
XCTestExpectation *expectation = [self expectationWithDescription:@"DELETE:/posts/1"];
[_session DELETE:@"/posts/1" parameters:nil]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
+
+ NSHTTPURLResponse *response = responded.response;
if(response.statusCode <= 204)
{
diff --git a/DZNetworkingTests/DZUploadTests.m b/DZNetworkingTests/DZUploadTests.m
index cf4c26e..ad13a0b 100644
--- a/DZNetworkingTests/DZUploadTests.m
+++ b/DZNetworkingTests/DZUploadTests.m
@@ -62,7 +62,7 @@ - (void)testFileUpload
}
[_session UPLOAD:path fieldName:@"file" URL:URL parameters:nil]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
[expectation fulfill];
@@ -91,7 +91,7 @@ - (void)testDataUpload
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[_session UPLOAD:data name:@"sample.txt" fieldName:@"file" URL:URL parameters:nil]
- .thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
+ .thenInBackground(^(DZResponse *responded) {
[expectation fulfill];