OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #import "ProtoRPC.h" |
| 35 |
| 36 #import <GPBProtocolBuffers.h> |
| 37 #import <RxLibrary/GRXWriteable.h> |
| 38 #import <RxLibrary/GRXWriter+Transformations.h> |
| 39 |
| 40 static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
Error) { |
| 41 NSDictionary *info = @{ |
| 42 NSLocalizedDescriptionKey: @"Unable to parse response f
rom the server", |
| 43 NSLocalizedRecoverySuggestionErrorKey: @"If this RPC is
idempotent, retry " |
| 44 @"with exponential backoff. Otherwise, query the server
status before " |
| 45 @"retrying.", |
| 46 NSUnderlyingErrorKey: parsingError, |
| 47 @"Expected class": expectedClass, |
| 48 @"Received value": proto, |
| 49 }; |
| 50 // TODO(jcanizales): Use kGRPCErrorDomain and GRPCErrorCodeInternal when they'
re public. |
| 51 return [NSError errorWithDomain:@"io.grpc" |
| 52 code:13 |
| 53 userInfo:info]; |
| 54 } |
| 55 |
| 56 @implementation ProtoRPC { |
| 57 id<GRXWriteable> _responseWriteable; |
| 58 } |
| 59 |
| 60 #pragma clang diagnostic push |
| 61 #pragma clang diagnostic ignored "-Wobjc-designated-initializers" |
| 62 - (instancetype)initWithHost:(NSString *)host |
| 63 path:(NSString *)path |
| 64 requestsWriter:(GRXWriter *)requestsWriter { |
| 65 [NSException raise:NSInvalidArgumentException |
| 66 format:@"Please use ProtoRPC's designated initializer instead."]; |
| 67 return nil; |
| 68 } |
| 69 #pragma clang diagnostic pop |
| 70 |
| 71 // Designated initializer |
| 72 - (instancetype)initWithHost:(NSString *)host |
| 73 method:(ProtoMethod *)method |
| 74 requestsWriter:(GRXWriter *)requestsWriter |
| 75 responseClass:(Class)responseClass |
| 76 responsesWriteable:(id<GRXWriteable>)responsesWriteable { |
| 77 // Because we can't tell the type system to constrain the class, we need to ch
eck at runtime: |
| 78 if (![responseClass respondsToSelector:@selector(parseFromData:error:)]) { |
| 79 [NSException raise:NSInvalidArgumentException |
| 80 format:@"A protobuf class to parse the responses must be provide
d."]; |
| 81 } |
| 82 // A writer that serializes the proto messages to send. |
| 83 GRXWriter *bytesWriter = [requestsWriter map:^id(GPBMessage *proto) { |
| 84 if (![proto isKindOfClass:GPBMessage.class]) { |
| 85 [NSException raise:NSInvalidArgumentException |
| 86 format:@"Request must be a proto message: %@", proto]; |
| 87 } |
| 88 return [proto data]; |
| 89 }]; |
| 90 if ((self = [super initWithHost:host path:method.HTTPPath requestsWriter:bytes
Writer])) { |
| 91 __weak ProtoRPC *weakSelf = self; |
| 92 |
| 93 // A writeable that parses the proto messages received. |
| 94 _responseWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *va
lue) { |
| 95 // TODO(jcanizales): This is done in the main thread, and needs to happen
in another thread. |
| 96 NSError *error = nil; |
| 97 id parsed = [responseClass parseFromData:value error:&error]; |
| 98 if (parsed) { |
| 99 [responsesWriteable writeValue:parsed]; |
| 100 } else { |
| 101 [weakSelf finishWithError:ErrorForBadProto(value, responseClass, error)]
; |
| 102 } |
| 103 } completionHandler:^(NSError *errorOrNil) { |
| 104 [responsesWriteable writesFinishedWithError:errorOrNil]; |
| 105 }]; |
| 106 } |
| 107 return self; |
| 108 } |
| 109 |
| 110 - (void)start { |
| 111 [self startWithWriteable:_responseWriteable]; |
| 112 } |
| 113 |
| 114 - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
| 115 [super startWithWriteable:writeable]; |
| 116 // Break retain cycles. |
| 117 _responseWriteable = nil; |
| 118 } |
| 119 @end |
OLD | NEW |