echo.client.pb.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. }
  81. /// Expand (Server Streaming)
  82. public class Echo_EchoExpandCall {
  83. private var call : Call
  84. /// Create a call.
  85. fileprivate init(_ channel: Channel) {
  86. self.call = channel.makeCall("/echo.Echo/Expand")
  87. }
  88. /// Call this once with the message to send.
  89. fileprivate func run(request: Echo_EchoRequest, metadata: Metadata) throws -> Echo_EchoExpandCall {
  90. let requestData = try request.serializeProtobuf()
  91. let sem = DispatchSemaphore(value: 0)
  92. try call.start(.serverStreaming,
  93. metadata:metadata,
  94. message:requestData)
  95. {callResult in
  96. sem.signal()
  97. }
  98. _ = sem.wait(timeout: DispatchTime.distantFuture)
  99. return self
  100. }
  101. /// Call this to wait for a result. Blocks.
  102. public func receive() throws -> Echo_EchoResponse {
  103. var returnError : Echo_EchoClientError?
  104. var response : Echo_EchoResponse!
  105. let sem = DispatchSemaphore(value: 0)
  106. do {
  107. try call.receiveMessage() {(responseData) in
  108. if let responseData = responseData {
  109. response = try? Echo_EchoResponse(protobuf:responseData)
  110. if response == nil {
  111. returnError = Echo_EchoClientError.invalidMessageReceived
  112. }
  113. } else {
  114. returnError = Echo_EchoClientError.endOfStream
  115. }
  116. sem.signal()
  117. }
  118. _ = sem.wait(timeout: DispatchTime.distantFuture)
  119. }
  120. if let returnError = returnError {
  121. throw returnError
  122. }
  123. return response
  124. }
  125. }
  126. /// Collect (Client Streaming)
  127. public class Echo_EchoCollectCall {
  128. private var call : Call
  129. /// Create a call.
  130. fileprivate init(_ channel: Channel) {
  131. self.call = channel.makeCall("/echo.Echo/Collect")
  132. }
  133. /// Call this to start a call.
  134. fileprivate func run(metadata:Metadata) throws -> Echo_EchoCollectCall {
  135. let sem = DispatchSemaphore(value: 0)
  136. try self.call.start(.clientStreaming,
  137. metadata:metadata)
  138. {callResult in
  139. sem.signal()
  140. }
  141. _ = sem.wait(timeout: DispatchTime.distantFuture)
  142. return self
  143. }
  144. /// Call this to send each message in the request stream.
  145. public func send(_ message: Echo_EchoRequest) throws {
  146. let messageData = try message.serializeProtobuf()
  147. try call.sendMessage(data:messageData)
  148. }
  149. /// Call this to close the connection and wait for a response. Blocks.
  150. public 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 call.receiveMessage() {(responseData) in
  156. if let responseData = responseData,
  157. let response = try? Echo_EchoResponse(protobuf:responseData) {
  158. returnResponse = response
  159. } else {
  160. returnError = Echo_EchoClientError.invalidMessageReceived
  161. }
  162. sem.signal()
  163. }
  164. try call.close(completion:{})
  165. _ = sem.wait(timeout: DispatchTime.distantFuture)
  166. } catch (let error) {
  167. throw error
  168. }
  169. if let returnError = returnError {
  170. throw returnError
  171. }
  172. return returnResponse
  173. }
  174. }
  175. /// Update (Bidirectional Streaming)
  176. public class Echo_EchoUpdateCall {
  177. private var call : Call
  178. /// Create a call.
  179. fileprivate init(_ channel: Channel) {
  180. self.call = channel.makeCall("/echo.Echo/Update")
  181. }
  182. /// Call this to start a call.
  183. fileprivate func run(metadata:Metadata) throws -> Echo_EchoUpdateCall {
  184. let sem = DispatchSemaphore(value: 0)
  185. try self.call.start(.bidiStreaming,
  186. metadata:metadata)
  187. {callResult in
  188. sem.signal()
  189. }
  190. _ = sem.wait(timeout: DispatchTime.distantFuture)
  191. return self
  192. }
  193. /// Call this to wait for a result. Blocks.
  194. public func receive() throws -> Echo_EchoResponse {
  195. var returnError : Echo_EchoClientError?
  196. var returnMessage : Echo_EchoResponse!
  197. let sem = DispatchSemaphore(value: 0)
  198. do {
  199. try call.receiveMessage() {(data) in
  200. if let data = data {
  201. returnMessage = try? Echo_EchoResponse(protobuf:data)
  202. if returnMessage == nil {
  203. returnError = Echo_EchoClientError.invalidMessageReceived
  204. }
  205. } else {
  206. returnError = Echo_EchoClientError.endOfStream
  207. }
  208. sem.signal()
  209. }
  210. _ = sem.wait(timeout: DispatchTime.distantFuture)
  211. }
  212. if let returnError = returnError {
  213. throw returnError
  214. }
  215. return returnMessage
  216. }
  217. /// Call this to send each message in the request stream.
  218. public func send(_ message:Echo_EchoRequest) throws {
  219. let messageData = try message.serializeProtobuf()
  220. try call.sendMessage(data:messageData)
  221. }
  222. /// Call this to close the sending connection.
  223. public func closeSend() throws {
  224. let sem = DispatchSemaphore(value: 0)
  225. try call.close() {
  226. sem.signal()
  227. }
  228. _ = sem.wait(timeout: DispatchTime.distantFuture)
  229. }
  230. }
  231. /// Call methods of this class to make API calls.
  232. public class Echo_EchoService {
  233. private var channel: Channel
  234. /// This metadata will be sent with all requests.
  235. public var metadata : Metadata
  236. /// This property allows the service host name to be overridden.
  237. /// For example, it can be used to make calls to "localhost:8080"
  238. /// appear to be to "example.com".
  239. public var host : String {
  240. get {
  241. return self.channel.host
  242. }
  243. set {
  244. self.channel.host = newValue
  245. }
  246. }
  247. /// Create a client that makes insecure connections.
  248. public init(address: String) {
  249. gRPC.initialize()
  250. channel = Channel(address:address)
  251. metadata = Metadata()
  252. }
  253. /// Create a client that makes secure connections.
  254. public init(address: String, certificates: String?, host: String?) {
  255. gRPC.initialize()
  256. channel = Channel(address:address, certificates:certificates, host:host)
  257. metadata = Metadata()
  258. }
  259. /// Synchronous. Unary.
  260. public func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  261. return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
  262. }
  263. /// Asynchronous. Server-streaming.
  264. /// Send the initial message.
  265. /// Use methods on the returned object to get streamed responses.
  266. public func expand(_ request: Echo_EchoRequest) throws -> Echo_EchoExpandCall {
  267. return try Echo_EchoExpandCall(channel).run(request:request, metadata:metadata)
  268. }
  269. /// Asynchronous. Client-streaming.
  270. /// Use methods on the returned object to stream messages and
  271. /// to close the connection and wait for a final response.
  272. public func collect() throws -> Echo_EchoCollectCall {
  273. return try Echo_EchoCollectCall(channel).run(metadata:metadata)
  274. }
  275. /// Asynchronous. Bidirectional-streaming.
  276. /// Use methods on the returned object to stream messages,
  277. /// to wait for replies, and to close the connection.
  278. public func update() throws -> Echo_EchoUpdateCall {
  279. return try Echo_EchoUpdateCall(channel).run(metadata:metadata)
  280. }
  281. }