Skip to content

Commit

Permalink
- Added JBResponseSerializer and JBRequestSerializer for response / r…
Browse files Browse the repository at this point in the history
…equest serialisation manipulation.

- Property parameters are not readonly any more.
  • Loading branch information
Josip Bernat committed Apr 7, 2014
1 parent 6e2ac8a commit 3403e19
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 52 deletions.
27 changes: 26 additions & 1 deletion JBMessage/JBMessage/JBMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ extern JBHTTPMethod const JBHTTPMethodPOST;
extern JBHTTPMethod const JBHTTPMethodPUT;
extern JBHTTPMethod const JBHTTPMethodDELETE;

typedef NS_ENUM(NSInteger, JBResponseSerializerType) {
JBResponseSerializerTypeHTTP = 0, /// AFHTTPResponseSerializer
JBResponseSerializerTypeJSON, /// AFJSONResponseSerializer
JBResponseSerializerTypeXMLParser, /// AFXMLParserResponseSerializer
JBResponseSerializerTypePropertyList, /// AFPropertyListResponseSerializer
JBResponseSerializerTypeImage, /// AFImageResponseSerializer
JBResponseSerializerTypeCompound /// AFCompoundResponseSerializer
};

typedef NS_ENUM(NSInteger, JBRequestSerializerType) {
JBRequestSerializerTypeHTTP = 0, /// AFHTTPRequestSerializer
JBRequestSerializerTypeJSON, /// AFJSONRequestSerializer
JBRequestSerializerTypePropertyList /// AFPropertyListRequestSerializer
};

/**
* Block object containing response object and error. Used as callback when request is done with execution.
*
Expand Down Expand Up @@ -86,13 +101,23 @@ typedef void (^JBDownloadBlock)(NSUInteger bytesRead, NSInteger totalBytesRead,
/**
* Parameters to be send in the request.
*/
@property (nonatomic, strong, readonly) NSDictionary *parameters;
@property (nonatomic, strong) NSDictionary *parameters;

/**
* Authorization token to be send in header values. Default is nil.
*/
@property (nonatomic, strong) NSString *authorizationToken;

/**
* Response serializer used for handling request response. Default is JBResponseSerializerTypeHTTP.
*/
@property (nonatomic, readwrite) JBResponseSerializerType responseSerializer;

/**
* Request serializer used for formatting http body. Default is JBRequestSerializerTypeHTTP.
*/
@property (nonatomic, readwrite) JBRequestSerializerType requestSerializer;

#pragma mark - URL Registration

/**
Expand Down
192 changes: 141 additions & 51 deletions JBMessage/JBMessage/JBMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ @interface JBMessage () {
}

@property (nonatomic) UIBackgroundTaskIdentifier backgroundTaskIdentifier;
@property (nonatomic, strong) NSDictionary *parameters;

@end

@interface JBMessage (Connection)

- (AFHTTPRequestOperationManager *)requestOperationManager;
- (AFHTTPResponseSerializer <AFURLResponseSerialization> *)httpResponseSerializer;
- (AFHTTPRequestSerializer <AFURLRequestSerialization> *)httpRequestSerializer;
- (NSString *)actionUrlString;
- (NSMutableURLRequest *)urlRequest;

@end


@implementation JBMessage

#pragma mark - Memory Management
Expand Down Expand Up @@ -67,16 +77,31 @@ - (id)initWithParameters:(NSDictionary *)parameters
self.parameters = parameters;
self.responseBlock = responseBlock;

_filename = @"filename";
_fileURL = nil;
_authorizationToken = nil;
_httpMethod = JBHTTPMethodPOST;
_shouldCompleteOnMainQueue = YES;
[self initialize];
}

return self;
}

- (id)init {

if (self = [super init]) {

[self initialize];
}
return self;
}

- (void)initialize {

_filename = @"filename";
_fileURL = nil;
_authorizationToken = nil;
_httpMethod = JBHTTPMethodPOST;
_responseSerializer = JBResponseSerializerTypeHTTP;
_shouldCompleteOnMainQueue = YES;
}

#pragma mark - Background Task

- (void)beginBackgroundTask {
Expand Down Expand Up @@ -145,48 +170,10 @@ - (void)operationDidFinish {

- (void)executeRequest {

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
if (self.authorizationToken) {
[manager.requestSerializer setValue:self.authorizationToken forHTTPHeaderField:@"Token"];
}

// NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
// [set addObject:@"text/html"];
// manager.responseSerializer.acceptableContentTypes = set;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

NSMutableURLRequest *request = nil;
__block NSError *uploadError = nil;
NSString *urlString = [NSString stringWithFormat:@"%@%@", baseUrlString, self.action];
NSURLRequest *request = [self urlRequest];
AFHTTPRequestOperationManager *manager = [self requestOperationManager];

__weak id this = self;
if (self.httpMethod == JBHTTPMethodGET || !self.fileURL) {

NSError *error = nil;
request = [manager.requestSerializer requestWithMethod:self.httpMethod
URLString:urlString
parameters:self.parameters
error:&error];
#ifdef DEBUG
if (error) {
NSLog(@"Error while creating NSMutableRequest: %@", error);
}
#endif
}
else {
request = [manager.requestSerializer multipartFormRequestWithMethod:self.httpMethod
URLString:urlString
parameters:self.parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

__strong JBMessage *strongThis = this;
[formData appendPartWithFileURL:strongThis.fileURL
name:strongThis.filename
error:&uploadError];
} error:&uploadError];
}


AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {

Expand All @@ -200,8 +187,6 @@ - (void)executeRequest {
__strong JBMessage *strongThis = this;
[strongThis receivedResponse:nil error:error];
}];


[operation setUploadProgressBlock:self.uploadBlock];
[operation setDownloadProgressBlock:self.downloadBlock];

Expand Down Expand Up @@ -244,9 +229,7 @@ - (id)parseResponse:(id)rawResponse error:(NSError *__autoreleasing *)error{
*error = jsonError;

#ifdef DEBUG
if(jsonError) {
NSLog(@"%@, %@", [jsonError localizedDescription], [[NSString alloc] initWithData:rawResponse encoding:NSUTF8StringEncoding]);
}
if(jsonError) { NSLog(@"%@, %@", [jsonError localizedDescription], [[NSString alloc] initWithData:rawResponse encoding:NSUTF8StringEncoding]); }
#endif
return response;
}
Expand Down Expand Up @@ -275,3 +258,110 @@ - (void)send {
}

@end

@implementation JBMessage (Connection)

#pragma mark - Connection Helpers

- (AFHTTPRequestOperationManager *)requestOperationManager {

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
if (self.authorizationToken) {
[manager.requestSerializer setValue:self.authorizationToken forHTTPHeaderField:@"Token"];
}

manager.responseSerializer = [self httpResponseSerializer];
manager.requestSerializer = [self httpRequestSerializer];

return manager;
}

- (AFHTTPResponseSerializer <AFURLResponseSerialization> *)httpResponseSerializer {

switch (self.responseSerializer) {
case JBResponseSerializerTypeCompound:
return [AFCompoundResponseSerializer serializer];

case JBResponseSerializerTypeHTTP:
return [AFHTTPResponseSerializer serializer];

case JBResponseSerializerTypeImage:
return [AFImageResponseSerializer serializer];

case JBResponseSerializerTypeJSON:
return [AFJSONResponseSerializer serializer];

case JBResponseSerializerTypePropertyList:
return [AFPropertyListResponseSerializer serializer];

case JBResponseSerializerTypeXMLParser:
return [AFXMLParserResponseSerializer serializer];

default:
break;
}
}

- (AFHTTPRequestSerializer <AFURLRequestSerialization> *)httpRequestSerializer {

switch (self.requestSerializer) {
case JBRequestSerializerTypeHTTP:
return [AFHTTPRequestSerializer serializer];
break;

case JBRequestSerializerTypeJSON:
return [AFJSONRequestSerializer serializer];
break;

case JBRequestSerializerTypePropertyList:
return [AFPropertyListRequestSerializer serializer];
break;

default:
break;
}
}

- (NSString *)actionUrlString {
return [NSString stringWithFormat:@"%@%@", baseUrlString, self.action];
}

- (NSMutableURLRequest *)urlRequest {

AFHTTPRequestOperationManager *manager = [self requestOperationManager];
NSMutableURLRequest *request = nil;

if (self.httpMethod == JBHTTPMethodGET || !self.fileURL) {

NSError *error = nil;
request = [manager.requestSerializer requestWithMethod:self.httpMethod
URLString:[self actionUrlString]
parameters:self.parameters
error:&error];
#ifdef DEBUG
if (error) { NSLog(@"Error while creating request: %@", error); }
#endif
}
else {

__weak id this = self;
NSError *multpartError = nil;
request = [manager.requestSerializer multipartFormRequestWithMethod:self.httpMethod
URLString:[self actionUrlString]
parameters:self.parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

__strong JBMessage *strongThis = this;
[formData appendPartWithFileURL:strongThis.fileURL
name:strongThis.filename
error:nil];
} error:&multpartError];
#ifdef DEBUG
if (multpartError) { NSLog(@"Error while creating multpart form request: %@", multpartError); }
#endif
}

return request;
}

@end

0 comments on commit 3403e19

Please sign in to comment.