L7SHTTPClient Class Reference
Inherits from | L7SAFHTTPClient |
Declared in | L7SHTTPClient.h |
Overview
L7SHTTPClient
extends a class of AFNetworking, AFHTTPClient
, that captures the common patterns of communicating with an web application over HTTP. It encapsulates information like base URL, authorization credentials, and HTTP headers, and uses them to construct and manage the execution of HTTP request operations.
Automatic Content Parsing
Instances of L7SHTTPClient
may specify which types of requests it expects and should handle by registering HTTP operation classes for automatic parsing. Registered classes will determine whether they can handle a particular request.
You can override these HTTP headers or define new ones using setDefaultHeader:value:
.
Note: In the case that a request URL doesn’t end with “.json”, one needs to add “Accept” header as the example below to indicate the L7SAFHTTPClient
to parse a response in json format.
[httpClient registerHTTPOperationClass:[L7SAFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
You may construct a URL request operation accordingly in enqueueHTTPRequestOperationWithRequest:success:failure
. L7SHTTPClient
overwrites the method of constructing an operation with a particular URL request to a protected resources:
- (L7SAFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSMutableURLRequest *)urlRequest
success:(void (^)(L7SAFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(L7SAFHTTPRequestOperation *operation, NSError *error))failure
Example to Access Protected Resources
L7SHTTPClient *httpClient = [[ L7SHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://www.example.com"]];
[httpClient registerHTTPOperationClass:[L7SAFJSONRequestOperation class]];
/set headers to specify the acceptable format
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
NSMutableDictionary * parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:@"listProducts" forKey:@"operation"];
[httpClient getPath:@"/protected/resource/products"
parameters:parameters
success:^(L7SAFHTTPRequestOperation *operation, id responseObject){
//add your code to handle results from the server
} failure:^(L7SAFHTTPRequestOperation *operation, NSError *error) {
//add your code to handle the failures
}];
Make an Unprotected HTTP request
L7SHTTPClient
is used only if you intend to make a request to a protected endpoint. You may use its superclass L7SAFHTTPClient
directly to make a request to a unprotected endpoint. Using L7SHTTPClient
to send a request to a non-protected will result in leaking your access credetials.
Status Notifications
SDK sends notifcations when some key satuse changes through the iOS NSNotificationCenter.
Crafting an HTTP operation with any given parameters and headers
The following is an example of crafing an HTTP operation with any given parameters and headers:
L7SHTTPClient *httpClient = [[ L7SHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:policyManager.apiEndpoint]];
NSString *authorization = @"some authorization";
[httpClient setDefaultHeader:@"Authorization" value:authorization];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
@"password", @"grant_type",
login, @"username",
password, @"password",
nil];
NSMutableURLRequest *mutableRequest = [httpClient requestWithMethod:@"POST" path:somePath parameters:parameters];
NSMutableDictionary * headers = [[NSMutableDictionary alloc] init];
[headers setObject:@"application/json" forKey:@"Accept"];
[mutableRequest setAllHTTPHeaderFields:headers];
L7SAFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:mutableRequest
success:^(L7SAFHTTPRequestOperation *operation, id responseObject)
{
//handle success
} failure:^(L7SAFHTTPRequestOperation *operation, NSError *error) {
//handle failure
}];
[httpClient enqueueHTTPRequestOperation:operation];
Tasks
Creating HTTP Operations
-
– HTTPRequestOperationWithRequest:success:failure:
Creates an
L7SAFHTTPRequestOperation
.
Making HTTP Requests
-
– getPath:parameters:success:failure:
Creates an
L7SAFHTTPRequestOperation
with aGET
request, and enqueues it to the HTTP client’s operation queue. -
– postPath:parameters:success:failure:
Creates an
L7SAFHTTPRequestOperation
with aPOST
request, and enqueues it to the HTTP client’s operation queue. -
– putPath:parameters:success:failure:
Creates an
L7SAFHTTPRequestOperation
with aPUT
request, and enqueues it to the HTTP client’s operation queue. -
– deletePath:parameters:success:failure:
Creates an
L7SAFHTTPRequestOperation
with aDELETE
request, and enqueues it to the HTTP client’s operation queue.
Instance Methods
HTTPRequestOperationWithRequest:success:failure:
Creates an L7SAFHTTPRequestOperation
.
- (L7SAFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSMutableURLRequest *)urlRequest success:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , NSError *error ))failure
Parameters
- urlRequest
The request object to be loaded asynchronously during execution of the operation.
- success
A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request.
- failure
A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments:, the created request operation and the
NSError
object describing the network or parsing error that occurred.
Discussion
Creates an L7SAFHTTPRequestOperation
.
Declared In
L7SHTTPClient.h
deletePath:parameters:success:failure:
Creates an L7SAFHTTPRequestOperation
with a DELETE
request, and enqueues it to the HTTP client’s operation queue.
- (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters success:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , NSError *error ))failure
Parameters
- path
The path to be appended to the HTTP client’s base URL and used as the request URL.
- parameters
The parameters to be encoded and appended as the query string for the request URL.
- success
A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request.
- failure
A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the
NSError
object describing the network or parsing error that occurred.
Discussion
Creates an L7SAFHTTPRequestOperation
with a DELETE
request, and enqueues it to the HTTP client’s operation queue.
Declared In
L7SHTTPClient.h
getPath:parameters:success:failure:
Creates an L7SAFHTTPRequestOperation
with a GET
request, and enqueues it to the HTTP client’s operation queue.
- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , NSError *error ))failure
Parameters
- path
The path to be appended to the HTTP client’s base URL and used as the request URL.
- parameters
The parameters to be encoded and appended as the query string for the request URL.
- success
A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request.
- failure
A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the
NSError
object describing the network or parsing error that occurred.
Discussion
Creates an L7SAFHTTPRequestOperation
with a GET
request, and enqueues it to the HTTP client’s operation queue.
Declared In
L7SHTTPClient.h
postPath:parameters:success:failure:
Creates an L7SAFHTTPRequestOperation
with a POST
request, and enqueues it to the HTTP client’s operation queue.
- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , NSError *error ))failure
Parameters
- path
The path to be appended to the HTTP client’s base URL and used as the request URL.
- parameters
The parameters to be encoded and set in the request HTTP body.
- success
A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request.
- failure
A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the
NSError
object describing the network or parsing error that occurred.
Discussion
Creates an L7SAFHTTPRequestOperation
with a POST
request, and enqueues it to the HTTP client’s operation queue.
Declared In
L7SHTTPClient.h
putPath:parameters:success:failure:
Creates an L7SAFHTTPRequestOperation
with a PUT
request, and enqueues it to the HTTP client’s operation queue.
- (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( L7SAFHTTPRequestOperation *operation , NSError *error ))failure
Parameters
- path
The path to be appended to the HTTP client’s base URL and used as the request URL.
- parameters
The parameters to be encoded and set in the request HTTP body.
- success
A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request.
- failure
A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the
NSError
object describing the network or parsing error that occurred.
Discussion
Creates an L7SAFHTTPRequestOperation
with a PUT
request, and enqueues it to the HTTP client’s operation queue.
Declared In
L7SHTTPClient.h