Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #import "ProtoRPC.h" |
| 20 | |
Jorge Canizales | 800a71c | 2016-07-13 14:44:33 -0700 | [diff] [blame] | 21 | #if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS |
Muxi Yan | c92d90a | 2018-04-11 18:10:02 -0700 | [diff] [blame] | 22 | #import <Protobuf/GPBProtocolBuffers.h> |
Jorge Canizales | 800a71c | 2016-07-13 14:44:33 -0700 | [diff] [blame] | 23 | #else |
Muxi Yan | c92d90a | 2018-04-11 18:10:02 -0700 | [diff] [blame] | 24 | #import <GPBProtocolBuffers.h> |
Jorge Canizales | 800a71c | 2016-07-13 14:44:33 -0700 | [diff] [blame] | 25 | #endif |
Jorge Canizales | 3936ed7 | 2015-06-21 14:43:56 -0700 | [diff] [blame] | 26 | #import <RxLibrary/GRXWriteable.h> |
Jorge Canizales | 3936ed7 | 2015-06-21 14:43:56 -0700 | [diff] [blame] | 27 | #import <RxLibrary/GRXWriter+Transformations.h> |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 28 | |
Jorge Canizales | afb7a0e | 2015-09-03 02:39:36 -0700 | [diff] [blame] | 29 | static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsingError) { |
| 30 | NSDictionary *info = @{ |
Muxi Yan | c92d90a | 2018-04-11 18:10:02 -0700 | [diff] [blame] | 31 | NSLocalizedDescriptionKey : @"Unable to parse response from the server", |
| 32 | NSLocalizedRecoverySuggestionErrorKey : |
| 33 | @"If this RPC is idempotent, retry " |
| 34 | @"with exponential backoff. Otherwise, query the server status before " |
| 35 | @"retrying.", |
| 36 | NSUnderlyingErrorKey : parsingError, |
| 37 | @"Expected class" : expectedClass, |
| 38 | @"Received value" : proto, |
| 39 | }; |
Jorge Canizales | afb7a0e | 2015-09-03 02:39:36 -0700 | [diff] [blame] | 40 | // TODO(jcanizales): Use kGRPCErrorDomain and GRPCErrorCodeInternal when they're public. |
Muxi Yan | c92d90a | 2018-04-11 18:10:02 -0700 | [diff] [blame] | 41 | return [NSError errorWithDomain:@"io.grpc" code:13 userInfo:info]; |
Jorge Canizales | afb7a0e | 2015-09-03 02:39:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Muxi Yan | 45ca202 | 2018-02-05 11:48:00 -0800 | [diff] [blame] | 44 | #pragma clang diagnostic push |
| 45 | #pragma clang diagnostic ignored "-Wdeprecated-implementations" |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 46 | @implementation ProtoRPC { |
Muxi Yan | 45ca202 | 2018-02-05 11:48:00 -0800 | [diff] [blame] | 47 | #pragma clang diagnostic pop |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 48 | id<GRXWriteable> _responseWriteable; |
| 49 | } |
| 50 | |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 51 | #pragma clang diagnostic push |
| 52 | #pragma clang diagnostic ignored "-Wobjc-designated-initializers" |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 53 | - (instancetype)initWithHost:(NSString *)host |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 54 | path:(NSString *)path |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 55 | requestsWriter:(GRXWriter *)requestsWriter { |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 56 | [NSException raise:NSInvalidArgumentException |
| 57 | format:@"Please use ProtoRPC's designated initializer instead."]; |
| 58 | return nil; |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 59 | } |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 60 | #pragma clang diagnostic pop |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 61 | |
| 62 | // Designated initializer |
| 63 | - (instancetype)initWithHost:(NSString *)host |
Makarand Dharmapurikar | 6f95010 | 2016-06-07 16:52:19 -0700 | [diff] [blame] | 64 | method:(GRPCProtoMethod *)method |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 65 | requestsWriter:(GRXWriter *)requestsWriter |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 66 | responseClass:(Class)responseClass |
| 67 | responsesWriteable:(id<GRXWriteable>)responsesWriteable { |
| 68 | // Because we can't tell the type system to constrain the class, we need to check at runtime: |
Marc Wandschneider | f03d266 | 2015-05-27 12:48:24 -0700 | [diff] [blame] | 69 | if (![responseClass respondsToSelector:@selector(parseFromData:error:)]) { |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 70 | [NSException raise:NSInvalidArgumentException |
| 71 | format:@"A protobuf class to parse the responses must be provided."]; |
| 72 | } |
Jorge Canizales | 6482d5d | 2015-04-21 01:30:00 -0700 | [diff] [blame] | 73 | // A writer that serializes the proto messages to send. |
Jorge Canizales | 29f55d5 | 2015-07-17 12:19:13 -0700 | [diff] [blame] | 74 | GRXWriter *bytesWriter = [requestsWriter map:^id(GPBMessage *proto) { |
Jorge Canizales | e7209c5 | 2015-09-03 01:53:06 -0700 | [diff] [blame] | 75 | if (![proto isKindOfClass:GPBMessage.class]) { |
| 76 | [NSException raise:NSInvalidArgumentException |
| 77 | format:@"Request must be a proto message: %@", proto]; |
| 78 | } |
Jorge Canizales | 29f55d5 | 2015-07-17 12:19:13 -0700 | [diff] [blame] | 79 | return [proto data]; |
| 80 | }]; |
Jorge Canizales | 5260f53 | 2015-07-04 14:47:41 -0700 | [diff] [blame] | 81 | if ((self = [super initWithHost:host path:method.HTTPPath requestsWriter:bytesWriter])) { |
Jorge Canizales | e7209c5 | 2015-09-03 01:53:06 -0700 | [diff] [blame] | 82 | __weak ProtoRPC *weakSelf = self; |
| 83 | |
Jorge Canizales | 6482d5d | 2015-04-21 01:30:00 -0700 | [diff] [blame] | 84 | // A writeable that parses the proto messages received. |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 85 | _responseWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { |
Jorge Canizales | e7209c5 | 2015-09-03 01:53:06 -0700 | [diff] [blame] | 86 | // TODO(jcanizales): This is done in the main thread, and needs to happen in another thread. |
| 87 | NSError *error = nil; |
| 88 | id parsed = [responseClass parseFromData:value error:&error]; |
| 89 | if (parsed) { |
| 90 | [responsesWriteable writeValue:parsed]; |
| 91 | } else { |
Jorge Canizales | afb7a0e | 2015-09-03 02:39:36 -0700 | [diff] [blame] | 92 | [weakSelf finishWithError:ErrorForBadProto(value, responseClass, error)]; |
Jorge Canizales | e7209c5 | 2015-09-03 01:53:06 -0700 | [diff] [blame] | 93 | } |
Muxi Yan | c92d90a | 2018-04-11 18:10:02 -0700 | [diff] [blame] | 94 | } |
| 95 | completionHandler:^(NSError *errorOrNil) { |
| 96 | [responsesWriteable writesFinishedWithError:errorOrNil]; |
| 97 | }]; |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 98 | } |
| 99 | return self; |
| 100 | } |
| 101 | |
| 102 | - (void)start { |
| 103 | [self startWithWriteable:_responseWriteable]; |
| 104 | } |
| 105 | |
| 106 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
| 107 | [super startWithWriteable:writeable]; |
Jorge Canizales | 6482d5d | 2015-04-21 01:30:00 -0700 | [diff] [blame] | 108 | // Break retain cycles. |
Jorge Canizales | 2779ccb | 2015-04-20 23:42:46 -0700 | [diff] [blame] | 109 | _responseWriteable = nil; |
| 110 | } |
| 111 | @end |
Makarand Dharmapurikar | 6f95010 | 2016-06-07 16:52:19 -0700 | [diff] [blame] | 112 | |
Makarand Dharmapurikar | 4f11ab1 | 2016-06-08 10:40:00 -0700 | [diff] [blame] | 113 | @implementation GRPCProtoCall |
Makarand Dharmapurikar | 6f95010 | 2016-06-07 16:52:19 -0700 | [diff] [blame] | 114 | |
| 115 | @end |