Created
June 24, 2016 14:03
-
-
Save SiarheiFedartsou/5b7a6e5f901b0e7372fe9b1b47fbdd6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)sendRequest:(id<GAApiRequest>)request success:(void (^)(AFHTTPRequestOperation *, id<GAApiResponse>))success | |
failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure | |
{ | |
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { | |
GALog(@"Processing in background request : %@, time in GBG left %f", request.apiPath, [UIApplication sharedApplication].backgroundTimeRemaining); | |
} | |
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; | |
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary]; | |
[cookieProperties setObject:@"PHPSESSID" forKey:NSHTTPCookieName]; | |
[cookieProperties setObject:@"someValue123456" forKey:NSHTTPCookieValue]; | |
[cookieProperties setObject:@"www.mobitee.com" forKey:NSHTTPCookieDomain]; | |
[cookieProperties setObject:@"www.mobitee.com" forKey:NSHTTPCookieOriginURL]; | |
// [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath]; | |
// [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion]; | |
// set expiration to one month from now or any NSDate of your choosing | |
// this makes the cookie sessionless and it will persist across web sessions and app launches | |
/// if you want the cookie to be destroyed when your app exits, don't set this | |
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires]; | |
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties]; | |
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; | |
@weakify(self); | |
AFHTTPRequestOperation* operation = [self postPath:[self apiPath:request.apiPath] | |
parameters:[request serialize] | |
success:^(AFHTTPRequestOperation* requestOperation, NSDictionary * responseObject) { | |
@strongify(self); | |
Class responseClass = [request responseClass]; | |
id<GAApiResponse> response = [[responseClass alloc] initWithDictionary:responseObject]; | |
// NSMutableArray* arr = [[NSMutableArray alloc] init]; | |
// for (int i = 0; i < 1000; ++i) { | |
// [arr addObject:[self task]]; | |
// } | |
// | |
// [[BFTask taskForCompletionOfAllTasks:arr] continueWithBlock:^id(BFTask *task) { | |
// NSLog(@"task %@ %@ %@", task, task.error, task.result); | |
// return nil; | |
// }]; | |
if ([response isFailed] && response.errorCode == GAApiResponseErrorCodeUserNotConnected) { | |
[self resendRequestAfterSessionRefresh:request success:success failure:failure]; | |
} else if (success) { // do we need call success block for other types of server errors? | |
success(requestOperation, response); | |
} | |
} failure:failure]; | |
operation.responseSerializer = [[request responseClass] responseSerializer]; | |
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ | |
GALog(@"%s, Time in BG expired", __PRETTY_FUNCTION__); | |
}]; | |
} | |
- (BFTask*) task | |
{ | |
BFTaskCompletionSource* source = [BFTaskCompletionSource taskCompletionSource]; | |
NSString *currentUserId = [GAStorage instance].accountID; | |
[self refreshSessionForUserId:currentUserId success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) { | |
GALog(@"response: %@", responseObject); | |
GASetUserLoggedIn(YES); | |
if (responseObject && [[responseObject objectForKey:@"status"] isEqualToString:@"success"]) { | |
[GAPIQSyncStorage instance].sessionID = [responseObject valueForKeyPath:@"data.session_id"]; | |
[source setResult:[responseObject valueForKeyPath:@"data.session_id"]]; | |
} else { | |
[source setError:[NSError ga_errorWithDescription:@"tratata"]]; | |
} | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
GALogNetwork(@"[FAILURE] refreshSessionForUserId: %@", currentUserId); | |
[source setError:[NSError ga_errorWithDescription:@"tratata2"]]; | |
// set user not connected | |
GASetUserLoggedIn(NO); | |
}]; | |
return source.task; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment