Skip to content
Snippets Groups Projects
Commit fdea83d4 authored by Muxi Yan's avatar Muxi Yan
Browse files

Update grpc objc API for support of PUT method

parent 16c26ed2
No related branches found
No related tags found
No related merge requests found
...@@ -225,7 +225,12 @@ extern id const kGRPCTrailersKey; ...@@ -225,7 +225,12 @@ extern id const kGRPCTrailersKey;
*/ */
- (instancetype)initWithHost:(NSString *)host - (instancetype)initWithHost:(NSString *)host
path:(NSString *)path path:(NSString *)path
requestsWriter:(GRXWriter *)requestsWriter NS_DESIGNATED_INITIALIZER; requestsWriter:(GRXWriter *)requestsWriter;
- (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
requestsWriter:(GRXWriter *)requestsWriter
http2Method:(NSString *)http2Method NS_DESIGNATED_INITIALIZER;
/** /**
* Finishes the request side of this call, notifies the server that the RPC should be cancelled, and * Finishes the request side of this call, notifies the server that the RPC should be cancelled, and
......
...@@ -75,6 +75,7 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey"; ...@@ -75,6 +75,7 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
NSString *_host; NSString *_host;
NSString *_path; NSString *_path;
NSString *_http2Method;
GRPCWrappedCall *_wrappedCall; GRPCWrappedCall *_wrappedCall;
GRPCConnectivityMonitor *_connectivityMonitor; GRPCConnectivityMonitor *_connectivityMonitor;
...@@ -109,13 +110,20 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey"; ...@@ -109,13 +110,20 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
} }
- (instancetype)init { - (instancetype)init {
return [self initWithHost:nil path:nil requestsWriter:nil]; return [self initWithHost:nil path:nil requestsWriter:nil http2Method:nil];
}
- (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
requestsWriter:(GRXWriter *)requestWriter{
return [self initWithHost:host path:path requestsWriter:requestWriter http2Method:@"POST"];
} }
// Designated initializer // Designated initializer
- (instancetype)initWithHost:(NSString *)host - (instancetype)initWithHost:(NSString *)host
path:(NSString *)path path:(NSString *)path
requestsWriter:(GRXWriter *)requestWriter { requestsWriter:(GRXWriter *)requestWriter
http2Method:(NSString *)http2Method {
if (!host || !path) { if (!host || !path) {
[NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."]; [NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."];
} }
...@@ -126,6 +134,7 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey"; ...@@ -126,6 +134,7 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
if ((self = [super init])) { if ((self = [super init])) {
_host = [host copy]; _host = [host copy];
_path = [path copy]; _path = [path copy];
_http2Method = http2Method;
// Serial queue to invoke the non-reentrant methods of the grpc_call object. // Serial queue to invoke the non-reentrant methods of the grpc_call object.
_callQueue = dispatch_queue_create("io.grpc.call", NULL); _callQueue = dispatch_queue_create("io.grpc.call", NULL);
...@@ -231,6 +240,7 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey"; ...@@ -231,6 +240,7 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
- (void)sendHeaders:(NSDictionary *)headers { - (void)sendHeaders:(NSDictionary *)headers {
// TODO(jcanizales): Add error handlers for async failures // TODO(jcanizales): Add error handlers for async failures
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMetadata alloc] initWithMetadata:headers [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMetadata alloc] initWithMetadata:headers
http2Method:_http2Method
handler:nil]]]; handler:nil]]];
} }
......
...@@ -45,6 +45,10 @@ ...@@ -45,6 +45,10 @@
@interface GRPCOpSendMetadata : GRPCOperation @interface GRPCOpSendMetadata : GRPCOperation
- (instancetype)initWithMetadata:(NSDictionary *)metadata - (instancetype)initWithMetadata:(NSDictionary *)metadata
handler:(void(^)())handler;
- (instancetype)initWithMetadata:(NSDictionary *)metadata
http2Method:(NSString *)http2Method
handler:(void(^)())handler NS_DESIGNATED_INITIALIZER; handler:(void(^)())handler NS_DESIGNATED_INITIALIZER;
@end @end
......
...@@ -64,16 +64,26 @@ ...@@ -64,16 +64,26 @@
@implementation GRPCOpSendMetadata @implementation GRPCOpSendMetadata
- (instancetype)init { - (instancetype)init {
return [self initWithMetadata:nil handler:nil]; return [self initWithMetadata:nil http2Method:nil handler:nil];
} }
- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)())handler { - (instancetype)initWithMetadata:(NSDictionary *)metadata
handler:(void (^)())handler {
return [self initWithMetadata:metadata http2Method:@"POST" handler:handler];
}
- (instancetype)initWithMetadata:(NSDictionary *)metadata
http2Method:(NSString *)http2Method
handler:(void (^)())handler {
if (self = [super init]) { if (self = [super init]) {
_op.op = GRPC_OP_SEND_INITIAL_METADATA; _op.op = GRPC_OP_SEND_INITIAL_METADATA;
_op.data.send_initial_metadata.count = metadata.count; _op.data.send_initial_metadata.count = metadata.count;
_op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray; _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray;
_op.data.send_initial_metadata.maybe_compression_level.is_set = false; _op.data.send_initial_metadata.maybe_compression_level.is_set = false;
_op.data.send_initial_metadata.maybe_compression_level.level = 0; _op.data.send_initial_metadata.maybe_compression_level.level = 0;
if ([http2Method isEqualToString:@"PUT"]) {
_op.flags = GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST;
}
_handler = handler; _handler = handler;
} }
return self; return self;
......
...@@ -47,7 +47,8 @@ __attribute__((deprecated("Please use GRPCProtoCall."))) ...@@ -47,7 +47,8 @@ __attribute__((deprecated("Please use GRPCProtoCall.")))
method:(GRPCProtoMethod *)method method:(GRPCProtoMethod *)method
requestsWriter:(GRXWriter *)requestsWriter requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable NS_DESIGNATED_INITIALIZER; responsesWriteable:(id<GRXWriteable>)responsesWriteable
http2Method:(NSString *)http2Method NS_DESIGNATED_INITIALIZER;
- (void)start; - (void)start;
@end @end
......
...@@ -65,7 +65,8 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing ...@@ -65,7 +65,8 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
#pragma clang diagnostic ignored "-Wobjc-designated-initializers" #pragma clang diagnostic ignored "-Wobjc-designated-initializers"
- (instancetype)initWithHost:(NSString *)host - (instancetype)initWithHost:(NSString *)host
path:(NSString *)path path:(NSString *)path
requestsWriter:(GRXWriter *)requestsWriter { requestsWriter:(GRXWriter *)requestsWriter
http2Method:(NSString *)http2Method {
[NSException raise:NSInvalidArgumentException [NSException raise:NSInvalidArgumentException
format:@"Please use ProtoRPC's designated initializer instead."]; format:@"Please use ProtoRPC's designated initializer instead."];
return nil; return nil;
...@@ -77,7 +78,8 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing ...@@ -77,7 +78,8 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
method:(GRPCProtoMethod *)method method:(GRPCProtoMethod *)method
requestsWriter:(GRXWriter *)requestsWriter requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable { responsesWriteable:(id<GRXWriteable>)responsesWriteable
http2Method:(NSString *)http2Method {
// Because we can't tell the type system to constrain the class, we need to check at runtime: // Because we can't tell the type system to constrain the class, we need to check at runtime:
if (![responseClass respondsToSelector:@selector(parseFromData:error:)]) { if (![responseClass respondsToSelector:@selector(parseFromData:error:)]) {
[NSException raise:NSInvalidArgumentException [NSException raise:NSInvalidArgumentException
...@@ -91,7 +93,8 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing ...@@ -91,7 +93,8 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
} }
return [proto data]; return [proto data];
}]; }];
if ((self = [super initWithHost:host path:method.HTTPPath requestsWriter:bytesWriter])) { if ((self = [super initWithHost:host path:method.HTTPPath requestsWriter:bytesWriter
http2Method:http2Method])) {
__weak ProtoRPC *weakSelf = self; __weak ProtoRPC *weakSelf = self;
// A writeable that parses the proto messages received. // A writeable that parses the proto messages received.
......
...@@ -47,7 +47,8 @@ __attribute__((deprecated("Please use GRPCProtoService."))) ...@@ -47,7 +47,8 @@ __attribute__((deprecated("Please use GRPCProtoService.")))
- (GRPCProtoCall *)RPCToMethod:(NSString *)method - (GRPCProtoCall *)RPCToMethod:(NSString *)method
requestsWriter:(GRXWriter *)requestsWriter requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable; responsesWriteable:(id<GRXWriteable>)responsesWriteable
http2Method:(NSString *)http2Method;
@end @end
......
...@@ -68,7 +68,8 @@ ...@@ -68,7 +68,8 @@
- (GRPCProtoCall *)RPCToMethod:(NSString *)method - (GRPCProtoCall *)RPCToMethod:(NSString *)method
requestsWriter:(GRXWriter *)requestsWriter requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable { responsesWriteable:(id<GRXWriteable>)responsesWriteable
http2Method:(NSString *)http2Method {
GRPCProtoMethod *methodName = [[GRPCProtoMethod alloc] initWithPackage:_packageName GRPCProtoMethod *methodName = [[GRPCProtoMethod alloc] initWithPackage:_packageName
service:_serviceName service:_serviceName
method:method]; method:method];
...@@ -76,7 +77,8 @@ ...@@ -76,7 +77,8 @@
method:methodName method:methodName
requestsWriter:requestsWriter requestsWriter:requestsWriter
responseClass:responseClass responseClass:responseClass
responsesWriteable:responsesWriteable]; responsesWriteable:responsesWriteable
http2Method:http2Method];
} }
@end @end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment