echo.client.pb.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 callResult : CallResult!
  61. var response : Echo_EchoResponse?
  62. let requestData = try request.serializeProtobuf()
  63. try call.start(.unary,
  64. metadata:metadata,
  65. message:requestData)
  66. {(_callResult) in
  67. callResult = _callResult
  68. if let responseData = callResult.resultData {
  69. response = try? Echo_EchoResponse(protobuf:responseData)
  70. }
  71. sem.signal()
  72. }
  73. _ = sem.wait(timeout: DispatchTime.distantFuture)
  74. if let response = response {
  75. return response
  76. } else {
  77. throw Echo_EchoClientError.error(c: callResult)
  78. }
  79. }
  80. /// Start the call. Nonblocking.
  81. fileprivate func start(request: Echo_EchoRequest,
  82. metadata: Metadata,
  83. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  84. throws -> Echo_EchoGetCall {
  85. let requestData = try request.serializeProtobuf()
  86. try call.start(.unary,
  87. metadata:metadata,
  88. message:requestData)
  89. {(callResult) in
  90. if let responseData = callResult.resultData,
  91. let response = try? Echo_EchoResponse(protobuf:responseData) {
  92. completion(response, callResult)
  93. } else {
  94. completion(nil, callResult)
  95. }
  96. }
  97. return self
  98. }
  99. }
  100. /// Expand (Server Streaming)
  101. public class Echo_EchoExpandCall {
  102. private var call : Call
  103. /// Create a call.
  104. fileprivate init(_ channel: Channel) {
  105. self.call = channel.makeCall("/echo.Echo/Expand")
  106. }
  107. /// Call this once with the message to send.
  108. fileprivate func start(request: Echo_EchoRequest,
  109. metadata: Metadata,
  110. completion: @escaping (CallResult) -> ())
  111. throws -> Echo_EchoExpandCall {
  112. let requestData = try request.serializeProtobuf()
  113. try call.start(.serverStreaming,
  114. metadata:metadata,
  115. message:requestData,
  116. completion:completion)
  117. return self
  118. }
  119. /// Call this to wait for a result. Blocking.
  120. public func receive() throws -> Echo_EchoResponse {
  121. var returnError : Echo_EchoClientError?
  122. var response : Echo_EchoResponse!
  123. let sem = DispatchSemaphore(value: 0)
  124. do {
  125. try call.receiveMessage() {(responseData) in
  126. if let responseData = responseData {
  127. response = try? Echo_EchoResponse(protobuf:responseData)
  128. if response == nil {
  129. returnError = Echo_EchoClientError.invalidMessageReceived
  130. }
  131. } else {
  132. returnError = Echo_EchoClientError.endOfStream
  133. }
  134. sem.signal()
  135. }
  136. _ = sem.wait(timeout: DispatchTime.distantFuture)
  137. }
  138. if let returnError = returnError {
  139. throw returnError
  140. }
  141. return response
  142. }
  143. /// Call this to wait for a result. Nonblocking.
  144. public func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  145. do {
  146. try call.receiveMessage() {(responseData) in
  147. if let responseData = responseData {
  148. if let response = try? Echo_EchoResponse(protobuf:responseData) {
  149. completion(response, nil)
  150. } else {
  151. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  152. }
  153. } else {
  154. completion(nil, Echo_EchoClientError.endOfStream)
  155. }
  156. }
  157. }
  158. }
  159. }
  160. /// Collect (Client Streaming)
  161. public class Echo_EchoCollectCall {
  162. private var call : Call
  163. /// Create a call.
  164. fileprivate init(_ channel: Channel) {
  165. self.call = channel.makeCall("/echo.Echo/Collect")
  166. }
  167. /// Call this to start a call.
  168. fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
  169. throws -> Echo_EchoCollectCall {
  170. try self.call.start(.clientStreaming, metadata:metadata, completion:completion)
  171. return self
  172. }
  173. /// Call this to send each message in the request stream.
  174. public func send(_ message: Echo_EchoRequest) throws {
  175. let messageData = try message.serializeProtobuf()
  176. try call.sendMessage(data:messageData)
  177. }
  178. /// Call this to close the connection and wait for a response. Blocking.
  179. public func closeAndReceive() throws -> Echo_EchoResponse {
  180. var returnError : Echo_EchoClientError?
  181. var returnResponse : Echo_EchoResponse!
  182. let sem = DispatchSemaphore(value: 0)
  183. do {
  184. try call.receiveMessage() {(responseData) in
  185. if let responseData = responseData,
  186. let response = try? Echo_EchoResponse(protobuf:responseData) {
  187. returnResponse = response
  188. } else {
  189. returnError = Echo_EchoClientError.invalidMessageReceived
  190. }
  191. sem.signal()
  192. }
  193. try call.close(completion:{})
  194. _ = sem.wait(timeout: DispatchTime.distantFuture)
  195. } catch (let error) {
  196. throw error
  197. }
  198. if let returnError = returnError {
  199. throw returnError
  200. }
  201. return returnResponse
  202. }
  203. /// Call this to close the connection and wait for a response. Nonblocking.
  204. public func closeAndReceive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->())
  205. throws {
  206. do {
  207. try call.receiveMessage() {(responseData) in
  208. if let responseData = responseData,
  209. let response = try? Echo_EchoResponse(protobuf:responseData) {
  210. completion(response, nil)
  211. } else {
  212. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  213. }
  214. }
  215. try call.close(completion:{})
  216. } catch (let error) {
  217. throw error
  218. }
  219. }
  220. }
  221. /// Update (Bidirectional Streaming)
  222. public class Echo_EchoUpdateCall {
  223. private var call : Call
  224. /// Create a call.
  225. fileprivate init(_ channel: Channel) {
  226. self.call = channel.makeCall("/echo.Echo/Update")
  227. }
  228. /// Call this to start a call. Nonblocking.
  229. fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
  230. throws -> Echo_EchoUpdateCall {
  231. try self.call.start(.bidiStreaming, metadata:metadata, completion:completion)
  232. return self
  233. }
  234. /// Call this to wait for a result. Blocks.
  235. public func receive() throws -> Echo_EchoResponse {
  236. var returnError : Echo_EchoClientError?
  237. var returnMessage : Echo_EchoResponse!
  238. let sem = DispatchSemaphore(value: 0)
  239. do {
  240. try call.receiveMessage() {(data) in
  241. if let data = data {
  242. returnMessage = try? Echo_EchoResponse(protobuf:data)
  243. if returnMessage == nil {
  244. returnError = Echo_EchoClientError.invalidMessageReceived
  245. }
  246. } else {
  247. returnError = Echo_EchoClientError.endOfStream
  248. }
  249. sem.signal()
  250. }
  251. _ = sem.wait(timeout: DispatchTime.distantFuture)
  252. }
  253. if let returnError = returnError {
  254. throw returnError
  255. }
  256. return returnMessage
  257. }
  258. /// Call this to wait for a result. Nonblocking.
  259. public func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  260. do {
  261. try call.receiveMessage() {(data) in
  262. if let data = data {
  263. if let returnMessage = try? Echo_EchoResponse(protobuf:data) {
  264. completion(returnMessage, nil)
  265. } else {
  266. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  267. }
  268. } else {
  269. completion(nil, Echo_EchoClientError.endOfStream)
  270. }
  271. }
  272. }
  273. }
  274. /// Call this to send each message in the request stream.
  275. public func send(_ message:Echo_EchoRequest) throws {
  276. let messageData = try message.serializeProtobuf()
  277. try call.sendMessage(data:messageData)
  278. }
  279. /// Call this to close the sending connection. Blocking
  280. public func closeSend() throws {
  281. let sem = DispatchSemaphore(value: 0)
  282. try call.close() {
  283. sem.signal()
  284. }
  285. _ = sem.wait(timeout: DispatchTime.distantFuture)
  286. }
  287. /// Call this to close the sending connection. Nonblocking
  288. public func closeSend(completion:@escaping ()->()) throws {
  289. try call.close() {
  290. completion()
  291. }
  292. }
  293. }
  294. /// Call methods of this class to make API calls.
  295. public class Echo_EchoService {
  296. private var channel: Channel
  297. /// This metadata will be sent with all requests.
  298. public var metadata : Metadata
  299. /// This property allows the service host name to be overridden.
  300. /// For example, it can be used to make calls to "localhost:8080"
  301. /// appear to be to "example.com".
  302. public var host : String {
  303. get {
  304. return self.channel.host
  305. }
  306. set {
  307. self.channel.host = newValue
  308. }
  309. }
  310. /// Create a client that makes insecure connections.
  311. public init(address: String) {
  312. gRPC.initialize()
  313. channel = Channel(address:address)
  314. metadata = Metadata()
  315. }
  316. /// Create a client that makes secure connections.
  317. public init(address: String, certificates: String?, host: String?) {
  318. gRPC.initialize()
  319. channel = Channel(address:address, certificates:certificates, host:host)
  320. metadata = Metadata()
  321. }
  322. /// Synchronous. Unary.
  323. public func get(_ request: Echo_EchoRequest)
  324. throws
  325. -> Echo_EchoResponse {
  326. return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
  327. }
  328. /// Asynchronous. Unary.
  329. public func get(_ request: Echo_EchoRequest,
  330. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  331. throws
  332. -> Echo_EchoGetCall {
  333. return try Echo_EchoGetCall(channel).start(request:request,
  334. metadata:metadata,
  335. completion:completion)
  336. }
  337. /// Asynchronous. Server-streaming.
  338. /// Send the initial message.
  339. /// Use methods on the returned object to get streamed responses.
  340. public func expand(_ request: Echo_EchoRequest, completion: @escaping (CallResult)->())
  341. throws
  342. -> Echo_EchoExpandCall {
  343. return try Echo_EchoExpandCall(channel).start(request:request, metadata:metadata, completion:completion)
  344. }
  345. /// Asynchronous. Client-streaming.
  346. /// Use methods on the returned object to stream messages and
  347. /// to close the connection and wait for a final response.
  348. public func collect(completion: @escaping (CallResult)->())
  349. throws
  350. -> Echo_EchoCollectCall {
  351. return try Echo_EchoCollectCall(channel).start(metadata:metadata, completion:completion)
  352. }
  353. /// Asynchronous. Bidirectional-streaming.
  354. /// Use methods on the returned object to stream messages,
  355. /// to wait for replies, and to close the connection.
  356. public func update(completion: @escaping (CallResult)->())
  357. throws
  358. -> Echo_EchoUpdateCall {
  359. return try Echo_EchoUpdateCall(channel).start(metadata:metadata, completion:completion)
  360. }
  361. }