echo.client.pb.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: echo.proto
  6. *
  7. */
  8. /*
  9. *
  10. * Copyright 2017, Google Inc.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are
  15. * met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following disclaimer
  21. * in the documentation and/or other materials provided with the
  22. * distribution.
  23. * * Neither the name of Google Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from
  25. * this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. import Foundation
  41. import Dispatch
  42. import gRPC
  43. /// Type for errors thrown from generated client code.
  44. public enum Echo_EchoClientError : Error {
  45. case endOfStream
  46. case invalidMessageReceived
  47. case error(c: CallResult)
  48. }
  49. /// Get (Unary)
  50. public class Echo_EchoGetCall {
  51. private var call : Call
  52. /// Create a call.
  53. fileprivate init(_ channel: Channel) {
  54. self.call = channel.makeCall("/echo.Echo/Get")
  55. }
  56. /// Run the call. Blocks until the reply is received.
  57. fileprivate func run(request: Echo_EchoRequest,
  58. metadata: Metadata) throws -> Echo_EchoResponse {
  59. let sem = DispatchSemaphore(value: 0)
  60. var returnCallResult : CallResult!
  61. var returnResponse : Echo_EchoResponse?
  62. _ = try start(request:request, metadata:metadata) {response, callResult in
  63. returnResponse = response
  64. returnCallResult = callResult
  65. sem.signal()
  66. }
  67. _ = sem.wait(timeout: DispatchTime.distantFuture)
  68. if let returnResponse = returnResponse {
  69. return returnResponse
  70. } else {
  71. throw Echo_EchoClientError.error(c: returnCallResult)
  72. }
  73. }
  74. /// Start the call. Nonblocking.
  75. fileprivate func start(request: Echo_EchoRequest,
  76. metadata: Metadata,
  77. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  78. throws -> Echo_EchoGetCall {
  79. let requestData = try request.serializeProtobuf()
  80. try call.start(.unary,
  81. metadata:metadata,
  82. message:requestData)
  83. {(callResult) in
  84. if let responseData = callResult.resultData,
  85. let response = try? Echo_EchoResponse(protobuf:responseData) {
  86. completion(response, callResult)
  87. } else {
  88. completion(nil, callResult)
  89. }
  90. }
  91. return self
  92. }
  93. }
  94. /// Expand (Server Streaming)
  95. public class Echo_EchoExpandCall {
  96. private var call : Call
  97. /// Create a call.
  98. fileprivate init(_ channel: Channel) {
  99. self.call = channel.makeCall("/echo.Echo/Expand")
  100. }
  101. /// Call this once with the message to send. Nonblocking.
  102. fileprivate func start(request: Echo_EchoRequest,
  103. metadata: Metadata,
  104. completion: @escaping (CallResult) -> ())
  105. throws -> Echo_EchoExpandCall {
  106. let requestData = try request.serializeProtobuf()
  107. try call.start(.serverStreaming,
  108. metadata:metadata,
  109. message:requestData,
  110. completion:completion)
  111. return self
  112. }
  113. /// Call this to wait for a result. Blocking.
  114. public func receive() throws -> Echo_EchoResponse {
  115. var returnError : Echo_EchoClientError?
  116. var returnResponse : Echo_EchoResponse!
  117. let sem = DispatchSemaphore(value: 0)
  118. do {
  119. try receive() {response, error in
  120. returnResponse = response
  121. returnError = error
  122. sem.signal()
  123. }
  124. _ = sem.wait(timeout: DispatchTime.distantFuture)
  125. }
  126. if let returnError = returnError {
  127. throw returnError
  128. }
  129. return returnResponse
  130. }
  131. /// Call this to wait for a result. Nonblocking.
  132. public func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  133. do {
  134. try call.receiveMessage() {(responseData) in
  135. if let responseData = responseData {
  136. if let response = try? Echo_EchoResponse(protobuf:responseData) {
  137. completion(response, nil)
  138. } else {
  139. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  140. }
  141. } else {
  142. completion(nil, Echo_EchoClientError.endOfStream)
  143. }
  144. }
  145. }
  146. }
  147. }
  148. /// Collect (Client Streaming)
  149. public class Echo_EchoCollectCall {
  150. private var call : Call
  151. /// Create a call.
  152. fileprivate init(_ channel: Channel) {
  153. self.call = channel.makeCall("/echo.Echo/Collect")
  154. }
  155. /// Call this to start a call. Nonblocking.
  156. fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
  157. throws -> Echo_EchoCollectCall {
  158. try self.call.start(.clientStreaming, metadata:metadata, completion:completion)
  159. return self
  160. }
  161. /// Call this to send each message in the request stream. Nonblocking.
  162. public func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
  163. let messageData = try message.serializeProtobuf()
  164. try call.sendMessage(data:messageData, errorHandler:errorHandler)
  165. }
  166. /// Call this to close the connection and wait for a response. Blocking.
  167. public func closeAndReceive() throws -> Echo_EchoResponse {
  168. var returnError : Echo_EchoClientError?
  169. var returnResponse : Echo_EchoResponse!
  170. let sem = DispatchSemaphore(value: 0)
  171. do {
  172. try closeAndReceive() {response, error in
  173. returnResponse = response
  174. returnError = error
  175. sem.signal()
  176. }
  177. _ = sem.wait(timeout: DispatchTime.distantFuture)
  178. } catch (let error) {
  179. throw error
  180. }
  181. if let returnError = returnError {
  182. throw returnError
  183. }
  184. return returnResponse
  185. }
  186. /// Call this to close the connection and wait for a response. Nonblocking.
  187. public func closeAndReceive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->())
  188. throws {
  189. do {
  190. try call.receiveMessage() {(responseData) in
  191. if let responseData = responseData,
  192. let response = try? Echo_EchoResponse(protobuf:responseData) {
  193. completion(response, nil)
  194. } else {
  195. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  196. }
  197. }
  198. try call.close(completion:{})
  199. } catch (let error) {
  200. throw error
  201. }
  202. }
  203. }
  204. /// Update (Bidirectional Streaming)
  205. public class Echo_EchoUpdateCall {
  206. private var call : Call
  207. /// Create a call.
  208. fileprivate init(_ channel: Channel) {
  209. self.call = channel.makeCall("/echo.Echo/Update")
  210. }
  211. /// Call this to start a call. Nonblocking.
  212. fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
  213. throws -> Echo_EchoUpdateCall {
  214. try self.call.start(.bidiStreaming, metadata:metadata, completion:completion)
  215. return self
  216. }
  217. /// Call this to wait for a result. Blocking.
  218. public func receive() throws -> Echo_EchoResponse {
  219. var returnError : Echo_EchoClientError?
  220. var returnMessage : Echo_EchoResponse!
  221. let sem = DispatchSemaphore(value: 0)
  222. do {
  223. try receive() {response, error in
  224. returnMessage = response
  225. returnError = error
  226. sem.signal()
  227. }
  228. _ = sem.wait(timeout: DispatchTime.distantFuture)
  229. }
  230. if let returnError = returnError {
  231. throw returnError
  232. }
  233. return returnMessage
  234. }
  235. /// Call this to wait for a result. Nonblocking.
  236. public func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  237. do {
  238. try call.receiveMessage() {(data) in
  239. if let data = data {
  240. if let returnMessage = try? Echo_EchoResponse(protobuf:data) {
  241. completion(returnMessage, nil)
  242. } else {
  243. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  244. }
  245. } else {
  246. completion(nil, Echo_EchoClientError.endOfStream)
  247. }
  248. }
  249. }
  250. }
  251. /// Call this to send each message in the request stream.
  252. public func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
  253. let messageData = try message.serializeProtobuf()
  254. try call.sendMessage(data:messageData, errorHandler:errorHandler)
  255. }
  256. /// Call this to close the sending connection. Blocking.
  257. public func closeSend() throws {
  258. let sem = DispatchSemaphore(value: 0)
  259. try closeSend() {
  260. sem.signal()
  261. }
  262. _ = sem.wait(timeout: DispatchTime.distantFuture)
  263. }
  264. /// Call this to close the sending connection. Nonblocking.
  265. public func closeSend(completion:@escaping ()->()) throws {
  266. try call.close() {
  267. completion()
  268. }
  269. }
  270. }
  271. /// Call methods of this class to make API calls.
  272. public class Echo_EchoService {
  273. private var channel: Channel
  274. /// This metadata will be sent with all requests.
  275. public var metadata : Metadata
  276. /// This property allows the service host name to be overridden.
  277. /// For example, it can be used to make calls to "localhost:8080"
  278. /// appear to be to "example.com".
  279. public var host : String {
  280. get {
  281. return self.channel.host
  282. }
  283. set {
  284. self.channel.host = newValue
  285. }
  286. }
  287. /// Create a client that makes insecure connections.
  288. public init(address: String) {
  289. gRPC.initialize()
  290. channel = Channel(address:address)
  291. metadata = Metadata()
  292. }
  293. /// Create a client that makes secure connections.
  294. public init(address: String, certificates: String?, host: String?) {
  295. gRPC.initialize()
  296. channel = Channel(address:address, certificates:certificates, host:host)
  297. metadata = Metadata()
  298. }
  299. /// Synchronous. Unary.
  300. public func get(_ request: Echo_EchoRequest)
  301. throws
  302. -> Echo_EchoResponse {
  303. return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
  304. }
  305. /// Asynchronous. Unary.
  306. public func get(_ request: Echo_EchoRequest,
  307. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  308. throws
  309. -> Echo_EchoGetCall {
  310. return try Echo_EchoGetCall(channel).start(request:request,
  311. metadata:metadata,
  312. completion:completion)
  313. }
  314. /// Asynchronous. Server-streaming.
  315. /// Send the initial message.
  316. /// Use methods on the returned object to get streamed responses.
  317. public func expand(_ request: Echo_EchoRequest, completion: @escaping (CallResult)->())
  318. throws
  319. -> Echo_EchoExpandCall {
  320. return try Echo_EchoExpandCall(channel).start(request:request, metadata:metadata, completion:completion)
  321. }
  322. /// Asynchronous. Client-streaming.
  323. /// Use methods on the returned object to stream messages and
  324. /// to close the connection and wait for a final response.
  325. public func collect(completion: @escaping (CallResult)->())
  326. throws
  327. -> Echo_EchoCollectCall {
  328. return try Echo_EchoCollectCall(channel).start(metadata:metadata, completion:completion)
  329. }
  330. /// Asynchronous. Bidirectional-streaming.
  331. /// Use methods on the returned object to stream messages,
  332. /// to wait for replies, and to close the connection.
  333. public func update(completion: @escaping (CallResult)->())
  334. throws
  335. -> Echo_EchoUpdateCall {
  336. return try Echo_EchoUpdateCall(channel).start(metadata:metadata, completion:completion)
  337. }
  338. }