Created
November 16, 2012 04:52
-
-
Save AlexRezit/4084211 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RAPIUtils.h | |
// RKit | |
// | |
// Created by Alex Rezit on 08/11/2012. | |
// Copyright (c) 2012 Seymour Dev. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface RAPIUtils : NSObject | |
// URL & body | |
+ (NSURL *)urlWithRoot:(NSString *)root subpathFormat:(NSString *)subpathFormat, ...; | |
+ (NSData *)postBodyWithEncodedFormat:(NSString *)format, ...; | |
// Request | |
+ (NSDictionary *)jsonByPostRequestForURL:(NSURL *)url withPostBody:(NSData *)postBody; | |
@end | |
// | |
// RAPIUtils.m | |
// RKit | |
// | |
// Created by Alex Rezit on 08/11/2012. | |
// Copyright (c) 2012 Seymour Dev. All rights reserved. | |
// | |
#import "RAPIUtils.h" | |
@implementation RAPIUtils | |
#pragma mark - URL & body | |
+ (NSURL *)urlWithRoot:(NSURL *)rootURL subpathFormat:(NSString *)subpathFormat, ... | |
{ | |
va_list args; | |
va_start(args, subpathFormat); | |
NSString *subpathString = [[[NSString alloc] initWithFormat:subpathFormat arguments:args] autorelease]; | |
va_end(args); | |
NSURL *url = [NSURL URLWithString:subpathString relativeToURL:rootURL]; | |
return url; | |
} | |
+ (NSData *)postBodyWithEncodedFormat:(NSString *)format, ... | |
{ | |
va_list args; | |
va_start(args, format); | |
NSString *postBodyString = [[[NSString alloc] initWithFormat:format arguments:args] autorelease]; | |
NSData *postBody = [postBodyString dataUsingEncoding:NSUTF8StringEncoding]; | |
return postBody; | |
} | |
#pragma mark - Request | |
+ (NSDictionary *)jsonByPostRequestForURL:(NSURL *)url withPostBody:(NSData *)postBody | |
{ | |
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease]; | |
[request setHTTPMethod:@"POST"]; | |
[request setHTTPBody:postBody]; | |
NSHTTPURLResponse *response; | |
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL]; | |
if (response.statusCode == 200) { | |
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL]; | |
return responseDictionary; | |
} | |
return nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment