Skip to content

Instantly share code, notes, and snippets.

@ruairif
Last active August 29, 2015 14:05
Show Gist options
  • Save ruairif/fecb63997d0dc0c8e60f to your computer and use it in GitHub Desktop.
Save ruairif/fecb63997d0dc0c8e60f to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#import "opcUtils.h"
@interface opcAPIRequests : NSObject
+ (NSMutableURLRequest*) sendRequestTo:(NSString*)relativeURL withMethod:(RKRequestMethod)reqMethod withUID:(NSString*)uid withKey:(NSString*)key;
+ (NSMutableURLRequest*) sendRequestTo:(NSString*)relativeURL withParams:(NSDictionary*)params withMethod:(RKRequestMethod)reqMethod withUID:(NSString*)uid withKey:(NSString*)key;
+ (NSMutableURLRequest*) sendRequestTo:(NSString*)relativeURL withParams:(NSDictionary*)params withMethod:(RKRequestMethod)reqMethod ;
@end
#import "opcAPIRequests.h"
// uid refers to the uid that is returned with post to https://app.onpeagecrm.com/api/v3/login.json
// key refers to the base64 decoded key returned with the login request.
@implementation opcAPIRequests
+ (NSMutableURLRequest*) sendRequestTo:(NSString*)relativeURL withParams:(NSDictionary*)params withMethod:(RKRequestMethod)reqMethod {
RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSMutableURLRequest *request = [objectManager requestWithObject:nil method:reqMethod
path:relativeURL
parameters:params];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
return request;
}
+ (NSMutableURLRequest*) sendRequestTo:(NSString*)relativeURL withMethod:(RKRequestMethod)reqMethod withUID:(NSString*)uid withKey:(NSString*)key{
RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSMutableURLRequest *request = [objectManager requestWithObject:nil method:reqMethod
path:relativeURL
parameters:nil];
NSInteger timestamp = [[NSDate date] timeIntervalSince1970];
[request addValue:uid forHTTPHeaderField:@"X-OnePageCRM-UID"];
[request addValue:[NSString stringWithFormat:@"%d", timestamp ] forHTTPHeaderField:@"X-OnePageCRM-TS"];
NSString* authSignature = [opcUtils returnAuthWithKey:key withUID:uid withTime:[NSString stringWithFormat:@"%d", timestamp ] withMethod:@"GET" withURL:[NSString stringWithFormat:@"%@%@",objectManager.baseURL,relativeURL]];
[request addValue:authSignature forHTTPHeaderField:@"X-OnePageCRM-Auth"];
return request;
}
+ (NSMutableURLRequest*) sendRequestTo:(NSString*)relativeURL withParams:(NSDictionary *)params withMethod:(RKRequestMethod)reqMethod withUID:(NSString*)uid withKey:(NSString*)key{
RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSMutableURLRequest *request = [objectManager requestWithObject:nil method:reqMethod
path:relativeURL
parameters:params];
NSInteger timestamp = [[NSDate date] timeIntervalSince1970];
[request addValue:uid forHTTPHeaderField:@"X-OnePageCRM-UID"];
[request addValue:[NSString stringWithFormat:@"%d", timestamp ] forHTTPHeaderField:@"X-OnePageCRM-TS"];
NSString* authSignature = [opcUtils returnAuthWithKey:key withUID:uid withTime:[NSString stringWithFormat:@"%d", timestamp ] withMethod:@"GET" withURL:[NSString stringWithFormat:@"%@%@",objectManager.baseURL,relativeURL]];
[request addValue:authSignature forHTTPHeaderField:@"X-OnePageCRM-Auth"];
return request;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment