Home

Awesome

AFAbstractRESTClient is simple abstract solution to deal with REST webservices using AFNetworking. It basically adds some structure to API calls by separating each type of operation to a class.

In classical approach one can use façade pattern to create a fat client with API methods.

This solution is based on Template method pattern and allows developers to create API calls 'on-the-fly'.

Typical HTTP request consists of a few steps combined into one operation in abstract class which calls only a few methods it needs from subclass. Framework Abstract[GET|POST|DELETE]APICall classes are responsible for:

In most typical situation you don't have to write this whole logic yourself. You only need to override 2 methods: path and parse data to communicate with your API.

Examples for GET and POST requests are included in Tests.

How to get started

If you use cocoapods (you should! :) )

If you don't use cocoapods or just want to test the library alone

Example GET Call

#import "AbstractGETAPICall.h"

@interface GithubGETCall : AbstractGETAPICall

@end
#import "GithubGETCall.h"

@implementation GithubGETCall

- (NSString*) path {
    return @"https://api.github.com/repos/burczyk/AFAbstractRESTClient";
}

- (void) parseResponseData: (id) JSON withSuccessBlock:(void (^)(id JSON)) blockSuccess {
    // You can parse your data here, e.g. write them to Core Data/SQLite or create your own model.
    // After parsing you can return either JSON or parsed object as input for block is (id)
    blockSuccess(JSON);
}

@end
GithubGETCall *call = [[GithubGETCall alloc] init];
[call executeAPICallWithSuccessBlock:^(id responseObject) {
	
} failure:^(NSError *error, id responseObject) {
    
}];

Example POST Call with multipart/form-data

#import "AbstractPOSTAPICall.h"

@interface HttpBinPOSTCall : AbstractPOSTAPICall

@end
#import "HttpBinPOSTCall.h"

@implementation HttpBinPOSTCall

- (NSString*) path {
    return @"post";
}

- (NSDictionary*) queryParams {
    return @{ @"arg1" : @"value1", @"arg2" : @3.141592 };
}

- (void) constructBody:(id <AFMultipartFormData>) formData {
    
    // image source: http://commons.wikimedia.org/wiki/File:Mila_Kunis_2012.jpg
    NSData *image = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Mila_Kunis_2012.jpg/128px-Mila_Kunis_2012.jpg"]];
    
    [formData appendPartWithFormData:[@"example data" dataUsingEncoding:NSUTF8StringEncoding] name:@"key1"];
    [formData appendPartWithFormData:image name:@"Mila_Kunis_2012.jpg"];
}

@end
HttpBinPOSTCall *call = [[HttpBinPOSTCall alloc] init];
[call executeAPICallWithSuccessBlock:^(id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

} progress:^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {

}];

License

AFAbstractRESTClient is available under the MIT license. See the LICENSE file for more info.