Commit 2dc5d60e authored by Bosko Petreski's avatar Bosko Petreski Committed by Bosko Petreski

improvements

parent 7e7f6f12
......@@ -57,6 +57,7 @@
// Create Primed instance
// [Primed.sharedInstance initWithPublicKey:@"mypubkey" secretKey:@"mysecretkey" connectionString:@"https://gw.staging.primed.io:443"];
// Primed.sharedInstance.debug = YES;
// Create PrimedTracker instance
[PrimedTracker.sharedInstance initWithPublicKey:@"mypubkey" secretKey:@"mysecretkey" connectionString:@"https://gw.staging.primed.io:443" trackingConnectionString:@"http://18.191.69.104:5001/v1" heartbeatInterval:10 eventQueueRetryInterval:10];
......
......@@ -20,6 +20,7 @@ typedef void(^APIFailed)(NSString *message);
@property (nonatomic,strong) NSString *public_key;
@property (nonatomic,strong) NSString *nonce;
@property (nonatomic,strong) NSString *connectionStringPrimedIO;
@property (nonatomic,assign) BOOL primedTrackerAvailable;
/**
* Create instance of Primed class
......
......@@ -8,20 +8,21 @@
#import "Primed.h"
#import <CommonCrypto/CommonCrypto.h>
#import "PrimedTracker.h"
@implementation Primed
#pragma mark - HTTP Methods
-(NSMutableURLRequest *)requestWithURL:(NSString *)urlLink method:(NSString *)method{
NSString *strUrl = [NSString stringWithFormat:@"%@%@",self.connectionStringPrimedIO,urlLink];
NSString *strUrl = [NSString stringWithFormat:@"%@%@",Primed.sharedInstance.connectionStringPrimedIO,urlLink];
NSURL *URL = [NSURL URLWithString:strUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:self.public_key forHTTPHeaderField:@"X-Authorization-Key"];
[request addValue:self.sha512Signature forHTTPHeaderField:@"X-Authorization-Signature"];
[request addValue:self.nonce forHTTPHeaderField:@"X-Authorization-Nonce"];
[request addValue:Primed.sharedInstance.public_key forHTTPHeaderField:@"X-Authorization-Key"];
[request addValue:Primed.sharedInstance.sha512Signature forHTTPHeaderField:@"X-Authorization-Signature"];
[request addValue:Primed.sharedInstance.nonce forHTTPHeaderField:@"X-Authorization-Nonce"];
[request setHTTPMethod:method];
return request;
......@@ -31,17 +32,17 @@
if([[NSThread currentThread] isMainThread]){
if(error){
if(self.debug){ PrimedLog(@"[ERROR] %@: %@",request.URL,error.userInfo); }
if(Primed.sharedInstance.debug){ PrimedLog(@"[ERROR] %@: %@",request.URL,error.userInfo); }
failed(error.userInfo[@"NSLocalizedDescription"]);
}
else{
NSDictionary *dictReturn = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if(dictReturn){
if(self.debug){ PrimedLog(@"[SUCCESS] %@: %@",request.URL,dictReturn); }
if(Primed.sharedInstance.debug){ PrimedLog(@"[SUCCESS] %@: %@",request.URL,dictReturn); }
success(dictReturn);
}
else{
if(self.debug){ PrimedLog(@"[FAILED] %@ Error: %@",request.URL,[NSString.alloc initWithData:data encoding:NSUTF8StringEncoding]); }
if(Primed.sharedInstance.debug){ PrimedLog(@"[FAILED] %@ Error: %@",request.URL,[NSString.alloc initWithData:data encoding:NSUTF8StringEncoding]); }
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
failed([NSString stringWithFormat:@"Error CODE: %d",(int)httpResponse.statusCode]);
}
......@@ -50,17 +51,17 @@
else {
dispatch_async(dispatch_get_main_queue(), ^{
if(error){
if(self.debug){ PrimedLog(@"[ERROR] %@: %@",request.URL,error.userInfo); }
if(Primed.sharedInstance.debug){ PrimedLog(@"[ERROR] %@: %@",request.URL,error.userInfo); }
failed(error.userInfo[@"NSLocalizedDescription"]);
}
else{
NSDictionary *dictReturn = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if(dictReturn){
if(self.debug){ PrimedLog(@"[SUCCESS] %@: %@",request.URL,dictReturn); }
if(Primed.sharedInstance.debug){ PrimedLog(@"[SUCCESS] %@: %@",request.URL,dictReturn); }
success(dictReturn);
}
else{
if(self.debug){ PrimedLog(@"[FAILED] %@ Error: %@",request.URL,[NSString.alloc initWithData:data encoding:NSUTF8StringEncoding]); }
if(Primed.sharedInstance.debug){ PrimedLog(@"[FAILED] %@ Error: %@",request.URL,[NSString.alloc initWithData:data encoding:NSUTF8StringEncoding]); }
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
failed([NSString stringWithFormat:@"Error CODE: %d",(int)httpResponse.statusCode]);
}
......@@ -114,19 +115,19 @@
-(id)init{
self = [super init];
if(self) {
}
return self;
}
-(void)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString{
if(self.debug){
if(Primed.sharedInstance.debug){
PrimedLog(@"Primed Debug Activated");
}
self.public_key = publicKey;
self.nonce = [NSString stringWithFormat:@"%.f",NSDate.date.timeIntervalSince1970];
Primed.sharedInstance.public_key = publicKey;
Primed.sharedInstance.nonce = [NSString stringWithFormat:@"%.f",NSDate.date.timeIntervalSince1970];
NSString *prepSignature = [NSString stringWithFormat:@"%@%@%@",publicKey,secretKey,self.nonce];
self.sha512Signature = [Primed hmac:prepSignature];
self.connectionStringPrimedIO = connectionString;
Primed.sharedInstance.sha512Signature = [Primed hmac:prepSignature];
Primed.sharedInstance.connectionStringPrimedIO = connectionString;
}
#pragma mark - API Main Functions
......@@ -148,6 +149,11 @@
[generateURL appendFormat:@"&signals=%@",[signalToString stringByAddingPercentEncodingWithAllowedCharacters:setEncoding]];
[self get:generateURL success:^(NSDictionary *response) {
if(Primed.sharedInstance.primedTrackerAvailable){
[PrimedTracker.sharedInstance trackPersonalise:response];
}
success(response);
} failed:^(NSString *message) {
failed(message);
......
......@@ -75,9 +75,15 @@ typedef NS_ENUM(NSUInteger, ScrollDirection) {
-(void)trackExitViewPort:(NSString *)campaign elements:(NSArray *)elements;
/**
* Create instance of PrimedTracker class
* Track custom event
* @param data Custom JSON Data for event
*/
-(void)trackCustomEvent:(NSDictionary *)data;
/**
* Track personalise JSON respond from REST
* @param data Personalise JSON Data for event
*/
-(void)trackPersonalise:(NSDictionary *)data;
@end
......@@ -46,9 +46,6 @@
return NO;
}
}
#warning CHECK THIS
-(NSDictionary *)basicEventProperties:(NSString *)type eventObject:(NSDictionary *)eventObject{
NSMutableDictionary *payload = [NSMutableDictionary dictionaryWithDictionary:@{@"apikey":Primed.sharedInstance.public_key,
......@@ -101,26 +98,20 @@
if(PrimedTracker.sharedInstance.debug){
PrimedLog(@"Primed Tracker Debug Activated");
}
if(!Primed.sharedInstance.public_key){
Primed.sharedInstance.public_key = publicKey;
}
if(!Primed.sharedInstance.nonce){
Primed.sharedInstance.nonce = [NSString stringWithFormat:@"%.f",NSDate.date.timeIntervalSince1970];
}
if(!Primed.sharedInstance.sha512Signature){
NSString *prepSignature = [NSString stringWithFormat:@"%@%@%@",publicKey,secretKey,self.nonce];
Primed.sharedInstance.sha512Signature = [Primed hmac:prepSignature];
}
if(!Primed.sharedInstance.connectionStringPrimedIO){
Primed.sharedInstance.connectionStringPrimedIO = connectionString;
}
Primed.sharedInstance.public_key = publicKey;
Primed.sharedInstance.nonce = [NSString stringWithFormat:@"%.f",NSDate.date.timeIntervalSince1970];
NSString *prepSignature = [NSString stringWithFormat:@"%@%@%@",Primed.sharedInstance.public_key,secretKey,Primed.sharedInstance.nonce];
Primed.sharedInstance.sha512Signature = [Primed hmac:prepSignature];
Primed.sharedInstance.connectionStringPrimedIO = connectionString;
Primed.sharedInstance.primedTrackerAvailable = YES;
self.heartbeatInterval = heartbeatInterval;
self.eventQueueRetryInterval = eventQueueRetryInterval;
self.trackingConnectionString = trackingConnectionString;
NSURL *url = [NSURL URLWithString:self.trackingConnectionString];
self.manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @(Primed.sharedInstance.debug)}];
self.manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @(PrimedTracker.sharedInstance.debug)}];
self.socket = [[SocketIOClient alloc] initWithManager:self.manager nsp:@"/v1"];
[self.manager setNsps:@{@"/v1":self.socket}];
......@@ -209,6 +200,9 @@
NSDictionary *payload = [self basicEventProperties:@"custom" eventObject:data];
[self emitPayload:payload];
}
//personalise SEND JSON RESPOND FROM API
-(void)trackPersonalise:(NSDictionary *)data{
NSDictionary *payload = [self basicEventProperties:@"personalise" eventObject:data];
[self emitPayload:payload];
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment