Updated user agent to be a host-level setting

pull/4985/head
Kristopher Wuollett 9 years ago
parent 99354c3cad
commit 20b79e702d
  1. 2
      examples/objective-c/helloworld/main.m
  2. 7
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.h
  3. 15
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.m
  4. 1
      src/objective-c/GRPCClient/private/GRPCHost.h
  5. 5
      src/objective-c/GRPCClient/private/GRPCHost.m

@ -43,7 +43,7 @@ static NSString * const kHostAddress = @"localhost:50051";
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
@autoreleasepool { @autoreleasepool {
[GRPCCall useInsecureConnectionsForHost:kHostAddress]; [GRPCCall useInsecureConnectionsForHost:kHostAddress];
[GRPCCall setUserAgentPrefix:@"HelloWorld/1.0"]; [GRPCCall setUserAgentPrefix:@"HelloWorld/1.0" forHost:kHostAddress];
HLWGreeter *client = [[HLWGreeter alloc] initWithHost:kHostAddress]; HLWGreeter *client = [[HLWGreeter alloc] initWithHost:kHostAddress];

@ -38,10 +38,9 @@
@interface GRPCCall (ChannelArg) @interface GRPCCall (ChannelArg)
/** /**
* Use the provided @c userAgentPrefix at the beginning of the HTTP User Agent string for all calls. * Use the provided @c userAgentPrefix at the beginning of the HTTP User Agent string for all calls
* to the specified @c host.
*/ */
+ (void)setUserAgentPrefix:(NSString *)userAgentPrefix; + (void)setUserAgentPrefix:(NSString *)userAgentPrefix forHost:(NSString *)host;
+ (NSString *)userAgentPrefix;
@end @end

@ -39,16 +39,13 @@
static NSString *_userAgentPrefix; static NSString *_userAgentPrefix;
+ (void)setUserAgentPrefix:(NSString *)userAgentPrefix { + (void)setUserAgentPrefix:(NSString *)userAgentPrefix forHost:(NSString *)host {
@synchronized(self) { if (!host) {
_userAgentPrefix = userAgentPrefix; [NSException raise:NSInvalidArgumentException
} format:@"host and userAgentPrefix must be provided."];
}
+ (NSString *)userAgentPrefix {
@synchronized(self) {
return _userAgentPrefix;
} }
GRPCHost *hostConfig = [GRPCHost hostWithAddress:host];
hostConfig.userAgentPrefix = userAgentPrefix;
} }
@end @end

@ -39,6 +39,7 @@ struct grpc_call;
@interface GRPCHost : NSObject @interface GRPCHost : NSObject
@property(nonatomic, readonly) NSString *address; @property(nonatomic, readonly) NSString *address;
@property(nonatomic, copy) NSString *userAgentPrefix;
/** The following properties should only be modified for testing: */ /** The following properties should only be modified for testing: */

@ -109,9 +109,8 @@
if (!_channel) { if (!_channel) {
NSMutableDictionary *args = [NSMutableDictionary dictionary]; NSMutableDictionary *args = [NSMutableDictionary dictionary];
NSString *userAgentPrefix = [[GRPCCall userAgentPrefix] copy]; if (_userAgentPrefix) {
if (userAgentPrefix) { args[@GRPC_ARG_PRIMARY_USER_AGENT_STRING] = _userAgentPrefix;
args[@GRPC_ARG_PRIMARY_USER_AGENT_STRING] = userAgentPrefix;
} }
if (_secure) { if (_secure) {

Loading…
Cancel
Save