Closed Xmay closed 11 years ago
Hello there,
i though some Kind of UrlRequestFactory would a great advantage for AFNetworking, because i would cover many more usecases.
A little example to show you what i mean.
@implementation AFHTTPClient (UrlRequestFactory)
(NSMutableURLRequest )requestFromUrl: (NSURL ) url withMethode:(NSString )method andParameters:(NSDictionary )parameters { NSParameterAssert(method); NSParameterAssert(url);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:method]; [request setAllHTTPHeaderFields:[self valueForKey:@"defaultHeaders"]];
if (parameters) { if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) { NSString* stringUrl = [url absoluteString]; url = [NSURL URLWithString:[stringUrl stringByAppendingFormat:[stringUrl rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]]; [request setURL:url]; } else { NSString charset = (__bridge NSString )CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding)); NSError *error = nil; switch (self.parameterEncoding) { case AFFormURLParameterEncoding:; [request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]]; break; case AFJSONParameterEncoding:; [request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]]; break; case AFPropertyListParameterEncoding:; [request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]]; break; } if (error) { NSLog(@"%@ %@: %@", [self class], NSStringFromSelector(_cmd), error); } } }
return request; }
@end
It is just a littlebit modified version of the method requestWithMethod:path:parameters:
i can't see any issues with that, except the network reachability services (which seems to be depending on the baseUrl).
Maybe worth implementing in the next release?
Thanks, Nu doesn't integrate or have any dependencies on AFNetworking, so I'm going to assume this is misplaced.
Hello there,
i though some Kind of UrlRequestFactory would a great advantage for AFNetworking, because i would cover many more usecases.
A little example to show you what i mean.
@implementation AFHTTPClient (UrlRequestFactory)
(NSMutableURLRequest )requestFromUrl: (NSURL ) url withMethode:(NSString )method andParameters:(NSDictionary )parameters { NSParameterAssert(method); NSParameterAssert(url);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:method]; [request setAllHTTPHeaderFields:[self valueForKey:@"defaultHeaders"]];
if (parameters) { if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) { NSString* stringUrl = [url absoluteString]; url = [NSURL URLWithString:[stringUrl stringByAppendingFormat:[stringUrl rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]]; [request setURL:url]; } else { NSString charset = (__bridge NSString )CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding)); NSError *error = nil; switch (self.parameterEncoding) { case AFFormURLParameterEncoding:; [request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]]; break; case AFJSONParameterEncoding:; [request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]]; break; case AFPropertyListParameterEncoding:; [request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]]; break; } if (error) { NSLog(@"%@ %@: %@", [self class], NSStringFromSelector(_cmd), error); } } }
return request; }
@end
It is just a littlebit modified version of the method requestWithMethod:path:parameters:
i can't see any issues with that, except the network reachability services (which seems to be depending on the baseUrl).
Maybe worth implementing in the next release?