echo.client.pb.swift 11 KB

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