Skip to content

Instantly share code, notes, and snippets.

@fisherds
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save fisherds/5dd61c305289a264c883 to your computer and use it in GitHub Desktop.

Select an option

Save fisherds/5dd61c305289a264c883 to your computer and use it in GitHub Desktop.
iOS Client for Endpoints Backend. GradeRecorder Service and Authorizer objects
#pragma mark - Service
+ (BOOL) isLocalHost {
return kLocalHostTesting;
}
+ (GTLServiceGraderecorder*) getService {
if (__graderecorderService == nil) {
__graderecorderService = [[GTLServiceGraderecorder alloc] init];
if (kLocalHostTesting) {
__graderecorderService.rpcURL = [NSURL URLWithString:kLocalHostUrl];
__graderecorderService.fetcherService.allowLocalhostRequest = YES; // NEW!!! (added since video was made)
}
__graderecorderService.retryEnabled = YES;
[GTMHTTPFetcher setLoggingEnabled:YES];
}
return __graderecorderService;
}
#pragma mark - Authorizer
+ (BOOL) hasAuthorizer {
return [RHOAuthUtils _getAuthorizer] != nil; // Very important to use the _getAuthorizer function.
}
// Returns the current authorizer if available, otherwise attempt to return the saved authorizer.
+ (GTMOAuth2Authentication*) _getAuthorizer {
if (__authorizer == nil) {
GTMOAuth2Authentication* savedAuthorizer;
savedAuthorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:kIosClientID
clientSecret:kIosClientSecret];
if (savedAuthorizer.canAuthorize && savedAuthorizer.userEmail != nil) {
NSLog(@"Using a saved authorizer");
[RHOAuthUtils _setAuthorizer:savedAuthorizer];
}
}
__authorizer.authorizationTokenKey = @"id_token";
if (kLocalHostTesting) {
NSLog(@"Authorizing all requests.");
__authorizer.shouldAuthorizeAllRequests = YES;
}
return __authorizer;
}
// Set the authorizer and link the authorizer to the service.
+ (void) _setAuthorizer:(GTMOAuth2Authentication*) authorizer {
if (authorizer != nil) {
NSAssert(authorizer.userEmail != nil, @"Authorizer has no user email");
NSAssert(authorizer.canAuthorize, @"Authorizer can't authorize");
}
__authorizer = authorizer;
[RHOAuthUtils getService].authorizer = [RHOAuthUtils _getAuthorizer];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment