Permalink
Browse files

Undo async calls. Refactor

  • Loading branch information...
1 parent 64ea8c0 commit bbf180ef6469533d3ce6af07669b7f1db73c0b1a @GlennChiu committed Oct 31, 2012
Showing with 26 additions and 45 deletions.
  1. +3 −3 GCNetworkRequest.h
  2. +23 −42 GCNetworkRequest.m
View
6 GCNetworkRequest.h
@@ -70,6 +70,9 @@ typedef enum : unsigned char
@protocol GCMultiPartFormData <NSObject>
+- (void)addTextData:(NSString *)string
+ name:(NSString *)name;
+
- (void)addData:(NSData *)data
name:(NSString *)name;
@@ -85,7 +88,4 @@ typedef enum : unsigned char
name:(NSString *)name
mimeType:(NSString *)mimeType;
-- (void)addTextData:(NSString *)string
- name:(NSString *)name;
-
@end
View
65 GCNetworkRequest.m
@@ -81,12 +81,12 @@ @implementation GCNetworkRequest
+ (GCNetworkRequest *)requestWithURLString:(NSString *)url
{
- return [[[self class] alloc] initWithURLString:url HTTPMethod:nil parameters:nil encoding:0 multiPartFormDataHandler:nil];
+ return [[[self class] alloc] initWithURLString:url HTTPMethod:@"GET" parameters:nil encoding:GCParameterEncodingURL multiPartFormDataHandler:nil];
}
+ (GCNetworkRequest *)requestWithURLString:(NSString *)url HTTPMethod:(NSString *)method parameters:(NSDictionary *)parameters
{
- return [[[self class] alloc] initWithURLString:url HTTPMethod:method parameters:parameters encoding:0 multiPartFormDataHandler:nil];
+ return [[[self class] alloc] initWithURLString:url HTTPMethod:method parameters:parameters encoding:GCParameterEncodingURL multiPartFormDataHandler:nil];
}
+ (GCNetworkRequest *)requestWithURLString:(NSString *)url HTTPMethod:(NSString *)method parameters:(NSDictionary *)parameters encoding:(GCParameterEncoding)encoding
@@ -96,7 +96,7 @@ + (GCNetworkRequest *)requestWithURLString:(NSString *)url HTTPMethod:(NSString
+ (GCNetworkRequest *)requestWithURLString:(NSString *)url parameters:(NSDictionary *)parameters multiPartFormDataHandler:(void(^)(id <GCMultiPartFormData> formData))block
{
- return [[[self class] alloc] initWithURLString:url HTTPMethod:@"POST" parameters:parameters encoding:0 multiPartFormDataHandler:block];
+ return [[[self class] alloc] initWithURLString:url HTTPMethod:@"POST" parameters:parameters encoding:GCParameterEncodingURL multiPartFormDataHandler:block];
}
- (id)initWithURLString:(NSString *)url HTTPMethod:(NSString *)method parameters:(NSDictionary *)parameters encoding:(GCParameterEncoding)encoding multiPartFormDataHandler:(void(^)(id <GCMultiPartFormData> formData))block
@@ -136,29 +136,20 @@ - (id)initWithURLString:(NSString *)url HTTPMethod:(NSString *)method parameters
}
else
{
- __block void(^multipart_blk)(id <GCMultiPartFormData>) = [block copy];
+ GCNetworkRequestMultiPartFormData *multiPartFormData = [[GCNetworkRequestMultiPartFormData alloc] initWithNetworkRequest:self];
- dispatch_block_t blk = ^{
-
- GCNetworkRequestMultiPartFormData *multiPartFormData = [[GCNetworkRequestMultiPartFormData alloc] initWithNetworkRequest:self];
-
- if (parameters)
- {
- [parameters enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
-
- NSData *data = ([value isKindOfClass:[NSData class]]) ? value : GCUTF8EncodedStringToData([value description]);
- [multiPartFormData addData:data name:[key description]];
- }];
- }
-
- multipart_blk(multiPartFormData);
-
- [multiPartFormData finishMultiPartFormData];
-
- multipart_blk = nil;
- };
+ if (parameters)
+ {
+ [parameters enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
+
+ NSData *data = ([value isKindOfClass:[NSData class]]) ? value : GCUTF8EncodedStringToData([value description]);
+ [multiPartFormData addData:data name:[key description]];
+ }];
+ }
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), blk);
+ block(multiPartFormData);
+
+ [multiPartFormData finishMultiPartFormData];
}
}
return self;
@@ -187,28 +178,18 @@ - (NSString *)queryStringFromParameters:(NSDictionary *)parameters
- (void)URLEncodingFromParameters:(NSDictionary *)parameters
{
- dispatch_block_t block = ^{
-
- [self setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
- NSData *urlEncodedData = GCUTF8EncodedStringToData([self queryStringFromParameters:parameters]);
- if (!urlEncodedData) GCNRLog(@"Error: Encoding query URL parameters failed");
- [self setHTTPBody:urlEncodedData];
- };
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), block);
+ [self setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
+ NSData *urlEncodedData = GCUTF8EncodedStringToData([self queryStringFromParameters:parameters]);
+ if (!urlEncodedData) GCNRLog(@"Error: Encoding query URL parameters failed");
+ [self setHTTPBody:urlEncodedData];
}
- (void)JSONEncodingFromParameters:(NSDictionary *)parameters
{
- dispatch_block_t block = ^{
-
- [self setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
- NSData *jsonData = [self dataFromJSON:parameters];
- NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- [self setHTTPBody:GCUTF8EncodedStringToData(jsonString)];
- };
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), block);
+ [self setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
+ NSData *jsonData = [self dataFromJSON:parameters];
+ NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
+ [self setHTTPBody:GCUTF8EncodedStringToData(jsonString)];
}
- (void)addQueryStringToURLWithURLString:(NSString *)url parameters:(NSDictionary *)parameters

0 comments on commit bbf180e

Please sign in to comment.