blob: 5e9324c445627b2f36658cf3980d8e5590ae9e82 [file] [log] [blame]
Jorge Canizales9409ad82015-02-18 16:19:56 -08001/*
2 *
Yang Gao5fc90292015-02-20 09:46:22 -08003 * Copyright 2015, Google Inc.
Jorge Canizales9409ad82015-02-18 16:19:56 -08004 * 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
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070034/**
35 * The gRPC protocol is an RPC protocol on top of HTTP2.
36 *
Muxi Yan2c88b462016-10-28 10:47:25 -070037 * While the most common type of RPC receives only one request message and returns only one response
38 * message, the protocol also supports RPCs that return multiple individual messages in a streaming
39 * fashion, RPCs that accept a stream of request messages, or RPCs with both streaming requests and
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070040 * responses.
41 *
Muxi Yan2c88b462016-10-28 10:47:25 -070042 * Conceptually, each gRPC call consists of a bidirectional stream of binary messages, with RPCs of
43 * the "non-streaming type" sending only one message in the corresponding direction (the protocol
44 * doesn't make any distinction).
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070045 *
Muxi Yan2c88b462016-10-28 10:47:25 -070046 * Each RPC uses a different HTTP2 stream, and thus multiple simultaneous RPCs can be multiplexed
47 * transparently on the same TCP connection.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070048 */
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -070049
50#import <Foundation/Foundation.h>
Jorge Canizales3936ed72015-06-21 14:43:56 -070051#import <RxLibrary/GRXWriter.h>
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -070052
Jorge Canizales35768db2015-12-03 23:15:09 -080053#include <AvailabilityMacros.h>
54
Jorge Canizalesd21781f2015-09-03 03:42:02 -070055#pragma mark gRPC errors
56
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070057/** Domain of NSError objects produced by gRPC. */
Jorge Canizalesd21781f2015-09-03 03:42:02 -070058extern NSString *const kGRPCErrorDomain;
59
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070060/**
61 * gRPC error codes.
Muxi Yan2c88b462016-10-28 10:47:25 -070062 * Note that a few of these are never produced by the gRPC libraries, but are of general utility for
63 * server applications to produce.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070064 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -070065typedef NS_ENUM(NSUInteger, GRPCErrorCode) {
Jorge Canizalesb10776c2015-10-26 10:44:55 -070066 /** The operation was cancelled (typically by the caller). */
Jorge Canizalesd21781f2015-09-03 03:42:02 -070067 GRPCErrorCodeCancelled = 1,
68
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070069 /**
Muxi Yan2c88b462016-10-28 10:47:25 -070070 * Unknown error. Errors raised by APIs that do not return enough error information may be
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070071 * converted to this error.
72 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -070073 GRPCErrorCodeUnknown = 2,
74
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070075 /**
Muxi Yan2c88b462016-10-28 10:47:25 -070076 * The client specified an invalid argument. Note that this differs from FAILED_PRECONDITION.
77 * INVALID_ARGUMENT indicates arguments that are problematic regardless of the state of the
78 * server (e.g., a malformed file name).
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070079 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -070080 GRPCErrorCodeInvalidArgument = 3,
81
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070082 /**
Muxi Yan2c88b462016-10-28 10:47:25 -070083 * Deadline expired before operation could complete. For operations that change the state of the
84 * server, this error may be returned even if the operation has completed successfully. For
85 * example, a successful response from the server could have been delayed long enough for the
86 * deadline to expire.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070087 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -070088 GRPCErrorCodeDeadlineExceeded = 4,
89
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070090 /** Some requested entity (e.g., file or directory) was not found. */
Jorge Canizalesd21781f2015-09-03 03:42:02 -070091 GRPCErrorCodeNotFound = 5,
92
Muxi Yan2c88b462016-10-28 10:47:25 -070093 /** Some entity that we attempted to create (e.g., file or directory) already exists. */
Jorge Canizalesd21781f2015-09-03 03:42:02 -070094 GRPCErrorCodeAlreadyExists = 6,
95
Jorge Canizalesa6da17e2015-10-18 15:30:30 -070096 /**
Muxi Yan2c88b462016-10-28 10:47:25 -070097 * The caller does not have permission to execute the specified operation. PERMISSION_DENIED isn't
98 * used for rejections caused by exhausting some resource (RESOURCE_EXHAUSTED is used instead for
99 * those errors). PERMISSION_DENIED doesn't indicate a failure to identify the caller
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700100 * (UNAUTHENTICATED is used instead for those errors).
101 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700102 GRPCErrorCodePermissionDenied = 7,
103
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700104 /**
Muxi Yan2c88b462016-10-28 10:47:25 -0700105 * The request does not have valid authentication credentials for the operation (e.g. the caller's
106 * identity can't be verified).
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700107 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700108 GRPCErrorCodeUnauthenticated = 16,
109
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700110 /** Some resource has been exhausted, perhaps a per-user quota. */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700111 GRPCErrorCodeResourceExhausted = 8,
112
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700113 /**
Muxi Yan2c88b462016-10-28 10:47:25 -0700114 * The RPC was rejected because the server is not in a state required for the procedure's
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700115 * execution. For example, a directory to be deleted may be non-empty, etc.
Muxi Yan2c88b462016-10-28 10:47:25 -0700116 * The client should not retry until the server state has been explicitly fixed (e.g. by
117 * performing another RPC). The details depend on the service being called, and should be found in
118 * the NSError's userInfo.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700119 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700120 GRPCErrorCodeFailedPrecondition = 9,
121
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700122 /**
Muxi Yan2c88b462016-10-28 10:47:25 -0700123 * The RPC was aborted, typically due to a concurrency issue like sequencer check failures,
124 * transaction aborts, etc. The client should retry at a higher-level (e.g., restarting a read-
125 * modify-write sequence).
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700126 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700127 GRPCErrorCodeAborted = 10,
128
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700129 /**
Muxi Yan2c88b462016-10-28 10:47:25 -0700130 * The RPC was attempted past the valid range. E.g., enumerating past the end of a list.
131 * Unlike INVALID_ARGUMENT, this error indicates a problem that may be fixed if the system state
132 * changes. For example, an RPC to get elements of a list will generate INVALID_ARGUMENT if asked
133 * to return the element at a negative index, but it will generate OUT_OF_RANGE if asked to return
134 * the element at an index past the current size of the list.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700135 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700136 GRPCErrorCodeOutOfRange = 11,
137
Muxi Yan2c88b462016-10-28 10:47:25 -0700138 /** The procedure is not implemented or not supported/enabled in this server. */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700139 GRPCErrorCodeUnimplemented = 12,
140
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700141 /**
Muxi Yan2c88b462016-10-28 10:47:25 -0700142 * Internal error. Means some invariant expected by the server application or the gRPC library has
143 * been broken.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700144 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700145 GRPCErrorCodeInternal = 13,
146
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700147 /**
Muxi Yan2c88b462016-10-28 10:47:25 -0700148 * The server is currently unavailable. This is most likely a transient condition and may be
149 * corrected by retrying with a backoff.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700150 */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700151 GRPCErrorCodeUnavailable = 14,
152
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700153 /** Unrecoverable data loss or corruption. */
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700154 GRPCErrorCodeDataLoss = 15,
155};
156
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700157/**
Muxi Yan6c0b9602016-10-02 14:32:06 -0700158 * Safety remark of a gRPC method as defined in RFC 2616 Section 9.1
Muxi Yan9deb09f2016-09-28 16:03:04 -0700159 */
Muxi Yan6c0b9602016-10-02 14:32:06 -0700160typedef NS_ENUM(NSUInteger, GRPCCallSafety) {
Muxi Yan2c88b462016-10-28 10:47:25 -0700161 /** Signal that there is no guarantees on how the call affects the server state. */
Muxi Yan6c0b9602016-10-02 14:32:06 -0700162 GRPCCallSafetyDefault = 0,
163 /** Signal that the call is idempotent. gRPC is free to use PUT verb. */
164 GRPCCallSafetyIdempotentRequest = 1,
Muxi Yan2c88b462016-10-28 10:47:25 -0700165 /** Signal that the call is cacheable and will not affect server state. gRPC is free to use GET verb. */
Muxi Yan6c0b9602016-10-02 14:32:06 -0700166 GRPCCallSafetyCacheableRequest = 2,
Muxi Yan9deb09f2016-09-28 16:03:04 -0700167};
168
169/**
Muxi Yan2c88b462016-10-28 10:47:25 -0700170 * Keys used in |NSError|'s |userInfo| dictionary to store the response headers and trailers sent by
171 * the server.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700172 */
Jorge Canizales1ab2a712015-08-12 20:32:11 -0700173extern id const kGRPCHeadersKey;
174extern id const kGRPCTrailersKey;
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700175
Jorge Canizalesd21781f2015-09-03 03:42:02 -0700176#pragma mark GRPCCall
177
Jorge Canizalesf4f150f2015-11-01 22:31:12 -0800178/** Represents a single gRPC remote call. */
179@interface GRPCCall : GRXWriter
180
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700181/**
Muxi Yan2c88b462016-10-28 10:47:25 -0700182 * The container of the request headers of an RPC conforms to this protocol, which is a subset of
183 * NSMutableDictionary's interface. It will become a NSMutableDictionary later on.
184 * The keys of this container are the header names, which per the HTTP standard are case-
185 * insensitive. They are stored in lowercase (which is how HTTP/2 mandates them on the wire), and
186 * can only consist of ASCII characters.
187 * A header value is a NSString object (with only ASCII characters), unless the header name has the
188 * suffix "-bin", in which case the value has to be a NSData object.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700189 */
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700190/**
Muxi Yan2c88b462016-10-28 10:47:25 -0700191 * These HTTP headers will be passed to the server as part of this call. Each HTTP header is a
192 * name-value pair with string names and either string or binary values.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700193 *
Muxi Yan2c88b462016-10-28 10:47:25 -0700194 * The passed dictionary has to use NSString keys, corresponding to the header names. The value
195 * associated to each can be a NSString object or a NSData object. E.g.:
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700196 *
197 * call.requestHeaders = @{@"authorization": @"Bearer ..."};
198 *
199 * call.requestHeaders[@"my-header-bin"] = someData;
200 *
201 * After the call is started, trying to modify this property is an error.
202 *
203 * The property is initialized to an empty NSMutableDictionary.
204 */
Jorge Canizalesf4f150f2015-11-01 22:31:12 -0800205@property(atomic, readonly) NSMutableDictionary *requestHeaders;
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800206
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700207/**
Muxi Yan2c88b462016-10-28 10:47:25 -0700208 * This dictionary is populated with the HTTP headers received from the server. This happens before
209 * any response message is received from the server. It has the same structure as the request
210 * headers dictionary: Keys are NSString header names; names ending with the suffix "-bin" have a
211 * NSData value; the others have a NSString value.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700212 *
Muxi Yan2c88b462016-10-28 10:47:25 -0700213 * The value of this property is nil until all response headers are received, and will change before
214 * any of -writeValue: or -writesFinishedWithError: are sent to the writeable.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700215 */
Jorge Canizales0b34c892015-08-12 20:19:20 -0700216@property(atomic, readonly) NSDictionary *responseHeaders;
217
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700218/**
Muxi Yan2c88b462016-10-28 10:47:25 -0700219 * Same as responseHeaders, but populated with the HTTP trailers received from the server before the
220 * call finishes.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700221 *
Muxi Yan2c88b462016-10-28 10:47:25 -0700222 * The value of this property is nil until all response trailers are received, and will change
223 * before -writesFinishedWithError: is sent to the writeable.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700224 */
Jorge Canizales0b34c892015-08-12 20:19:20 -0700225@property(atomic, readonly) NSDictionary *responseTrailers;
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800226
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700227/**
Muxi Yan2c88b462016-10-28 10:47:25 -0700228 * The request writer has to write NSData objects into the provided Writeable. The server will
229 * receive each of those separately and in order as distinct messages.
230 * A gRPC call might not complete until the request writer finishes. On the other hand, the request
231 * finishing doesn't necessarily make the call to finish, as the server might continue sending
232 * messages to the response side of the call indefinitely (depending on the semantics of the
233 * specific remote method called).
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700234 * To finish a call right away, invoke cancel.
Muxi Yan2c88b462016-10-28 10:47:25 -0700235 * host parameter should not contain the scheme (http:// or https://), only the name or IP addr
236 * and the port number, for example @"localhost:5050".
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700237 */
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800238- (instancetype)initWithHost:(NSString *)host
Jorge Canizalesbe808e32015-07-04 14:37:58 -0700239 path:(NSString *)path
Muxi Yan2c88b462016-10-28 10:47:25 -0700240 requestsWriter:(GRXWriter *)requestsWriter NS_DESIGNATED_INITIALIZER;
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800241
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700242/**
Muxi Yan2c88b462016-10-28 10:47:25 -0700243 * Finishes the request side of this call, notifies the server that the RPC should be cancelled, and
244 * finishes the response side of the call with an error of code CANCELED.
Jorge Canizalesa6da17e2015-10-18 15:30:30 -0700245 */
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800246- (void)cancel;
247
Muxi Yan22f79732016-09-30 14:24:56 -0700248/**
249 * Set the call flag for a specific host path.
250 *
Muxi Yan2c88b462016-10-28 10:47:25 -0700251 * Host parameter should not contain the scheme (http:// or https://), only the name or IP addr
252 * and the port number, for example @"localhost:5050".
Muxi Yan22f79732016-09-30 14:24:56 -0700253 */
Muxi Yan2c88b462016-10-28 10:47:25 -0700254+ (void)setCallSafety:(GRPCCallSafety)callSafety host:(NSString *)host path:(NSString *)path;
Muxi Yan22f79732016-09-30 14:24:56 -0700255
Muxi Yan895f3d82017-04-05 13:12:30 -0700256/**
Muxi Yana2529ce2017-04-19 14:32:47 -0700257 * Set the dispatch queue to be used for callbacks.
Muxi Yan895f3d82017-04-05 13:12:30 -0700258 *
259 * This configuration is only effective before the call starts.
260 */
261- (void)setResponseDispatchQueue:(dispatch_queue_t)queue;
262
Jorge Canizales86446412015-10-26 10:55:54 -0700263// TODO(jcanizales): Let specify a deadline. As a category of GRXWriter?
Jorge Canizales5e0efd92015-02-17 18:23:58 -0800264@end
Jorge Canizalese1f74452015-11-03 18:30:50 -0800265
266#pragma mark Backwards compatibiity
267
268/** This protocol is kept for backwards compatibility with existing code. */
Jorge Canizales35768db2015-12-03 23:15:09 -0800269DEPRECATED_MSG_ATTRIBUTE("Use NSDictionary or NSMutableDictionary instead.")
Muxi Yan2c88b462016-10-28 10:47:25 -0700270@protocol GRPCRequestHeaders <NSObject>
Jorge Canizalese1f74452015-11-03 18:30:50 -0800271@property(nonatomic, readonly) NSUInteger count;
272
Jorge Canizales47a7b9e2015-12-17 11:43:24 -0800273- (id)objectForKeyedSubscript:(id)key;
274- (void)setObject:(id)obj forKeyedSubscript:(id)key;
Jorge Canizalese1f74452015-11-03 18:30:50 -0800275
276- (void)removeAllObjects;
Jorge Canizales47a7b9e2015-12-17 11:43:24 -0800277- (void)removeObjectForKey:(id)key;
Jorge Canizalese1f74452015-11-03 18:30:50 -0800278@end
279
Jorge Canizales35768db2015-12-03 23:15:09 -0800280#pragma clang diagnostic push
281#pragma clang diagnostic ignored "-Wdeprecated"
Jorge Canizalese1f74452015-11-03 18:30:50 -0800282/** This is only needed for backwards-compatibility. */
Muxi Yan2c88b462016-10-28 10:47:25 -0700283@interface NSMutableDictionary (GRPCRequestHeaders) <GRPCRequestHeaders>
Jorge Canizalese1f74452015-11-03 18:30:50 -0800284@end
Jorge Canizales35768db2015-12-03 23:15:09 -0800285#pragma clang diagnostic pop