Skip to content

Commit

Permalink
added DZResponse class which is now the resolved value of all requests.
Browse files Browse the repository at this point in the history
- The autocompletion snippet has also been updated to reflect the same.
  • Loading branch information
dezinezync committed Jul 28, 2015
1 parent 6fd96af commit 86b3f43
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 112 deletions.
6 changes: 3 additions & 3 deletions Autocomplete Snippets/DZURLSession/thenOnSuccess.codesnippet
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<string>CodeBlock</string>
</array>
<key>IDECodeSnippetContents</key>
<string>.thenInBackground(^(id responseObject, NSHTTPURLResponse *response, NSURLSessionDataTask *task) {
<string>.thenInBackground(^(DZResponse *responded) {

&lt;#Handle response #&gt;

});</string>
<key>IDECodeSnippetIdentifier</key>
<string>9EB916CE-5AB9-4297-8BDB-69CE8793D6E6</string>
Expand Down
8 changes: 8 additions & 0 deletions DZNetworking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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, ); }; };
Expand All @@ -38,6 +40,8 @@
F024471C1B6125AC004C65F9 /* DZCommon.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DZCommon.m; path = Utilities/DZCommon.m; sourceTree = "<group>"; };
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 = "<group>"; };
F038A8941B6773B000349EA8 /* DZResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DZResponse.h; path = Core/DZResponse.h; sourceTree = "<group>"; };
F038A8951B6773B000349EA8 /* DZResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DZResponse.m; path = Core/DZResponse.m; sourceTree = "<group>"; };
F04331CC1B5F931900A327C8 /* DZURLSessionProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DZURLSessionProtocol.h; path = Core/DZURLSessionProtocol.h; sourceTree = "<group>"; };
F078628B1B66104800E5740C /* OMGHTTPURLRQ.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OMGHTTPURLRQ.framework; path = ../Carthage/Build/iOS/OMGHTTPURLRQ.framework; sourceTree = "<group>"; };
F078628C1B66104800E5740C /* PromiseKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PromiseKit.framework; path = ../Carthage/Build/iOS/PromiseKit.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -157,6 +161,8 @@
F0FC8E701B4FB22900FDA46C /* Core */ = {
isa = PBXGroup;
children = (
F038A8941B6773B000349EA8 /* DZResponse.h */,
F038A8951B6773B000349EA8 /* DZResponse.m */,
F0FC8E6C1B4FB22600FDA46C /* DZURLSession.h */,
F0FC8E6D1B4FB22600FDA46C /* DZURLSession.m */,
F024470C1B612564004C65F9 /* DZUploadSession.h */,
Expand All @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions DZNetworking/Core/DZResponse.h
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>

@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
27 changes: 27 additions & 0 deletions DZNetworking/Core/DZResponse.m
Original file line number Diff line number Diff line change
@@ -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
92 changes: 42 additions & 50 deletions DZNetworking/Core/DZURLSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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];

});

}

Expand Down Expand Up @@ -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;
}

Expand All @@ -422,7 +408,9 @@ - (DZPromise *)requestWithReq:(NSURLRequest *)request
return;
}

resolve(PMKManifold(responseObject, res, task));
DZResponse *obj = [[DZResponse alloc] initWithData:responseObject :res :task];

resolve(obj);

}];

Expand All @@ -441,6 +429,10 @@ - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticat
{
[self.delegate URLSession:session didReceiveChallenge:challenge completionHandler:completionHandler];
}
else
{
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}

}

Expand Down
53 changes: 9 additions & 44 deletions DZNetworking/Core/DZUploadSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
//

#import "DZUploadSession.h"
#import <DZNetworking/DZURLSession.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface DZUploadSession () <NSURLSessionTaskDelegate>

@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) DZURLSession *session;

@end

Expand All @@ -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];

}

Expand Down Expand Up @@ -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];

}];
});

}

Expand Down
1 change: 1 addition & 0 deletions DZNetworking/DZNetworking.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <DZNetworking/PublicHeader.h>
#import <DZNetworking/DZResponse.h>
#import <DZNetworking/DZURLSession.h>
#import <DZNetworking/DZUploadSession.h>
#import <DZNetworking/DZS3CredentialsManager.h>
Expand Down
1 change: 1 addition & 0 deletions DZNetworking/Utilities/DZCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <DZNetworking/DZURLSessionProtocol.h>
#import <PromiseKit/PromiseKit.h>
#import <OMGHTTPURLRQ/OMGHTTPURLRQ.h>
#import <DZNetworking/DZResponse.h>

#ifdef DEBUG

Expand Down
Loading

0 comments on commit 86b3f43

Please sign in to comment.