Skip to content

Instantly share code, notes, and snippets.

@somtd
Created December 7, 2013 09:40
Show Gist options
  • Save somtd/7839116 to your computer and use it in GitHub Desktop.
Save somtd/7839116 to your computer and use it in GitHub Desktop.
API Client for Parse REST API using AFNetworking #BLOG
#import "ParseApiClient.h"
static NSString * const kSDFParseAPIBaseURLString = @"https://api.parse.com/1/";
static NSString * const kSDFParseAPIApplicationId = YOUR_APPLICATION_ID;
static NSString * const kSDFParseAPIKey = YOUR_PARSE_API_KEY;
@implementation ParseApiClient
+ (ParseApiClient *)sharedClient {
static ParseApiClient *sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedClient = [[ParseApiClient alloc] initWithBaseURL:[NSURL URLWithString:kSDFParseAPIBaseURLString]];
});
return sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setParameterEncoding:AFJSONParameterEncoding];
[self setDefaultHeader:@"X-Parse-Application-Id" value:kSDFParseAPIApplicationId];
[self setDefaultHeader:@"X-Parse-REST-API-Key" value:kSDFParseAPIKey];
[self setDefaultHeader:@"Accept" value:@"application/json,text/html"];
}
return self;
}
- (NSMutableURLRequest *)GETRequestForClass:(NSString *)className parameters:(NSDictionary *)parameters {
NSMutableURLRequest *request = nil;
request = [self requestWithMethod:@"GET" path:[NSString stringWithFormat:@"classes/%@", className] parameters:parameters];
return request;
}
- (NSMutableURLRequest *)GETRequestForAllRecordsOfClass:(NSString *)className updatedAfterDate:(NSDate *)updatedDate {
NSMutableURLRequest *request = nil;
NSDictionary *parameters = nil;
if (updatedDate) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.'999Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *jsonString = [NSString
stringWithFormat:@"{\"updatedAt\":{\"$gte\":{\"__type\":\"Date\",\"iso\":\"%@\"}}}",
[dateFormatter stringFromDate:updatedDate]];
parameters = [NSDictionary dictionaryWithObject:jsonString forKey:@"where"];
}
request = [self GETRequestForClass:className parameters:parameters];
return request;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment