LupinusHTTP is an HTTP networking library, wrapping NSURLSession
.
- Small, Simple
- Block-based response methods.
LupinusHTTP is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "LupinusHTTP"
LupinusHTTP has very simple step to request.
- Create
LupinusHTTPRequest
throughLupinusHTTP
's method.- HTTP Method and URL and Parameters and (post body).
- Receive response using
responseJSON
orresponseString
methods.
Example: Get Request to http://httpbin.org/get?key=value
LupinusHTTPRequest *httpRequest = [LupinusHTTP request:LupinusMethodGET URL:@"http://httpbin.org/get" query:@{
@"key" : @"value"
}];
[httpRequest responseJSON:^(NSURLRequest *request, NSURLResponse *response, id JSON, NSError *error) {
NSLog(@"JSON = %@", JSON);
}];
Always response*
complete method involked in matin thread.
LupinusHTTPRequest *httpRequest = [LupinusHTTP request:LupinusMethodGET URL:@"http://httpbin.org/get"];
[httpRequest responseJSON:^(NSURLRequest *request, NSURLResponse *response, id JSON, NSError *error) {
NSLog(@"JSON = %@", JSON);// => JSON Object(NSDictionary or NSArray)
}];
LupinusHTTPRequest *httpRequest = [LupinusHTTP request:LupinusMethodGET URL:@"http://httpbin.org/get"];
[httpRequest responseString:^(NSURLRequest *request, NSURLResponse *response, NSString *string, NSError *error) {
NSLog(@"string = %@", string);// => NSString
}];
LupinusHTTPRequest *httpRequest = [LupinusHTTP request:LupinusMethodGET URL:@"http://httpbin.org/get"];
[httpRequest responseRawData:^(NSURLRequest *request, NSURLResponse *response, NSData *data, NSError *error) {
// data
}];
e.g) http://httpbin.org/post?key=value
body
[1,2,3]
[LupinusHTTP request:LupinusMethodPOST URL:@"http://httpbin.org/post" query:@{
@"key" : @"value"
} body:@[@1, @2, @3]];
[httpRequest responseJSON:^(NSURLRequest *request, NSURLResponse *response, id JSON, NSError *error) {
NSLog(@"JSON = %@", JSON);// => JSON Object(NSDictionary or NSArray)
}];
When response.statusCode >= 400, recognize request as failed and error
is filled by status code of.
LupinusHTTPRequest *httpRequest = [LupinusHTTP request:LupinusMethodGET URL:@"http://httpbin.org/status/403"];
// response status code is 403
[httpRequest responseJSON:^(NSURLRequest *request, NSURLResponse *response, id JSON, NSError *error) {
// error is not nil
if(error){
NSLog(@"%@", error);
}
}];
You can create a new session with a modified session configuration.
// default : [NSURLSessionConfiguration defaultSessionConfiguration]
+ (instancetype)httpWithSessionConfiguration:(NSURLSessionConfiguration *) sessionConfiguration;
You can cancel the request by LupinusHTTPRequest#cancel
.
LupinusHTTPRequest *httpRequest = [LupinusHTTP request:LupinusMethodGET URL:@"http://httpbin.org/get"];
[httpRequest responseJSON:^(NSURLRequest *request, NSURLResponse *response, id JSON, NSError *error) {
// this callback doens't call!
}];
// cancel request
[httpRequest cancel];
Request flow design of LupinusHTTP.
// Create NSURLSession and NSURLRequest.
LupinusHTTPRequest *httpRequest = [LupinusHTTP request:LupinusMethodGET URL:@"http://httpbin.org/get"];
// already started HTTP request
// ...
// you can register complete handler
// Lupinus add this handler to `queue`
[httpRequest responseJSON:^(NSURLRequest *request, NSURLResponse *response, id JSON, NSError *error) {
NSLog(@"JSON = %@", JSON);// => JSON Object(NSDictionary or NSArray)
}];
// ....
// Get HTTP response.
// Lupinus dispatch_resume(self.queue); => callback the complete handlers.
No.
LupinusHTTP doens't work with Background Fetch.
- Multitasking in iOS 7 - iOS 7 - objc.io issue #5
- Background Fetch require using
NSURLSessionDownloadTask
.
But, welcome to your pull request!
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
LupinusHTTP is available under the MIT license. See the LICENSE file for more info.
LupinusHTTP inspired by Alamofire, AFNetworking and TacoShell.
Photo by Tatu Väyrynen