ProtoRPCLegacy.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #import "ProtoRPCLegacy.h"
  19. #import "ProtoMethod.h"
  20. #if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS
  21. #import <Protobuf/GPBProtocolBuffers.h>
  22. #else
  23. #import <GPBProtocolBuffers.h>
  24. #endif
  25. #import <GRPCClient/GRPCCall.h>
  26. #import <RxLibrary/GRXWriteable.h>
  27. #import <RxLibrary/GRXWriter+Transformations.h>
  28. #pragma clang diagnostic push
  29. #pragma clang diagnostic ignored "-Wdeprecated-implementations"
  30. @implementation ProtoRPC {
  31. #pragma clang diagnostic pop
  32. id<GRXWriteable> _responseWriteable;
  33. }
  34. #pragma clang diagnostic push
  35. #pragma clang diagnostic ignored "-Wobjc-designated-initializers"
  36. - (instancetype)initWithHost:(NSString *)host
  37. path:(NSString *)path
  38. requestsWriter:(GRXWriter *)requestsWriter {
  39. [NSException raise:NSInvalidArgumentException
  40. format:@"Please use ProtoRPC's designated initializer instead."];
  41. return nil;
  42. }
  43. #pragma clang diagnostic pop
  44. // Designated initializer
  45. - (instancetype)initWithHost:(NSString *)host
  46. method:(GRPCProtoMethod *)method
  47. requestsWriter:(GRXWriter *)requestsWriter
  48. responseClass:(Class)responseClass
  49. responsesWriteable:(id<GRXWriteable>)responsesWriteable {
  50. // Because we can't tell the type system to constrain the class, we need to check at runtime:
  51. if (![responseClass respondsToSelector:@selector(parseFromData:error:)]) {
  52. [NSException raise:NSInvalidArgumentException
  53. format:@"A protobuf class to parse the responses must be provided."];
  54. }
  55. // A writer that serializes the proto messages to send.
  56. GRXWriter *bytesWriter = [requestsWriter map:^id(GPBMessage *proto) {
  57. if (![proto isKindOfClass:[GPBMessage class]]) {
  58. [NSException raise:NSInvalidArgumentException
  59. format:@"Request must be a proto message: %@", proto];
  60. }
  61. return [proto data];
  62. }];
  63. if ((self = [super initWithHost:host path:method.HTTPPath requestsWriter:bytesWriter])) {
  64. __weak ProtoRPC *weakSelf = self;
  65. // A writeable that parses the proto messages received.
  66. _responseWriteable = [[GRXWriteable alloc]
  67. initWithValueHandler:^(NSData *value) {
  68. // TODO(jcanizales): This is done in the main thread, and needs to happen in another
  69. // thread.
  70. NSError *error = nil;
  71. id parsed = [responseClass parseFromData:value error:&error];
  72. if (parsed) {
  73. [responsesWriteable writeValue:parsed];
  74. } else {
  75. [weakSelf finishWithError:ErrorForBadProto(value, responseClass, error)];
  76. }
  77. }
  78. completionHandler:^(NSError *errorOrNil) {
  79. [responsesWriteable writesFinishedWithError:errorOrNil];
  80. }];
  81. }
  82. return self;
  83. }
  84. - (void)start {
  85. [self startWithWriteable:_responseWriteable];
  86. }
  87. - (void)startWithWriteable:(id<GRXWriteable>)writeable {
  88. [super startWithWriteable:writeable];
  89. // Break retain cycles.
  90. _responseWriteable = nil;
  91. }
  92. @end
  93. @implementation GRPCProtoCall
  94. @end