echo.client.pb.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: echo.proto
  6. *
  7. */
  8. /*
  9. * Copyright 2017, gRPC Authors All rights reserved.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. import Foundation
  24. import Dispatch
  25. import gRPC
  26. /// Type for errors thrown from generated client code.
  27. internal enum Echo_EchoClientError : Error {
  28. case endOfStream
  29. case invalidMessageReceived
  30. case error(c: CallResult)
  31. }
  32. /// Get (Unary)
  33. internal class Echo_EchoGetCall {
  34. private var call : Call
  35. /// Create a call.
  36. fileprivate init(_ channel: Channel) {
  37. self.call = channel.makeCall("/echo.Echo/Get")
  38. }
  39. /// Run the call. Blocks until the reply is received.
  40. fileprivate func run(request: Echo_EchoRequest,
  41. metadata: Metadata) throws -> Echo_EchoResponse {
  42. let sem = DispatchSemaphore(value: 0)
  43. var returnCallResult : CallResult!
  44. var returnResponse : Echo_EchoResponse?
  45. _ = try start(request:request, metadata:metadata) {response, callResult in
  46. returnResponse = response
  47. returnCallResult = callResult
  48. sem.signal()
  49. }
  50. _ = sem.wait(timeout: DispatchTime.distantFuture)
  51. if let returnResponse = returnResponse {
  52. return returnResponse
  53. } else {
  54. throw Echo_EchoClientError.error(c: returnCallResult)
  55. }
  56. }
  57. /// Start the call. Nonblocking.
  58. fileprivate func start(request: Echo_EchoRequest,
  59. metadata: Metadata,
  60. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  61. throws -> Echo_EchoGetCall {
  62. let requestData = try request.serializedData()
  63. try call.start(.unary,
  64. metadata:metadata,
  65. message:requestData)
  66. {(callResult) in
  67. if let responseData = callResult.resultData,
  68. let response = try? Echo_EchoResponse(serializedData:responseData) {
  69. completion(response, callResult)
  70. } else {
  71. completion(nil, callResult)
  72. }
  73. }
  74. return self
  75. }
  76. }
  77. /// Expand (Server Streaming)
  78. internal class Echo_EchoExpandCall {
  79. private var call : Call
  80. /// Create a call.
  81. fileprivate init(_ channel: Channel) {
  82. self.call = channel.makeCall("/echo.Echo/Expand")
  83. }
  84. /// Call this once with the message to send. Nonblocking.
  85. fileprivate func start(request: Echo_EchoRequest,
  86. metadata: Metadata,
  87. completion: @escaping (CallResult) -> ())
  88. throws -> Echo_EchoExpandCall {
  89. let requestData = try request.serializedData()
  90. try call.start(.serverStreaming,
  91. metadata:metadata,
  92. message:requestData,
  93. completion:completion)
  94. return self
  95. }
  96. /// Call this to wait for a result. Blocking.
  97. internal func receive() throws -> Echo_EchoResponse {
  98. var returnError : Echo_EchoClientError?
  99. var returnResponse : Echo_EchoResponse!
  100. let sem = DispatchSemaphore(value: 0)
  101. do {
  102. try receive() {response, error in
  103. returnResponse = response
  104. returnError = error
  105. sem.signal()
  106. }
  107. _ = sem.wait(timeout: DispatchTime.distantFuture)
  108. }
  109. if let returnError = returnError {
  110. throw returnError
  111. }
  112. return returnResponse
  113. }
  114. /// Call this to wait for a result. Nonblocking.
  115. internal func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  116. do {
  117. try call.receiveMessage() {(responseData) in
  118. if let responseData = responseData {
  119. if let response = try? Echo_EchoResponse(serializedData:responseData) {
  120. completion(response, nil)
  121. } else {
  122. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  123. }
  124. } else {
  125. completion(nil, Echo_EchoClientError.endOfStream)
  126. }
  127. }
  128. }
  129. }
  130. }
  131. /// Collect (Client Streaming)
  132. internal class Echo_EchoCollectCall {
  133. private var call : Call
  134. /// Create a call.
  135. fileprivate init(_ channel: Channel) {
  136. self.call = channel.makeCall("/echo.Echo/Collect")
  137. }
  138. /// Call this to start a call. Nonblocking.
  139. fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
  140. throws -> Echo_EchoCollectCall {
  141. try self.call.start(.clientStreaming, metadata:metadata, completion:completion)
  142. return self
  143. }
  144. /// Call this to send each message in the request stream. Nonblocking.
  145. internal func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
  146. let messageData = try message.serializedData()
  147. try call.sendMessage(data:messageData, errorHandler:errorHandler)
  148. }
  149. /// Call this to close the connection and wait for a response. Blocking.
  150. internal func closeAndReceive() throws -> Echo_EchoResponse {
  151. var returnError : Echo_EchoClientError?
  152. var returnResponse : Echo_EchoResponse!
  153. let sem = DispatchSemaphore(value: 0)
  154. do {
  155. try closeAndReceive() {response, error in
  156. returnResponse = response
  157. returnError = error
  158. sem.signal()
  159. }
  160. _ = sem.wait(timeout: DispatchTime.distantFuture)
  161. } catch (let error) {
  162. throw error
  163. }
  164. if let returnError = returnError {
  165. throw returnError
  166. }
  167. return returnResponse
  168. }
  169. /// Call this to close the connection and wait for a response. Nonblocking.
  170. internal func closeAndReceive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->())
  171. throws {
  172. do {
  173. try call.receiveMessage() {(responseData) in
  174. if let responseData = responseData,
  175. let response = try? Echo_EchoResponse(serializedData:responseData) {
  176. completion(response, nil)
  177. } else {
  178. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  179. }
  180. }
  181. try call.close(completion:{})
  182. } catch (let error) {
  183. throw error
  184. }
  185. }
  186. }
  187. /// Update (Bidirectional Streaming)
  188. internal class Echo_EchoUpdateCall {
  189. private var call : Call
  190. /// Create a call.
  191. fileprivate init(_ channel: Channel) {
  192. self.call = channel.makeCall("/echo.Echo/Update")
  193. }
  194. /// Call this to start a call. Nonblocking.
  195. fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
  196. throws -> Echo_EchoUpdateCall {
  197. try self.call.start(.bidiStreaming, metadata:metadata, completion:completion)
  198. return self
  199. }
  200. /// Call this to wait for a result. Blocking.
  201. internal func receive() throws -> Echo_EchoResponse {
  202. var returnError : Echo_EchoClientError?
  203. var returnMessage : Echo_EchoResponse!
  204. let sem = DispatchSemaphore(value: 0)
  205. do {
  206. try receive() {response, error in
  207. returnMessage = response
  208. returnError = error
  209. sem.signal()
  210. }
  211. _ = sem.wait(timeout: DispatchTime.distantFuture)
  212. }
  213. if let returnError = returnError {
  214. throw returnError
  215. }
  216. return returnMessage
  217. }
  218. /// Call this to wait for a result. Nonblocking.
  219. internal func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  220. do {
  221. try call.receiveMessage() {(data) in
  222. if let data = data {
  223. if let returnMessage = try? Echo_EchoResponse(serializedData:data) {
  224. completion(returnMessage, nil)
  225. } else {
  226. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  227. }
  228. } else {
  229. completion(nil, Echo_EchoClientError.endOfStream)
  230. }
  231. }
  232. }
  233. }
  234. /// Call this to send each message in the request stream.
  235. internal func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
  236. let messageData = try message.serializedData()
  237. try call.sendMessage(data:messageData, errorHandler:errorHandler)
  238. }
  239. /// Call this to close the sending connection. Blocking.
  240. internal func closeSend() throws {
  241. let sem = DispatchSemaphore(value: 0)
  242. try closeSend() {
  243. sem.signal()
  244. }
  245. _ = sem.wait(timeout: DispatchTime.distantFuture)
  246. }
  247. /// Call this to close the sending connection. Nonblocking.
  248. internal func closeSend(completion:@escaping ()->()) throws {
  249. try call.close() {
  250. completion()
  251. }
  252. }
  253. }
  254. /// Call methods of this class to make API calls.
  255. internal class Echo_EchoService {
  256. private var channel: Channel
  257. /// This metadata will be sent with all requests.
  258. internal var metadata : Metadata
  259. /// This property allows the service host name to be overridden.
  260. /// For example, it can be used to make calls to "localhost:8080"
  261. /// appear to be to "example.com".
  262. internal var host : String {
  263. get {
  264. return self.channel.host
  265. }
  266. set {
  267. self.channel.host = newValue
  268. }
  269. }
  270. /// Create a client that makes insecure connections.
  271. internal init(address: String) {
  272. gRPC.initialize()
  273. channel = Channel(address:address)
  274. metadata = Metadata()
  275. }
  276. /// Create a client that makes secure connections.
  277. internal init(address: String, certificates: String?, host: String?) {
  278. gRPC.initialize()
  279. channel = Channel(address:address, certificates:certificates, host:host)
  280. metadata = Metadata()
  281. }
  282. /// Synchronous. Unary.
  283. internal func get(_ request: Echo_EchoRequest)
  284. throws
  285. -> Echo_EchoResponse {
  286. return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
  287. }
  288. /// Asynchronous. Unary.
  289. internal func get(_ request: Echo_EchoRequest,
  290. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  291. throws
  292. -> Echo_EchoGetCall {
  293. return try Echo_EchoGetCall(channel).start(request:request,
  294. metadata:metadata,
  295. completion:completion)
  296. }
  297. /// Asynchronous. Server-streaming.
  298. /// Send the initial message.
  299. /// Use methods on the returned object to get streamed responses.
  300. internal func expand(_ request: Echo_EchoRequest, completion: @escaping (CallResult)->())
  301. throws
  302. -> Echo_EchoExpandCall {
  303. return try Echo_EchoExpandCall(channel).start(request:request, metadata:metadata, completion:completion)
  304. }
  305. /// Asynchronous. Client-streaming.
  306. /// Use methods on the returned object to stream messages and
  307. /// to close the connection and wait for a final response.
  308. internal func collect(completion: @escaping (CallResult)->())
  309. throws
  310. -> Echo_EchoCollectCall {
  311. return try Echo_EchoCollectCall(channel).start(metadata:metadata, completion:completion)
  312. }
  313. /// Asynchronous. Bidirectional-streaming.
  314. /// Use methods on the returned object to stream messages,
  315. /// to wait for replies, and to close the connection.
  316. internal func update(completion: @escaping (CallResult)->())
  317. throws
  318. -> Echo_EchoUpdateCall {
  319. return try Echo_EchoUpdateCall(channel).start(metadata:metadata, completion:completion)
  320. }
  321. }