Manos Chorianopoulos

add getCosmoteUserAsync request

......@@ -86,6 +86,7 @@
- (void)getPacingDetailsAsync:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)cosmoteSharingAsync:(NSString*)sharingId :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)cosmoteRetrieveSharingAsync:(NSString*)sharingId :(NSNumber*)accept :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCosmoteUserAsync:(NSString*)guid :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
@end
#endif /* MyApi_h */
......
......@@ -1418,4 +1418,17 @@ NSString *VERIFY_URL = @"/partners/cosmote/verify";
}];
}
- (void)getCosmoteUserAsync:(NSString*)guid :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
[[Warply sharedService] getCosmoteUserWithSuccessBlock:guid :^(NSDictionary *response) {
if (success) {
success(response);
}
} failureBlock:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
@end
......
......@@ -352,6 +352,8 @@ WL_VERSION_INTERFACE()
- (void)verifyTicketWithSuccessBlock:(NSString*)guid :(NSString*)ticket :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCosmoteUserWithSuccessBlock:(NSString*)guid :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)addAddressWithSuccessBlock:(NSString*)friendlyName :(NSString*)addressName :(NSString*)addressNumber :(NSString*)postalCode :(NSNumber*)floorNumber :(NSString*)doorbel :(NSString*)region :(NSString*)latitude :(NSString*)longitude :(NSString*)notes :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getAddressWithSuccessBlock:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
......
......@@ -1366,6 +1366,43 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
}];
}
- (void)getCosmoteUserWithSuccessBlock:(NSString*)guid :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
// NSString* appUuid = [NSString alloc];
// appUuid = [WLKeychain getStringForKey:@"NBAPPUuid"];
NSDictionary *postDictionary = @{@"user_identifier" : guid};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:0 error:NULL];
[self sendContextGetCosmoteUser:jsonData successBlock:^(NSDictionary *contextResponse) {
NSDictionary* tokens = [NSDictionary alloc];
tokens = [contextResponse objectForKey:@"result"];
NSString* clientId = [NSString alloc];
clientId = [tokens objectForKey:@"client_id"];
NSString* refreshToken = [NSString alloc];
refreshToken = [tokens objectForKey:@"refresh_token"];
NSString* accessToken = [NSString alloc];
accessToken = [tokens objectForKey:@"access_token"];
NSString* clientSecret = [NSString alloc];
clientSecret = [tokens objectForKey:@"client_secret"];
[_db executeUpdate:@"DROP TABLE requestVariables"];
[_db executeUpdate:@"CREATE TABLE requestVariables (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, client_id INTEGER, client_secret VARCHAR(255), access_token VARCHAR(255), refresh_token VARCHAR(255))"];
WL_FMDBLogError
[_db executeUpdate:@"INSERT INTO requestVariables (id, client_id, client_secret, access_token, refresh_token) VALUES (1, ?, ?, ?, ?)", clientId, clientSecret, accessToken, refreshToken];
NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
if (success) {
success(successResponse);
}
NSLog(@"**************** WARPLY Response *****************" );
NSLog(@"%@", successResponse );
} failureBlock:^(NSError *error) {
if (failure) {
failure(error);
}
NSLog(@"Error at get Cosmote User %@", error );
}];
}
- (void)getCouponsWithSuccessBlock:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
NSDictionary *postDictionary = @{@"coupon":@{@"action": @"get_user_coupons", @"fetch_data": @[@"transaction",@"communication"]}};
......@@ -2617,6 +2654,34 @@ return appIsRegisteredWithWarply;
return appIsRegisteredWithWarply;
}
- (BOOL)sendContextGetCosmoteUser:(NSData*)context
successBlock:(void (^)(NSDictionary *contextResponse))successBlock
failureBlock:(void (^)(NSError *error))failureBlock
{
BOOL appIsRegisteredWithWarply = [self isRegistrationValid];
if (WL_FEATURE_IS_DISABLED_WITH_KEY(WL_WARPLY_ENABLED))
return appIsRegisteredWithWarply;
if (appIsRegisteredWithWarply == NO) {
[_pendingOperationsQueue setSuspended:YES];
[self registration];
}
NSString *path = nil;
NSInvocation *invocation = [self getCosmoteUserRequestInvocationWithType:WLContextRequestTypePost
data:context
path:path
successBlock:[successBlock copy]
failureBlock:[failureBlock copy]];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
[_pendingOperationsQueue addOperation:operation];
return appIsRegisteredWithWarply;
}
#pragma - Events
///////////////////////////////////////////////////////////////////////////////
- (void)addEvent:(WLEvent *)event priority:(BOOL)priority
......@@ -3007,6 +3072,28 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
return invocation;
}
- (NSInvocation *)getCosmoteUserRequestInvocationWithType:(WLContextRequestType)type
data:(NSData *)context
path:(NSString *)path
successBlock:(void (^)(NSDictionary *contextResponse))successBlock
failureBlock:(void (^)(NSError *error))failureBlock
{
void (^ successBlockCopy)(void) = [successBlock copy];
void (^ failureBlockCopy)(void) = [failureBlock copy];
NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(getCosmoteUserRequestWithType:data:path:successBlock:failureBlock:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sgn];
[invocation setTarget:self];
[invocation setSelector:@selector(getCosmoteUserRequestWithType:data:path:successBlock:failureBlock:)];
[invocation setArgument:&type atIndex:2];
[invocation setArgument:&context atIndex:3];
[invocation setArgument:&path atIndex:4];
[invocation setArgument:&successBlockCopy atIndex:5];
[invocation setArgument:&failureBlockCopy atIndex:6];
[invocation retainArguments];
return invocation;
}
- (NSInvocation *)resetPasswordRequestInvocationWithType:(WLContextRequestType)type
data:(NSData *)context
path:(NSString *)path
......@@ -4263,6 +4350,103 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
}
}
- (void)getCosmoteUserRequestWithType:(WLContextRequestType)requestType
data:(NSData*)context
path:(NSString *)path
successBlock:(void (^)(id contextResponse))successBlock
failureBlock:(void (^)(NSError *error))failureBlock
{
//Create REQUEST
NSMutableString *urlString = [NSMutableString stringWithFormat:@"%@/partners/oauth/f83dfde1145e4c2da69793abb2f579af/token", _baseURL]; // /partners/cosmote/verify
WLLOG(@"[WARP Trace] HTTP URL: %@", urlString);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:100];
NSDictionary *parameters;
if (context != nil) {
parameters = [NSJSONSerialization JSONObjectWithData:context options:NSJSONReadingAllowFragments error:NULL];
}
SuccessResponse successResponse = ^(NSURLSessionDataTask * _Nonnull task, id _Nullable JSON) {
if (JSON == nil) {
successBlock(@{@"status": @"0"});
}
else {
// NSData *responseData = [NSJSONSerialization dataWithJSONObject:JSON options:NSJSONWritingPrettyPrinted error:NULL];
// NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// WLLOG(@"[WARP Trace] HTTP %@ Response:%@", request.HTTPMethod, responseString);
WLLOG(@"************ WARP %@ CONTEXT ******************", request.HTTPMethod);
WLLOG(@"[WARP Trace] HTTP Web Id: %@", _webId);
WLLOG(@"[WARP Trace] HTTP API Key: %@", _apiKey);
WLLOG(@"[WARP Trace] HTTP %@ Request URL: %@", request.HTTPMethod, request.URL.absoluteString);
NSString *contextString = [[NSString alloc] initWithData:context encoding:NSUTF8StringEncoding];
WLLOG(@"[WARP Trace] HTTP %@ Request Body: %@", request.HTTPMethod, contextString);
WLLOG(@"*************************************************");
NSDictionary *contextResponse=JSON;
if (successBlock)
{
if ([path length]!=0)
{
successBlock([contextResponse valueForKey:path]);
return;
}
successBlock(contextResponse);
}
}
};
FailureResponse faliureResponse = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
WLLOG(@"************ WARP %@ CONTEXT ******************", request.HTTPMethod);
WLLOG(@"[WARP Trace] HTTP Web Id: %@", _webId);
WLLOG(@"[WARP Trace] HTTP API Key: %@", _apiKey);
WLLOG(@"[WARP Trace] HTTP %@ Request URL: %@", request.HTTPMethod, request.URL.absoluteString);
NSString *contextString = [[NSString alloc] initWithData:context encoding:NSUTF8StringEncoding];
WLLOG(@"[WARP Trace] HTTP %@ Request Body: %@", request.HTTPMethod, contextString);
WLLOG(@"[WARP Trace] Error: %@", [error description]);
WLLOG(@"*************************************************");
if (failureBlock)
failureBlock(error);
};
// NSString *accessToken = [NSString alloc];
// if ([_db tableExists:@"requestVariables"] == YES) {
// FMResultSet *accessTokenSet = [_db executeQuery:@"SELECT access_token FROM requestVariables WHERE id = 1;"];
// while ([accessTokenSet next]) {
// accessToken = [[accessTokenSet resultDictionary][@"access_token"] stringValue];
// }
// }
_httpClient.responseSerializer = [AFJSONResponseSerializer serializer];
_httpClient.requestSerializer = [AFJSONRequestSerializer serializer];
//Set HTTP Headers
time_t timestamp = (time_t) [[NSDate date] timeIntervalSince1970];
[_httpClient.requestSerializer setValue:[@"Basic " stringByAppendingString:@"MWlTM0EyNjcxT2Q0a1B5QkIydEs1ZU5uRENhR0NWQjQ6MjI4MjA4ZTliMTQzNGQ2MmIxNGI3ZDAzYjM2ZjUwMzg="] forHTTPHeaderField:@"Authorization"];
[_httpClient.requestSerializer setValue:_webId forHTTPHeaderField:@"loyalty-web-id"];
[_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"%lu", timestamp] forHTTPHeaderField:@"loyalty-date"];
[_httpClient.requestSerializer setValue:[[NSString stringWithFormat:@"%@%lu", _apiKey, timestamp] SHA256Sum] forHTTPHeaderField:@"loyalty-signature"];
[_httpClient.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[_httpClient.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[_httpClient.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"User-Agent"];
[_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"ios:%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]] forHTTPHeaderField:@"loyalty-bundle-id"];
[_httpClient.requestSerializer setValue:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forHTTPHeaderField:@"unique-device-id"];
[_httpClient.requestSerializer setValue:@"apple" forHTTPHeaderField:@"vendor"];
[_httpClient.requestSerializer setValue:@"platform" forHTTPHeaderField:@"ios"];
[_httpClient.requestSerializer setValue:@"os_version" forHTTPHeaderField:[[UIDevice currentDevice] systemVersion]];
if (requestType == WLContextRequestTypePost) {
// NSDictionary *parameters = [NSJSONSerialization JSONObjectWithData:context options:NSJSONReadingAllowFragments error:NULL];
[_httpClient POST:urlString parameters:parameters progress:nil success:successResponse failure:faliureResponse];
}
else {
[_httpClient GET:urlString parameters:parameters progress:nil success:successResponse failure:faliureResponse];
}
}
///////////////////////////////////////////////////////////////////////////////
- (void)runContextRequestWithType:(WLContextRequestType)requestType
data:(NSData*)context
......
......@@ -2819,4 +2819,41 @@ public class swiftApi {
}
}
public func getCosmoteUserAsync(guid: String, _ getCosmoteUserCallback: @escaping (_ responseData: GenericResponseModel?) -> Void) -> Void {
let instanceOfMyApi = MyApi()
instanceOfMyApi.getCosmoteUserAsync(guid, requestCallback, failureBlock: requestFailureCallback)
func requestCallback(_ responseData: [AnyHashable: Any]?) -> Void {
if let responseDataDictionary = responseData as? [String: AnyObject] {
if (responseDataDictionary["status"] as? Int == 1) {
if let responseDataDictionary = responseData as? [String: Any] {
let tempResponse = GenericResponseModel(dictionary: responseDataDictionary)
getCosmoteUserCallback(tempResponse);
} else {
getCosmoteUserCallback(nil)
}
} else {
getCosmoteUserCallback(nil)
}
} else {
getCosmoteUserCallback(nil)
}
}
func requestFailureCallback(_ error: Error?) -> Void {
print("getCosmoteUser error: ")
print(error)
print("====================")
getCosmoteUserCallback(nil)
}
}
}
......