2011-09-21 22:42:01 -05:00
// AFHTTPClient.h
2011-06-01 11:51:53 -05:00
//
// Copyright (c) 2011 Gowalla (http://gowalla.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
# import <Foundation / Foundation.h>
2011-10-10 10:51:23 -05:00
@ class AFHTTPRequestOperation ;
@ protocol AFHTTPClientOperation ;
2011-09-26 11:50:13 -05:00
@ protocol AFMultipartFormData ;
2011-08-05 10:58:37 -05:00
2011-10-06 14:33:16 -05:00
typedef enum {
AFFormURLParameterEncoding ,
AFJSONParameterEncoding ,
AFPropertyListParameterEncoding ,
} AFHTTPClientParameterEncoding ;
2011-09-21 23:16:35 -05:00
/**
` AFHTTPClient ` objects encapsulates the common patterns of communicating with an application , webservice , or API . It encapsulates persistent information , like base URL , authorization credentials , and HTTP headers , and uses them to construct and manage the execution of HTTP request operations .
2011-09-26 12:51:20 -05:00
In its default implementation , ` AFHTTPClient ` sets the following HTTP headers :
- ` Accept : application / json `
- ` Accept - Encoding : gzip `
- ` Accept - Language : # { [ NSLocale preferredLanguages ] } , en - us ; q = 0.8 `
2011-09-26 16:07:59 -05:00
- ` User - Agent : # { generated user agent } `
2011-09-26 12:51:20 -05:00
You can override these HTTP headers or define new ones using ` setDefaultHeader : value : ` .
# Subclassing Notes
It is strongly recommended that you create an ` AFHTTPClient ` subclass for each website or web application that your application communicates with , and in each subclass , defining a method that returns a singleton object that acts as single a shared HTTP client that holds authentication credentials and other configuration for the entire application .
# # Methods to Override
If an ` AFHTTPClient ` wishes to change the way request parameters are encoded , then the base implementation of ` requestWithMethod : path : parameters : should be overridden . Otherwise , it should be sufficient to take the ` super ` implementation , and configure the resulting ` NSMutableURLRequest ` object accordingly .
2011-09-21 23:16:35 -05:00
*/
2011-09-21 22:42:01 -05:00
@ interface AFHTTPClient : NSObject {
2011-09-16 10:02:51 -05:00
@ private
NSURL * _baseURL ;
2011-09-21 11:36:39 -05:00
NSStringEncoding _stringEncoding ;
2011-10-06 14:33:16 -05:00
AFHTTPClientParameterEncoding _parameterEncoding ;
2011-10-05 15:44:51 -05:00
NSMutableArray * _registeredHTTPOperationClassNames ;
2011-07-27 15:14:15 -05:00
NSMutableDictionary * _defaultHeaders ;
NSOperationQueue * _operationQueue ;
}
2011-09-26 12:51:20 -05:00
///---------------------------------------
/// @name Accessing HTTP Client Properties
///---------------------------------------
2011-09-18 15:06:29 -05:00
/**
2011-09-24 21:53:46 -04:00
The url used as the base for paths specified in methods such as ` getPath : parameteres : success : failure `
2011-09-18 15:06:29 -05:00
*/
@ property ( readonly , nonatomic , retain ) NSURL * baseURL ;
2011-09-21 17:51:05 -05:00
/**
The string encoding used in constructing url requests . This is ` NSUTF8StringEncoding ` by default .
*/
2011-09-21 11:36:39 -05:00
@ property ( nonatomic , assign ) NSStringEncoding stringEncoding ;
2011-10-06 14:33:16 -05:00
/**
*/
@ property ( nonatomic , assign ) AFHTTPClientParameterEncoding parameterEncoding ;
2011-09-21 17:51:05 -05:00
/**
2011-09-21 23:16:35 -05:00
The operation queue which manages operations enqueued by the HTTP client .
2011-09-21 17:51:05 -05:00
*/
2011-09-26 11:06:56 -05:00
@ property ( readonly , nonatomic , retain ) NSOperationQueue * operationQueue ;
2011-09-21 11:36:39 -05:00
2011-09-22 09:43:58 -05:00
///---------------------------------------------
/// @name Creating and Initializing HTTP Clients
///---------------------------------------------
/**
Creates and initializes an ` AFHTTPClient ` object with the specified base URL .
@ param url The base URL for the HTTP client . This argument must not be nil .
@ return The newly - initialized HTTP client
*/
+ ( AFHTTPClient * ) clientWithBaseURL : ( NSURL * ) url ;
2011-09-18 15:06:29 -05:00
/**
2011-09-21 23:16:35 -05:00
Initializes an ` AFHTTPClient ` object with the specified base URL .
2011-09-18 15:06:29 -05:00
2011-09-21 23:16:35 -05:00
@ param url The base URL for the HTTP client . This argument must not be nil .
2011-09-18 15:06:29 -05:00
2011-09-22 09:43:58 -05:00
@ discussion This is the designated initializer for ` AFHTTPClient `
2011-09-21 23:16:35 -05:00
@ return The newly - initialized HTTP client
2011-09-18 15:06:29 -05:00
*/
2011-09-16 10:02:51 -05:00
- ( id ) initWithBaseURL : ( NSURL * ) url ;
2011-10-05 15:44:51 -05:00
///----------------------------------
/// @name Managing HTTP Operations
///----------------------------------
- ( BOOL ) registerHTTPOperationClass : ( Class ) operationClass ;
2011-09-18 15:06:29 -05:00
///----------------------------------
/// @name Managing HTTP Header Values
///----------------------------------
/**
2011-09-26 12:51:20 -05:00
Returns the value for the HTTP headers set in request objects created by the HTTP client .
2011-09-18 15:06:29 -05:00
@ param header The HTTP header to return the default value for
@ return The default value for the HTTP header , or ` nil ` if unspecified
*/
2011-06-01 11:51:53 -05:00
- ( NSString * ) defaultValueForHeader : ( NSString * ) header ;
2011-09-18 15:06:29 -05:00
/**
2011-09-21 23:16:35 -05:00
Sets the value for the HTTP headers set in request objects made by the HTTP client . If ` nil ` , removes the existing value for that header .
2011-09-18 15:06:29 -05:00
@ param header The HTTP header to set a default value for
@ param value The value set as default for the specified header , or ` nil
*/
2011-06-01 11:51:53 -05:00
- ( void ) setDefaultHeader : ( NSString * ) header value : ( NSString * ) value ;
2011-09-18 15:06:29 -05:00
/**
2011-09-21 23:16:35 -05:00
Sets the " Authorization " HTTP header set in request objects made by the HTTP client to a basic authentication value with Base64 - encoded username and password . This overwrites any existing value for this header .
2011-09-18 15:06:29 -05:00
@ param username The HTTP basic auth username
@ param password The HTTP basic auth password
*/
2011-08-24 15:21:17 -05:00
- ( void ) setAuthorizationHeaderWithUsername : ( NSString * ) username password : ( NSString * ) password ;
2011-09-18 15:06:29 -05:00
/**
2011-09-21 23:16:35 -05:00
Sets the " Authorization " HTTP header set in request objects made by the HTTP client to a token - based authentication value , such as an OAuth access token . This overwrites any existing value for this header .
2011-09-18 15:06:29 -05:00
@ param token The authentication token
*/
2011-06-01 11:51:53 -05:00
- ( void ) setAuthorizationHeaderWithToken : ( NSString * ) token ;
2011-09-18 15:06:29 -05:00
/**
Clears any existing value for the " Authorization " HTTP header .
*/
2011-06-01 11:51:53 -05:00
- ( void ) clearAuthorizationHeader ;
2011-09-18 15:06:29 -05:00
///-------------------------------
/// @name Creating Request Objects
///-------------------------------
/**
2011-09-21 23:16:35 -05:00
Creates an ` NSMutableURLRequest ` object with the specified HTTP method and path . If the HTTP method is ` GET ` , the parameters will be used to construct a url - encoded query string that is appended to the request ' s URL . If ` POST ` , ` PUT ` , or ` DELETE ` , the parameters will be encoded into a ` application / x - www - form - urlencoded ` HTTP body .
2011-09-18 15:06:29 -05:00
2011-09-21 17:51:05 -05:00
@ param method The HTTP method for the request , such as ` GET ` , ` POST ` , ` PUT ` , or ` DELETE ` .
2011-09-21 23:16:35 -05:00
@ param path The path to be appended to the HTTP client ' s base URL and used as the request URL .
2011-09-21 17:51:05 -05:00
@ param parameters The parameters to be either set as a query string for ` GET ` requests , or the request HTTP body .
2011-09-18 15:06:29 -05:00
@ return An ` NSMutableURLRequest ` object
*/
2011-08-05 10:58:37 -05:00
- ( NSMutableURLRequest * ) requestWithMethod : ( NSString * ) method
2011-09-26 12:51:20 -05:00
path : ( NSString * ) path
parameters : ( NSDictionary * ) parameters ;
2011-07-27 15:14:15 -05:00
2011-09-21 17:51:05 -05:00
/**
2011-09-26 11:50:13 -05:00
Creates an ` NSMutableURLRequest ` object with the specified HTTP method and path , and constructs a ` multipart / form - data ` HTTP body , using the specified parameters and multipart form data block . See http : //www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2
2011-09-21 17:51:05 -05:00
@ param method The HTTP method for the request . Must be either ` POST ` , ` PUT ` , or ` DELETE ` .
2011-09-21 23:16:35 -05:00
@ param path The path to be appended to the HTTP client ' s base URL and used as the request URL .
2011-09-21 17:51:05 -05:00
@ param parameters The parameters to be encoded and set in the request HTTP body .
2011-09-26 11:50:13 -05:00
@ param block A block that takes a single argument and appends data to the HTTP body . The block argument is an object adopting the ` AFMultipartFormData ` protocol . This can be used to upload files , encode HTTP body as JSON or XML , or specify multiple values for the same parameter , as one might for array values .
2011-09-21 17:51:05 -05:00
2011-09-26 11:50:13 -05:00
@ see AFMultipartFormData
2011-09-21 17:51:05 -05:00
@ return An ` NSMutableURLRequest ` object
*/
2011-09-21 14:00:05 -05:00
- ( NSMutableURLRequest * ) multipartFormRequestWithMethod : ( NSString * ) method
path : ( NSString * ) path
parameters : ( NSDictionary * ) parameters
2011-09-26 11:50:13 -05:00
constructingBodyWithBlock : ( void ( ^ ) ( id < AFMultipartFormData > formData ) ) block ;
2011-09-21 14:00:05 -05:00
2011-09-18 15:06:29 -05:00
///--------------------------------
/// @name Enqueuing HTTP Operations
///--------------------------------
2011-09-16 10:06:11 -05:00
2011-09-21 17:51:05 -05:00
/**
2011-09-21 23:16:35 -05:00
Creates and enqueues an ` AFHTTPRequestOperation ` to the HTTP client ' s operation queue .
2011-09-21 17:51:05 -05:00
@ param request The request object to be loaded asynchronously during execution of the operation .
2011-09-26 14:46:46 -05:00
@ param success A block object to be executed when the request operation finishes successfully , with a status code in the 2 xx range , and with an acceptable content type ( e . g . ` application / json ` ) . This block has no return value and takes a single argument , which is an object created from the response data of request .
2011-09-21 17:51:05 -05:00
@ param failure A block object to be executed when the request operation finishes unsuccessfully , or that finishes successfully , but encountered an error while parsing the resonse data as JSON . This block has no return value and takes a single argument , which is the ` NSError ` object describing the network or parsing error that occurred .
*/
2011-10-05 12:36:45 -05:00
- ( void ) enqueueHTTPRequestOperationWithRequest : ( NSURLRequest * ) request
success : ( void ( ^ ) ( id object ) ) success
failure : ( void ( ^ ) ( NSHTTPURLResponse * response , NSError * error ) ) failure ;
2011-10-06 14:33:16 -05:00
/**
*/
- ( void ) enqueueHTTPRequestOperation : ( AFHTTPRequestOperation * ) operation ;
2011-07-27 15:14:15 -05:00
2011-09-18 15:06:29 -05:00
///---------------------------------
/// @name Cancelling HTTP Operations
///---------------------------------
2011-09-21 17:51:05 -05:00
/**
2011-09-21 23:16:35 -05:00
Cancels all operations in the HTTP client ' s operation queue that match the specified HTTP method and URL .
2011-09-21 17:51:05 -05:00
@ param method The HTTP method to match for the cancelled requests , such as ` GET ` , ` POST ` , ` PUT ` , or ` DELETE ` .
@ param url The URL to match for the cancelled requests .
*/
- ( void ) cancelHTTPOperationsWithMethod : ( NSString * ) method andURL : ( NSURL * ) url ;
2011-08-14 20:19:59 -05:00
2011-09-18 15:06:29 -05:00
///---------------------------
/// @name Making HTTP Requests
///---------------------------
2011-09-21 23:16:35 -05:00
/**
Creates an ` AFHTTPRequestOperation ` with a ` GET ` request , and enqueues it to the HTTP client ' s operation queue .
@ param path The path to be appended to the HTTP client ' s base URL and used as the request URL .
@ param parameters The parameters to be encoded and appended as the query string for the request URL .
2011-09-22 11:31:01 -05:00
@ param success A block object to be executed when the request operation finishes successfully , with a status code in the 2 xx range , and with an acceptable content type ( e . g . ` application / json ` ) . This block has no return value and takes a single argument , which is the response object created from the response data of request .
2011-09-21 23:16:35 -05:00
@ param failure A block object to be executed when the request operation finishes unsuccessfully , or that finishes successfully , but encountered an error while parsing the resonse data as JSON . This block has no return value and takes a single argument , which is the ` NSError ` object describing the network or parsing error that occurred .
2011-09-26 14:46:46 -05:00
@ see enqueueHTTPOperationWithRequest : success : failure
2011-09-21 23:16:35 -05:00
*/
2011-09-21 14:00:05 -05:00
- ( void ) getPath : ( NSString * ) path
parameters : ( NSDictionary * ) parameters
2011-09-26 14:46:46 -05:00
success : ( void ( ^ ) ( id object ) ) success
failure : ( void ( ^ ) ( NSHTTPURLResponse * response , NSError * error ) ) failure ;
2011-06-01 11:51:53 -05:00
2011-09-21 23:16:35 -05:00
/**
Creates an ` AFHTTPRequestOperation ` with a ` POST ` request , and enqueues it to the HTTP client ' s operation queue .
@ param path The path to be appended to the HTTP client ' s base URL and used as the request URL .
@ param parameters The parameters to be encoded and set in the request HTTP body .
2011-09-22 11:31:01 -05:00
@ param success A block object to be executed when the request operation finishes successfully , with a status code in the 2 xx range , and with an acceptable content type ( e . g . ` application / json ` ) . This block has no return value and takes a single argument , which is the response object created from the response data of request .
2011-09-21 23:16:35 -05:00
@ param failure A block object to be executed when the request operation finishes unsuccessfully , or that finishes successfully , but encountered an error while parsing the resonse data as JSON . This block has no return value and takes a single argument , which is the ` NSError ` object describing the network or parsing error that occurred .
2011-09-26 14:46:46 -05:00
@ see enqueueHTTPOperationWithRequest : success : failure
2011-09-21 23:16:35 -05:00
*/
2011-08-05 10:58:37 -05:00
- ( void ) postPath : ( NSString * ) path
parameters : ( NSDictionary * ) parameters
2011-09-26 14:46:46 -05:00
success : ( void ( ^ ) ( id object ) ) success
failure : ( void ( ^ ) ( NSHTTPURLResponse * response , NSError * error ) ) failure ;
2011-08-05 10:58:37 -05:00
2011-09-21 23:16:35 -05:00
/**
Creates an ` AFHTTPRequestOperation ` with a ` PUT ` request , and enqueues it to the HTTP client ' s operation queue .
@ param path The path to be appended to the HTTP client ' s base URL and used as the request URL .
@ param parameters The parameters to be encoded and set in the request HTTP body .
2011-09-22 11:31:01 -05:00
@ param success A block object to be executed when the request operation finishes successfully , with a status code in the 2 xx range , and with an acceptable content type ( e . g . ` application / json ` ) . This block has no return value and takes a single argument , which is the response object created from the response data of request .
2011-09-21 23:16:35 -05:00
@ param failure A block object to be executed when the request operation finishes unsuccessfully , or that finishes successfully , but encountered an error while parsing the resonse data as JSON . This block has no return value and takes a single argument , which is the ` NSError ` object describing the network or parsing error that occurred .
2011-09-26 14:46:46 -05:00
@ see enqueueHTTPOperationWithRequest : success : failure
2011-09-21 23:16:35 -05:00
*/
2011-08-05 10:58:37 -05:00
- ( void ) putPath : ( NSString * ) path
parameters : ( NSDictionary * ) parameters
2011-09-26 14:46:46 -05:00
success : ( void ( ^ ) ( id object ) ) success
failure : ( void ( ^ ) ( NSHTTPURLResponse * response , NSError * error ) ) failure ;
2011-06-01 11:51:53 -05:00
2011-09-21 23:16:35 -05:00
/**
Creates an ` AFHTTPRequestOperation ` with a ` DELETE ` request , and enqueues it to the HTTP client ' s operation queue .
@ param path The path to be appended to the HTTP client ' s base URL and used as the request URL .
@ param parameters The parameters to be encoded and set in the request HTTP body .
2011-09-22 11:31:01 -05:00
@ param success A block object to be executed when the request operation finishes successfully , with a status code in the 2 xx range , and with an acceptable content type ( e . g . ` application / json ` ) . This block has no return value and takes a single argument , which is the response object created from the response data of request .
2011-09-21 23:16:35 -05:00
@ param failure A block object to be executed when the request operation finishes unsuccessfully , or that finishes successfully , but encountered an error while parsing the resonse data as JSON . This block has no return value and takes a single argument , which is the ` NSError ` object describing the network or parsing error that occurred .
2011-09-26 14:46:46 -05:00
@ see enqueueHTTPOperationWithRequest : success : failure
2011-09-21 23:16:35 -05:00
*/
2011-08-05 10:58:37 -05:00
- ( void ) deletePath : ( NSString * ) path
parameters : ( NSDictionary * ) parameters
2011-09-26 14:46:46 -05:00
success : ( void ( ^ ) ( id object ) ) success
failure : ( void ( ^ ) ( NSHTTPURLResponse * response , NSError * error ) ) failure ;
2011-10-10 10:51:23 -05:00
@ end
# pragma mark -
@ protocol AFHTTPClientOperation < NSObject >
+ ( BOOL ) canProcessRequest : ( NSURLRequest * ) request ;
+ ( NSURLRequest * ) canonicalRequestForRequest : ( NSURLRequest * ) request ;
+ ( id ) HTTPRequestOperationWithRequest : ( NSURLRequest * ) urlRequest
success : ( void ( ^ ) ( id object ) ) success
failure : ( void ( ^ ) ( NSHTTPURLResponse * response , NSError * error ) ) failure ;
2011-06-01 11:51:53 -05:00
@ end
2011-09-21 14:00:05 -05:00
# pragma mark -
2011-09-21 23:16:35 -05:00
/**
2011-09-26 11:50:13 -05:00
The ` AFMultipartFormData ` protocol defines the methods supported by the parameter in the block argument of ` multipartFormRequestWithMethod : path : parameters : constructingBodyWithBlock : ` .
2011-09-21 23:16:35 -05:00
*/
2011-09-26 11:50:13 -05:00
@ protocol AFMultipartFormData
2011-09-21 23:16:35 -05:00
/**
Appends HTTP headers , followed by the encoded data and the multipart form boundary .
@ param headers The HTTP headers to be appended to the form data .
@ param body The data to be encoded and appended to the form data .
*/
2011-09-21 14:00:05 -05:00
- ( void ) appendPartWithHeaders : ( NSDictionary * ) headers body : ( NSData * ) body ;
2011-09-21 23:16:35 -05:00
/**
2011-09-26 11:50:13 -05:00
Appends the HTTP headers ` Content - Disposition : form - data ; name = # { name } " `, followed by the encoded data and the multipart form boundary.
2011-09-21 23:16:35 -05:00
@ param data The data to be encoded and appended to the form data .
2011-09-23 10:21:07 -05:00
@ param name The name to be associated with the specified data . This parameter must not be ` nil ` .
2011-09-21 23:16:35 -05:00
*/
2011-09-26 11:50:13 -05:00
- ( void ) appendPartWithFormData : ( NSData * ) data name : ( NSString * ) name ;
/**
Appends the HTTP header ` Content - Disposition : file ; filename = # { generated filename } ; name = # { name } " ` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary.
@ param data The data to be encoded and appended to the form data .
@ 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 name The name to be associated with the specified data . This parameter must not be ` nil ` .
@ discussion The filename associated with this data in the form will be automatically generated using the parameter name specified and a unique timestamp - based hash .
*/
- ( void ) appendPartWithFileData : ( NSData * ) data mimeType : ( NSString * ) mimeType name : ( NSString * ) name ;
2011-09-21 23:16:35 -05:00
/**
2011-09-23 10:21:07 -05:00
Appends the HTTP header ` Content - Disposition : file ; filename = # { filename } " ` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary.
2011-09-21 23:16:35 -05:00
2011-09-23 10:21:07 -05:00
@ 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 ` .
2011-09-26 11:50:13 -05:00
@ param error If an error occurs , upon return contains an ` NSError ` object that describes the problem .
2011-09-21 23:16:35 -05:00
*/
2011-09-26 11:50:13 -05:00
- ( void ) appendPartWithFile : ( NSURL * ) fileURL mimeType : ( NSString * ) mimeType fileName : ( NSString * ) fileName error : ( NSError * * ) error ;
2011-09-24 21:53:46 -04:00
2011-09-21 23:16:35 -05:00
/**
Appends encoded data to the form data .
@ param data The data to be encoded and appended to the form data .
*/
2011-09-21 14:00:05 -05:00
- ( void ) appendData : ( NSData * ) data ;
2011-09-21 23:16:35 -05:00
/**
Appends a string to the form data .
@ param string The string to be encoded and appended to the form data .
*/
2011-09-21 14:00:05 -05:00
- ( void ) appendString : ( NSString * ) string ;
@ end
2011-10-05 12:36:45 -05:00