Skip to content

Instantly share code, notes, and snippets.

@ruairif
Last active Aug 29, 2015
Embed
What would you like to do?
#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