ObjC API update

pull/8223/head
Muxi Yan 8 years ago
parent 9deb09fa36
commit 22f7973179
  1. 24
      src/objective-c/GRPCClient/GRPCCall.h
  2. 43
      src/objective-c/GRPCClient/GRPCCall.m
  3. 3
      src/objective-c/ProtoRPC/ProtoRPC.h
  4. 9
      src/objective-c/ProtoRPC/ProtoRPC.m
  5. 3
      src/objective-c/ProtoRPC/ProtoService.h
  6. 6
      src/objective-c/ProtoRPC/ProtoService.m

@ -158,13 +158,12 @@ typedef NS_ENUM(NSUInteger, GRPCErrorCode) {
* Flags for options of a gRPC call
*
*/
typedef NS_OPTIONS(NSUInteger, GRPCCallFlags) {
typedef NS_ENUM(NSUInteger, GRPCCallAttr) {
GRPCCallAttrDefault = 0,
/** Signal that the call is idempotent */
GRPCFlagIdempotentRequest = 0x00000010,
/** Signal that the call should not return UNAVAILABLE before it has started */
GRPCFlagIgnoreConnectivity = 0x00000020,
GRPCCallAttrIdempotentRequest = 1,
/** Signal that the call is cacheable. GRPC is free to use GET verb */
GRPCFlagCacheableRequest = 0x00000040,
GRPCCallAttrCacheableRequest = 2,
};
/**
@ -238,12 +237,7 @@ extern id const kGRPCTrailersKey;
*/
- (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
requestsWriter:(GRXWriter *)requestsWriter;
- (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
requestsWriter:(GRXWriter *)requestsWriter
flags:(GRPCCallFlags)flags NS_DESIGNATED_INITIALIZER;
requestsWriter:(GRXWriter *)requestsWriter NS_DESIGNATED_INITIALIZER;
/**
* Finishes the request side of this call, notifies the server that the RPC should be cancelled, and
@ -251,6 +245,14 @@ extern id const kGRPCTrailersKey;
*/
- (void)cancel;
/**
* Set the call flag for a specific host path.
*
* Host parameter should not contain the scheme (http:// or https://), only the name or IP addr
* and the port number, for example @"localhost:5050".
*/
+ (void)setCallAttribute:(GRPCCallAttr)callAttr host:(NSString *)host path:(NSString *)path;
// TODO(jcanizales): Let specify a deadline. As a category of GRXWriter?
@end

@ -47,6 +47,7 @@
NSString * const kGRPCHeadersKey = @"io.grpc.HeadersKey";
NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
static NSMutableDictionary *callFlags;
@interface GRPCCall () <GRXWriteable>
// Make them read-write.
@ -75,7 +76,6 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
NSString *_host;
NSString *_path;
GRPCCallFlags _flags;
GRPCWrappedCall *_wrappedCall;
GRPCConnectivityMonitor *_connectivityMonitor;
@ -107,23 +107,43 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
// TODO(jcanizales): If grpc_init is idempotent, this should be changed from load to initialize.
+ (void)load {
grpc_init();
callFlags = [NSMutableDictionary dictionary];
}
+ (void)setCallAttribute:(GRPCCallAttr)callAttr host:(NSString *)host path:(NSString *)path {
NSString *hostAndPath = [NSString stringWithFormat:@"%@%@", host, path];
switch (callAttr) {
case GRPCCallAttrDefault:
callFlags[hostAndPath] = @0;
break;
case GRPCCallAttrIdempotentRequest:
callFlags[hostAndPath] = @GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST;
break;
case GRPCCallAttrCacheableRequest:
callFlags[hostAndPath] = @GRPC_INITIAL_METADATA_CACHEABLE_REQUEST;
break;
default:
break;
}
}
+ (uint32_t)getCallFlag:(NSString *)host path:(NSString *)path {
NSString *hostAndPath = [NSString stringWithFormat:@"%@%@", host, path];
if (nil != [callFlags objectForKey:hostAndPath]) {
return [callFlags[hostAndPath] intValue];
} else {
return 0;
}
- (instancetype)init {
return [self initWithHost:nil path:nil requestsWriter:nil flags:0];
}
- (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
requestsWriter:(GRXWriter *)requestWriter{
return [self initWithHost:host path:path requestsWriter:requestWriter flags:0];
- (instancetype)init {
return [self initWithHost:nil path:nil requestsWriter:nil];
}
// Designated initializer
- (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
requestsWriter:(GRXWriter *)requestWriter
flags:(GRPCCallFlags)flags {
requestsWriter:(GRXWriter *)requestWriter {
if (!host || !path) {
[NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."];
}
@ -134,7 +154,6 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
if ((self = [super init])) {
_host = [host copy];
_path = [path copy];
_flags = flags;
// Serial queue to invoke the non-reentrant methods of the grpc_call object.
_callQueue = dispatch_queue_create("io.grpc.call", NULL);
@ -240,7 +259,7 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
- (void)sendHeaders:(NSDictionary *)headers {
// TODO(jcanizales): Add error handlers for async failures
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMetadata alloc] initWithMetadata:headers
flags:_flags
flags:(uint32_t)[GRPCCall getCallFlag:_host path:_path]
handler:nil]]];
}

@ -47,8 +47,7 @@ __attribute__((deprecated("Please use GRPCProtoCall.")))
method:(GRPCProtoMethod *)method
requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable
flags:(GRPCCallFlags)flags NS_DESIGNATED_INITIALIZER;
responsesWriteable:(id<GRXWriteable>)responsesWriteable NS_DESIGNATED_INITIALIZER;
- (void)start;
@end

@ -65,8 +65,7 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"
- (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
requestsWriter:(GRXWriter *)requestsWriter
flags:(GRPCCallFlags)flags {
requestsWriter:(GRXWriter *)requestsWriter {
[NSException raise:NSInvalidArgumentException
format:@"Please use ProtoRPC's designated initializer instead."];
return nil;
@ -78,8 +77,7 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
method:(GRPCProtoMethod *)method
requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable
flags:(GRPCCallFlags)flags {
responsesWriteable:(id<GRXWriteable>)responsesWriteable {
// Because we can't tell the type system to constrain the class, we need to check at runtime:
if (![responseClass respondsToSelector:@selector(parseFromData:error:)]) {
[NSException raise:NSInvalidArgumentException
@ -93,8 +91,7 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
}
return [proto data];
}];
if ((self = [super initWithHost:host path:method.HTTPPath requestsWriter:bytesWriter
flags:flags])) {
if ((self = [super initWithHost:host path:method.HTTPPath requestsWriter:bytesWriter])) {
__weak ProtoRPC *weakSelf = self;
// A writeable that parses the proto messages received.

@ -48,8 +48,7 @@ __attribute__((deprecated("Please use GRPCProtoService.")))
- (GRPCProtoCall *)RPCToMethod:(NSString *)method
requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable
flags:(GRPCCallFlags)flags;
responsesWriteable:(id<GRXWriteable>)responsesWriteable;
@end

@ -68,8 +68,7 @@
- (GRPCProtoCall *)RPCToMethod:(NSString *)method
requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable
flags:(GRPCCallFlags)flags {
responsesWriteable:(id<GRXWriteable>)responsesWriteable {
GRPCProtoMethod *methodName = [[GRPCProtoMethod alloc] initWithPackage:_packageName
service:_serviceName
method:method];
@ -77,8 +76,7 @@
method:methodName
requestsWriter:requestsWriter
responseClass:responseClass
responsesWriteable:responsesWriteable
flags:flags];
responsesWriteable:responsesWriteable];
}
@end

Loading…
Cancel
Save