mirror of https://github.com/grpc/grpc.git
parent
ad51e66324
commit
d6a201b62b
11 changed files with 220 additions and 159 deletions
@ -0,0 +1,34 @@ |
||||
#import "ProtoRPC.h" |
||||
#import <GRPCClient/GRPCCallLegacy.h> |
||||
|
||||
@class GRPCProtoMethod; |
||||
@class GRXWriter; |
||||
@protocol GRXWriteable; |
||||
|
||||
__attribute__((deprecated("Please use GRPCProtoCall."))) @interface ProtoRPC |
||||
: GRPCCall |
||||
|
||||
/**
|
||||
* host parameter should not contain the scheme (http:// or https://), only the name or IP
|
||||
* addr and the port number, for example @"localhost:5050". |
||||
*/ |
||||
- |
||||
(instancetype)initWithHost : (NSString *)host method |
||||
: (GRPCProtoMethod *)method requestsWriter : (GRXWriter *)requestsWriter responseClass |
||||
: (Class)responseClass responsesWriteable |
||||
: (id<GRXWriteable>)responsesWriteable NS_DESIGNATED_INITIALIZER; |
||||
|
||||
- (void)start; |
||||
@end |
||||
|
||||
/**
|
||||
* This subclass is empty now. Eventually we'll remove ProtoRPC class |
||||
* to avoid potential naming conflict |
||||
*/ |
||||
#pragma clang diagnostic push |
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
||||
@interface GRPCProtoCall : ProtoRPC |
||||
#pragma clang diagnostic pop |
||||
|
||||
@end |
||||
|
@ -0,0 +1,84 @@ |
||||
#import "ProtoRPCLegacy.h" |
||||
|
||||
#if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS |
||||
#import <Protobuf/GPBProtocolBuffers.h> |
||||
#else |
||||
#import <GPBProtocolBuffers.h> |
||||
#endif |
||||
#import <GRPCClient/GRPCCall.h> |
||||
#import <RxLibrary/GRXWriteable.h> |
||||
#import <RxLibrary/GRXWriter+Transformations.h> |
||||
|
||||
|
||||
#pragma clang diagnostic push |
||||
#pragma clang diagnostic ignored "-Wdeprecated-implementations" |
||||
@implementation ProtoRPC { |
||||
#pragma clang diagnostic pop |
||||
id<GRXWriteable> _responseWriteable; |
||||
} |
||||
|
||||
#pragma clang diagnostic push |
||||
#pragma clang diagnostic ignored "-Wobjc-designated-initializers" |
||||
- (instancetype)initWithHost:(NSString *)host |
||||
path:(NSString *)path |
||||
requestsWriter:(GRXWriter *)requestsWriter { |
||||
[NSException raise:NSInvalidArgumentException |
||||
format:@"Please use ProtoRPC's designated initializer instead."]; |
||||
return nil; |
||||
} |
||||
#pragma clang diagnostic pop |
||||
|
||||
// Designated initializer |
||||
- (instancetype)initWithHost:(NSString *)host |
||||
method:(GRPCProtoMethod *)method |
||||
requestsWriter:(GRXWriter *)requestsWriter |
||||
responseClass:(Class)responseClass |
||||
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 |
||||
format:@"A protobuf class to parse the responses must be provided."]; |
||||
} |
||||
// A writer that serializes the proto messages to send. |
||||
GRXWriter *bytesWriter = [requestsWriter map:^id(GPBMessage *proto) { |
||||
if (![proto isKindOfClass:[GPBMessage class]]) { |
||||
[NSException raise:NSInvalidArgumentException |
||||
format:@"Request must be a proto message: %@", proto]; |
||||
} |
||||
return [proto data]; |
||||
}]; |
||||
if ((self = [super initWithHost:host path:method.HTTPPath requestsWriter:bytesWriter])) { |
||||
__weak ProtoRPC *weakSelf = self; |
||||
|
||||
// A writeable that parses the proto messages received. |
||||
_responseWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
||||
// TODO(jcanizales): This is done in the main thread, and needs to happen in another thread. |
||||
NSError *error = nil; |
||||
id parsed = [responseClass parseFromData:value error:&error]; |
||||
if (parsed) { |
||||
[responsesWriteable writeValue:parsed]; |
||||
} else { |
||||
[weakSelf finishWithError:ErrorForBadProto(value, responseClass, error)]; |
||||
} |
||||
} |
||||
completionHandler:^(NSError *errorOrNil) { |
||||
[responsesWriteable writesFinishedWithError:errorOrNil]; |
||||
}]; |
||||
} |
||||
return self; |
||||
} |
||||
|
||||
- (void)start { |
||||
[self startWithWriteable:_responseWriteable]; |
||||
} |
||||
|
||||
- (void)startWithWriteable:(id<GRXWriteable>)writeable { |
||||
[super startWithWriteable:writeable]; |
||||
// Break retain cycles. |
||||
_responseWriteable = nil; |
||||
} |
||||
@end |
||||
|
||||
@implementation GRPCProtoCall |
||||
|
||||
@end |
@ -0,0 +1,18 @@ |
||||
#import "ProtoService.h" |
||||
|
||||
@class GRPCProtoCall; |
||||
@class GRXWriter; |
||||
@protocol GRXWriteable; |
||||
|
||||
@interface ProtoService (Legacy) |
||||
|
||||
- (instancetype)initWithHost:(NSString *)host |
||||
packageName:(NSString *)packageName |
||||
serviceName:(NSString *)serviceName; |
||||
|
||||
- (GRPCProtoCall *)RPCToMethod:(NSString *)method |
||||
requestsWriter:(GRXWriter *)requestsWriter |
||||
responseClass:(Class)responseClass |
||||
responsesWriteable:(id<GRXWriteable>)responsesWriteable; |
||||
|
||||
@end |
@ -0,0 +1,40 @@ |
||||
#import "ProtoServiceLegacy.h" |
||||
#import "ProtoRPCLegacy.h" |
||||
#import "ProtoMethod.h" |
||||
|
||||
#pragma clang diagnostic push |
||||
#pragma clang diagnostic ignored "-Wdeprecated-implementations" |
||||
@implementation ProtoService (Legacy) |
||||
#pragma clang diagnostic pop |
||||
|
||||
#pragma clang diagnostic push |
||||
#pragma clang diagnostic ignored "-Wobjc-designated-initializers" |
||||
// Do not call designated initializer here due to nullability incompatibility. This method is from |
||||
// old API and does not assert on nullability of the parameters. |
||||
|
||||
- (instancetype)initWithHost:(NSString *)host |
||||
packageName:(NSString *)packageName |
||||
serviceName:(NSString *)serviceName { |
||||
if ((self = [super init])) { |
||||
_host = [host copy]; |
||||
_packageName = [packageName copy]; |
||||
_serviceName = [serviceName copy]; |
||||
} |
||||
return self; |
||||
} |
||||
|
||||
|
||||
- (GRPCProtoCall *)RPCToMethod:(NSString *)method |
||||
requestsWriter:(GRXWriter *)requestsWriter |
||||
responseClass:(Class)responseClass |
||||
responsesWriteable:(id<GRXWriteable>)responsesWriteable { |
||||
GRPCProtoMethod *methodName = |
||||
[[GRPCProtoMethod alloc] initWithPackage:_packageName service:_serviceName method:method]; |
||||
return [[GRPCProtoCall alloc] initWithHost:_host |
||||
method:methodName |
||||
requestsWriter:requestsWriter |
||||
responseClass:responseClass |
||||
responsesWriteable:responsesWriteable]; |
||||
} |
||||
|
||||
@end |
Loading…
Reference in new issue