From b8ca3496f846096ab8ded3d22aac0df716f9779f Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Wed, 12 Oct 2011 10:41:59 -0500 Subject: [PATCH] Adding -unregisterHTTPOperationClass to AFHTTPClient Removing 'Accept: applciation/json' default header from AFHTTPClient Revising documentation for AFHTTPClient --- AFNetworking/AFHTTPClient.h | 35 +++++++++++++++--------- AFNetworking/AFHTTPClient.m | 19 ++++--------- Mac Example/Classes/AFGowallaAPIClient.m | 3 ++ iOS Example/Classes/AFGowallaAPIClient.m | 3 ++ 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/AFNetworking/AFHTTPClient.h b/AFNetworking/AFHTTPClient.h index 0a3f555..3223b1c 100644 --- a/AFNetworking/AFHTTPClient.h +++ b/AFNetworking/AFHTTPClient.h @@ -54,7 +54,6 @@ typedef enum { By default, `AFHTTPClient` sets the following HTTP headers: - - `Accept: application/json` - `Accept-Encoding: gzip` - `Accept-Language: #{[NSLocale preferredLanguages]}, en-us;q=0.8` - `User-Agent: #{generated user agent}` @@ -86,7 +85,7 @@ typedef enum { @property (nonatomic, assign) NSStringEncoding stringEncoding; /** - + The `AFHTTPClientParameterEncoding` value corresponding to how parameters are encoded into a request body. This is `AFFormURLParameterEncoding` by default. */ @property (nonatomic, assign) AFHTTPClientParameterEncoding parameterEncoding; @@ -113,7 +112,7 @@ typedef enum { @param url The base URL for the HTTP client. This argument must not be nil. - @discussion This is the designated initializer for `AFHTTPClient` + @discussion This is the designated initializer. @return The newly-initialized HTTP client */ @@ -123,8 +122,28 @@ typedef enum { /// @name Managing HTTP Operations ///---------------------------------- +/** + Attempts to register a class conforming to the `AFHTTPClientOperation` protocol, adding it to a chain to automatically generate request operations from a URL request. + + @param The class conforming to the `AFHTTPClientOperation` protocol to register + + @return `YES` if the registration is successful, `NO` otherwise. The only failure condition is if `operationClass` does not conform to the `AFHTTPCLientOperation` protocol. + + @discussion When `requestWithMethod:path:parameters` is invoked, each registered class is consulted in turn to see if it can handle the specific request. The first class to return `YES` when sent a `canProcessRequest:` message is used to generate an operation using `HTTPRequestOperationWithRequest:success:failure:`. There is no guarantee that all registered classes will be consulted. Classes are consulted in the reverse order of their registration. Attempting to register an already-registered class will move it to the top of the chain. + + @see `AFHTTPClientOperation` + */ - (BOOL)registerHTTPOperationClass:(Class)operationClass; +/** + Unregisteres the specified class conforming to the `AFHTTPClientOperation` protocol. + + @param The class conforming to the `AFHTTPClientOperation` protocol to unregister + + @discussion After this method is invoked, `operationClass` is no longer consulted when `requestWithMethod:path:parameters` is invoked. + */ +- (void)unregisterHTTPOperationClass:(Class)operationClass; + ///---------------------------------- /// @name Managing HTTP Header Values ///---------------------------------- @@ -367,16 +386,6 @@ typedef enum { */ - (void)appendPartWithFileData:(NSData *)data mimeType:(NSString *)mimeType name:(NSString *)name; -/** - Appends the HTTP header `Content-Disposition: file; filename=#{filename}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary. - - @param fileURL The URL for the local file to have its contents appended to the form data. This parameter must not be `nil`. - @param mimeType The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be `nil`. - @param fileName The filename to be associated with the file contents. This parameter must not be `nil`. - @param error If an error occurs, upon return contains an `NSError` object that describes the problem. - */ -- (void)appendPartWithFile:(NSURL *)fileURL mimeType:(NSString *)mimeType fileName:(NSString *)fileName error:(NSError **)error; - /** Appends encoded data to the form data. diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index 42b2b40..9a24ce7 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -163,9 +163,6 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) { self.defaultHeaders = [NSMutableDictionary dictionary]; - // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 - [self setDefaultHeader:@"Accept" value:@"application/json"]; - // Accept-Encoding HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3 [self setDefaultHeader:@"Accept-Encoding" value:@"gzip"]; @@ -208,6 +205,11 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) { return YES; } +- (void)unregisterHTTPOperationClass:(Class)operationClass { + NSString *className = NSStringFromClass(operationClass); + [self.registeredHTTPOperationClassNames removeObject:className]; +} + #pragma mark - - (NSString *)defaultValueForHeader:(NSString *)header { @@ -453,17 +455,6 @@ static inline NSString * AFMultipartFormFinalBoundary() { [self appendPartWithHeaders:mutableHeaders body:data]; } -- (void)appendPartWithFile:(NSURL *)fileURL mimeType:(NSString *)mimeType fileName:(NSString *)fileName error:(NSError **)error { - NSData *data = [NSData dataWithContentsOfFile:[fileURL absoluteString] options:0 error:error]; - if (data) { - NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary]; - [mutableHeaders setValue:[NSString stringWithFormat:@"file; filename=\"%@\"", fileName] forKey:@"Content-Disposition"]; - [mutableHeaders setValue:mimeType forKey:@"Content-Type"]; - - [self appendPartWithHeaders:mutableHeaders body:data]; - } -} - - (void)appendData:(NSData *)data { [self.mutableData appendData:data]; } diff --git a/Mac Example/Classes/AFGowallaAPIClient.m b/Mac Example/Classes/AFGowallaAPIClient.m index 53c7f02..1f0b5e7 100644 --- a/Mac Example/Classes/AFGowallaAPIClient.m +++ b/Mac Example/Classes/AFGowallaAPIClient.m @@ -49,6 +49,9 @@ NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/"; [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; + // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 + [self setDefaultHeader:@"Accept" value:@"application/json"]; + // X-Gowalla-API-Key HTTP Header; see http://api.gowalla.com/api/docs [self setDefaultHeader:@"X-Gowalla-API-Key" value:kAFGowallaClientID]; diff --git a/iOS Example/Classes/AFGowallaAPIClient.m b/iOS Example/Classes/AFGowallaAPIClient.m index 4f0bb3d..6e8f5f4 100644 --- a/iOS Example/Classes/AFGowallaAPIClient.m +++ b/iOS Example/Classes/AFGowallaAPIClient.m @@ -49,6 +49,9 @@ NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/"; [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; + // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 + [self setDefaultHeader:@"Accept" value:@"application/json"]; + // X-Gowalla-API-Key HTTP Header; see http://api.gowalla.com/api/docs [self setDefaultHeader:@"X-Gowalla-API-Key" value:kAFGowallaClientID];