Mercurio 0.3.0

Mercurio 0.3.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Nov 2016

Maintained by Stefano Zanetti.



 
Depends on:
AFNetworking~> 3.0
Mantle~> 2.0.6
SAMKeychain~> 1.5.1
 

Mercurio 0.3.0

Mercurio is a fast way to make an api with AFNetworking and parse the response with Mantle.

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

Mercurio is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Mercurio', '~> 0.1.3'

Example

Define a response object
@interface MEResponse : MEModel

@property (copy, nonatomic) NSString *accept;
@property (copy, nonatomic) NSString *acceptEncoding;
@property (copy, nonatomic) NSString *acceptLanguage;
@property (copy, nonatomic) NSString *host;
@property (copy, nonatomic) NSString *userAgent;

@end
@implementation MEResponse

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{ selStr(accept) : @"Accept",
              selStr(acceptEncoding) : @"Accept-Encoding",
              selStr(acceptLanguage) : @"Accept-Language",
              selStr(host) : @"Host",
              selStr(userAgent) : @"User-Agent" };
}

@end

GET

You must create an instance of MEApi, set the path, the class response and eventually the root of json response object. That's all!

MEApi *api = [MEApi apiWithMethod:MEApiMethodGET
                             path:@"https://httpbin.org/get"
                    responseClass:[MEResponse class]
                         jsonRoot:@"headers"];

Now you simple have to tell the MESessionManager which API you want execute and nothing else.

[[MESessionManager sharedInstance] sessionDataTaskWithApi:api
                                               completion:^(id responseObject, NSURLSessionDataTask *task, NSError *error) {
                                                   if (!error) {
                                                       NSLog(@"%@", responseObject);
                                                   }
                                               }];
POST Multipart

Like the previous example, you must create an instance of MEMultipartFormApi. MEMultipartFormApi is a MEApi that is conformed to the MEMultipartFormApiProtocol protocol.

MEMultipartFormApi *api = [MEMultipartFormApi apiWithMethod:MEApiMethodPOST
                                                       path:@"http://posttestserver.com/post.php?dir=mercurio"
                                              responseClass:[NSNull class]
                                                   jsonRoot:nil];

[api setMultipartFormConstructingBodyBlock:^(id<AFMultipartFormData> formData) {
       [formData appendPartWithFileData:[NSData data]
                                   name:@""name"
                               fileName:@"file.jpg"
                               mimeType:@"image/jpeg"];
}];

And then the MESessionManager does the rest.

[[MESessionManager sharedInstance] sessionMultipartDataTaskWithApi:api
                                                        completion:^(id responseObject, NSURLSessionDataTask *task, NSError *error) {    
                                                            if (!error) {
                                                                NSLog(@"%@", responseObject);
                                                            }
                                                        }];

MESessionManager always returns a NSURLSessionDataTask so you can cancel the operation at any time.

Author

Stefano Zanetti, [email protected]

License

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