Skip to content
/ Mercurio Public

Mercurio is a fast way to call an api and parse the response

License

Notifications You must be signed in to change notification settings

stefz/Mercurio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mercurio

[![CI Status](http://img.shields.io/travis/Stefano Zanetti/Mercurio.svg?style=flat)](https://travis-ci.org/Stefano Zanetti/Mercurio) Version License Platform

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, stefano.zanetti@pragmamark.org

License

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

About

Mercurio is a fast way to call an api and parse the response

Resources

License

Stars

Watchers

Forks

Packages

No packages published