MakeRPCViewController.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #import "MakeRPCViewController.h"
  19. #import <AuthTestService/AuthSample.pbrpc.h>
  20. #import <Google/SignIn.h>
  21. #import <ProtoRPC/ProtoRPC.h>
  22. NSString * const kTestScope = @"https://www.googleapis.com/auth/xapi.zoo";
  23. static NSString * const kTestHostAddress = @"grpc-test.sandbox.googleapis.com";
  24. // Category for RPC errors to create the descriptions as we want them to appear on our view.
  25. @interface NSError (AuthSample)
  26. - (NSString *)UIDescription;
  27. @end
  28. @implementation NSError (AuthSample)
  29. - (NSString *)UIDescription {
  30. if (self.code == GRPCErrorCodeUnauthenticated) {
  31. // Authentication error. OAuth2 specifies we'll receive a challenge header.
  32. // |userInfo[kGRPCHeadersKey]| is the dictionary of response headers.
  33. NSString *challengeHeader = self.userInfo[kGRPCHeadersKey][@"www-authenticate"] ?: @"";
  34. return [@"Invalid credentials. Server challenge:\n" stringByAppendingString:challengeHeader];
  35. } else {
  36. // Any other error.
  37. return [NSString stringWithFormat:@"Unexpected RPC error %li: %@",
  38. (long)self.code, self.localizedDescription];
  39. }
  40. }
  41. @end
  42. @interface MakeRPCViewController ()<GRPCProtoResponseHandler>
  43. @end
  44. @implementation MakeRPCViewController
  45. - (dispatch_queue_t)dispatchQueue {
  46. return dispatch_get_main_queue();
  47. }
  48. - (void)viewWillAppear:(BOOL)animated {
  49. // Create a service client and a proto request as usual.
  50. AUTHTestService *client = [[AUTHTestService alloc] initWithHost:kTestHostAddress];
  51. AUTHRequest *request = [AUTHRequest message];
  52. request.fillUsername = YES;
  53. request.fillOauthScope = YES;
  54. // Set the request header with call options
  55. GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
  56. options.oauth2AccessToken = GIDSignIn.sharedInstance.currentUser.authentication.accessToken;
  57. GRPCUnaryProtoCall *call = [client unaryCallWithMessage:request
  58. responseHandler:self
  59. callOptions:options];
  60. [call start];
  61. self.mainLabel.text = @"Waiting for RPC to complete...";
  62. }
  63. - (void)didReceiveProtoMessage:(GPBMessage *)message {
  64. AUTHResponse *response = (AUTHResponse *)message;
  65. if (response) {
  66. // This test server responds with the email and scope of the access token it receives.
  67. self.mainLabel.text = [NSString stringWithFormat:@"Used scope: %@ on behalf of user %@",
  68. response.oauthScope, response.username];
  69. }
  70. }
  71. - (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
  72. if (error) {
  73. self.mainLabel.text = error.UIDescription;
  74. }
  75. }
  76. @end