From 92db5fc72488f9d62b81ee311a79832df787f3ef Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Fri, 7 Dec 2018 10:29:24 -0800 Subject: [PATCH] Rename getTokenWithHandler --- src/objective-c/GRPCClient/GRPCCall.m | 14 ++++++++++++-- src/objective-c/GRPCClient/GRPCCallOptions.h | 13 ++++++++++++- src/objective-c/ProtoRPC/ProtoRPC.m | 2 +- src/objective-c/tests/APIv2Tests/APIv2Tests.m | 2 +- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m index 589b52031a7..18f79311a69 100644 --- a/src/objective-c/GRPCClient/GRPCCall.m +++ b/src/objective-c/GRPCClient/GRPCCall.m @@ -885,7 +885,7 @@ const char *kCFStreamVarName = "grpc_cfstream"; @synchronized(self) { self.isWaitingForToken = YES; } - [_callOptions.authTokenProvider getTokenWithHandler:^(NSString *token) { + void (^tokenHandler)(NSString *token) = ^(NSString *token) { @synchronized(self) { if (self.isWaitingForToken) { if (token) { @@ -895,7 +895,17 @@ const char *kCFStreamVarName = "grpc_cfstream"; self.isWaitingForToken = NO; } } - }]; + }; + id authTokenProvider = _callOptions.authTokenProvider; + if ([authTokenProvider respondsToSelector:@selector(provideTokenToHandler:)]) { + [_callOptions.authTokenProvider provideTokenToHandler:tokenHandler]; + } else { + NSAssert([authTokenProvider respondsToSelector:@selector(getTokenWithHandler:)], + @"authTokenProvider has no usable method"); + if ([authTokenProvider respondsToSelector:@selector(getTokenWithHandler:)]) { + [_callOptions.authTokenProvider getTokenWithHandler:tokenHandler]; + } + } } else { [self startCallWithWriteable:writeable]; } diff --git a/src/objective-c/GRPCClient/GRPCCallOptions.h b/src/objective-c/GRPCClient/GRPCCallOptions.h index 85786c74174..b7f08480dc0 100644 --- a/src/objective-c/GRPCClient/GRPCCallOptions.h +++ b/src/objective-c/GRPCClient/GRPCCallOptions.h @@ -58,13 +58,24 @@ typedef NS_ENUM(NSUInteger, GRPCTransportType) { /** * Implement this protocol to provide a token to gRPC when a call is initiated. */ -@protocol GRPCAuthorizationProtocol +@protocol GRPCAuthorizationProtocol + +@optional + +/** + * This method is called when gRPC is about to start the call. When OAuth token is acquired, + * \a handler is expected to be called with \a token being the new token to be used for this call. + */ +- (void)provideTokenToHandler:(void (^_Nullable)(NSString *_Nullable token))handler; /** + * This method is deprecated. Please use \a provideTokenToHandler. + * * This method is called when gRPC is about to start the call. When OAuth token is acquired, * \a handler is expected to be called with \a token being the new token to be used for this call. */ - (void)getTokenWithHandler:(void (^_Nullable)(NSString *_Nullable token))handler; + @end @interface GRPCCallOptions : NSObject diff --git a/src/objective-c/ProtoRPC/ProtoRPC.m b/src/objective-c/ProtoRPC/ProtoRPC.m index da534351780..92d7fce33cc 100644 --- a/src/objective-c/ProtoRPC/ProtoRPC.m +++ b/src/objective-c/ProtoRPC/ProtoRPC.m @@ -168,7 +168,7 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing } - (void)writeMessage:(GPBMessage *)message { - NSAssert([message isKindOfClass:[GPBMessage class]]); + NSAssert([message isKindOfClass:[GPBMessage class]], @"Parameter message must be a GPBMessage"); if (![message isKindOfClass:[GPBMessage class]]) { NSLog(@"Failed to send a message that is non-proto."); return; diff --git a/src/objective-c/tests/APIv2Tests/APIv2Tests.m b/src/objective-c/tests/APIv2Tests/APIv2Tests.m index ca7bf472830..fd472aafebd 100644 --- a/src/objective-c/tests/APIv2Tests/APIv2Tests.m +++ b/src/objective-c/tests/APIv2Tests/APIv2Tests.m @@ -241,7 +241,7 @@ static const NSTimeInterval kTestTimeout = 16; [self waitForExpectationsWithTimeout:kTestTimeout handler:nil]; } -- (void)getTokenWithHandler:(void (^)(NSString *token))handler { +- (void)provideTokenToHandler:(void (^)(NSString *token))handler { dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); dispatch_sync(queue, ^{ handler(@"test-access-token");