echo.grpc.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // DO NOT EDIT.
  3. //
  4. // Generated by the protocol buffer compiler.
  5. // Source: echo.proto
  6. //
  7. //
  8. // Copyright 2018, gRPC Authors All rights reserved.
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. //
  22. import Foundation
  23. import Dispatch
  24. import SwiftGRPC
  25. import SwiftProtobuf
  26. internal protocol Echo_EchoGetCall: ClientCallUnary {}
  27. fileprivate final class Echo_EchoGetCallBase: ClientCallUnaryBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoGetCall {
  28. override class var method: String { return "/echo.Echo/Get" }
  29. }
  30. internal protocol Echo_EchoExpandCall: ClientCallServerStreaming {
  31. /// Do not call this directly, call `receive()` in the protocol extension below instead.
  32. func _receive(timeout: DispatchTime) throws -> Echo_EchoResponse?
  33. /// Call this to wait for a result. Nonblocking.
  34. func receive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse?>) -> Void) throws
  35. }
  36. internal extension Echo_EchoExpandCall {
  37. /// Call this to wait for a result. Blocking.
  38. func receive(timeout: DispatchTime = .distantFuture) throws -> Echo_EchoResponse? { return try self._receive(timeout: timeout) }
  39. }
  40. fileprivate final class Echo_EchoExpandCallBase: ClientCallServerStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandCall {
  41. override class var method: String { return "/echo.Echo/Expand" }
  42. }
  43. class Echo_EchoExpandCallTestStub: ClientCallServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandCall {
  44. override class var method: String { return "/echo.Echo/Expand" }
  45. }
  46. internal protocol Echo_EchoCollectCall: ClientCallClientStreaming {
  47. /// Send a message to the stream. Nonblocking.
  48. func send(_ message: Echo_EchoRequest, completion: @escaping (Error?) -> Void) throws
  49. /// Do not call this directly, call `send()` in the protocol extension below instead.
  50. func _send(_ message: Echo_EchoRequest, timeout: DispatchTime) throws
  51. /// Call this to close the connection and wait for a response. Blocking.
  52. func closeAndReceive() throws -> Echo_EchoResponse
  53. /// Call this to close the connection and wait for a response. Nonblocking.
  54. func closeAndReceive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse>) -> Void) throws
  55. }
  56. internal extension Echo_EchoCollectCall {
  57. /// Send a message to the stream and wait for the send operation to finish. Blocking.
  58. func send(_ message: Echo_EchoRequest, timeout: DispatchTime = .distantFuture) throws { try self._send(message, timeout: timeout) }
  59. }
  60. fileprivate final class Echo_EchoCollectCallBase: ClientCallClientStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
  61. override class var method: String { return "/echo.Echo/Collect" }
  62. }
  63. /// Simple fake implementation of Echo_EchoCollectCall
  64. /// stores sent values for later verification and finall returns a previously-defined result.
  65. class Echo_EchoCollectCallTestStub: ClientCallClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
  66. override class var method: String { return "/echo.Echo/Collect" }
  67. }
  68. internal protocol Echo_EchoUpdateCall: ClientCallBidirectionalStreaming {
  69. /// Do not call this directly, call `receive()` in the protocol extension below instead.
  70. func _receive(timeout: DispatchTime) throws -> Echo_EchoResponse?
  71. /// Call this to wait for a result. Nonblocking.
  72. func receive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse?>) -> Void) throws
  73. /// Send a message to the stream. Nonblocking.
  74. func send(_ message: Echo_EchoRequest, completion: @escaping (Error?) -> Void) throws
  75. /// Do not call this directly, call `send()` in the protocol extension below instead.
  76. func _send(_ message: Echo_EchoRequest, timeout: DispatchTime) throws
  77. /// Call this to close the sending connection. Blocking.
  78. func closeSend() throws
  79. /// Call this to close the sending connection. Nonblocking.
  80. func closeSend(completion: (() -> Void)?) throws
  81. }
  82. internal extension Echo_EchoUpdateCall {
  83. /// Call this to wait for a result. Blocking.
  84. func receive(timeout: DispatchTime = .distantFuture) throws -> Echo_EchoResponse? { return try self._receive(timeout: timeout) }
  85. }
  86. internal extension Echo_EchoUpdateCall {
  87. /// Send a message to the stream and wait for the send operation to finish. Blocking.
  88. func send(_ message: Echo_EchoRequest, timeout: DispatchTime = .distantFuture) throws { try self._send(message, timeout: timeout) }
  89. }
  90. fileprivate final class Echo_EchoUpdateCallBase: ClientCallBidirectionalStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
  91. override class var method: String { return "/echo.Echo/Update" }
  92. }
  93. class Echo_EchoUpdateCallTestStub: ClientCallBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
  94. override class var method: String { return "/echo.Echo/Update" }
  95. }
  96. /// Instantiate Echo_EchoServiceClient, then call methods of this protocol to make API calls.
  97. internal protocol Echo_EchoService: ServiceClient {
  98. /// Synchronous. Unary.
  99. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse
  100. /// Asynchronous. Unary.
  101. func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall
  102. /// Asynchronous. Server-streaming.
  103. /// Send the initial message.
  104. /// Use methods on the returned object to get streamed responses.
  105. func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall
  106. /// Asynchronous. Client-streaming.
  107. /// Use methods on the returned object to stream messages and
  108. /// to close the connection and wait for a final response.
  109. func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall
  110. /// Asynchronous. Bidirectional-streaming.
  111. /// Use methods on the returned object to stream messages,
  112. /// to wait for replies, and to close the connection.
  113. func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall
  114. }
  115. internal final class Echo_EchoServiceClient: ServiceClientBase, Echo_EchoService {
  116. /// Synchronous. Unary.
  117. internal func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  118. return try Echo_EchoGetCallBase(channel)
  119. .run(request: request, metadata: metadata)
  120. }
  121. /// Asynchronous. Unary.
  122. internal func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
  123. return try Echo_EchoGetCallBase(channel)
  124. .start(request: request, metadata: metadata, completion: completion)
  125. }
  126. /// Asynchronous. Server-streaming.
  127. /// Send the initial message.
  128. /// Use methods on the returned object to get streamed responses.
  129. internal func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
  130. return try Echo_EchoExpandCallBase(channel)
  131. .start(request: request, metadata: metadata, completion: completion)
  132. }
  133. /// Asynchronous. Client-streaming.
  134. /// Use methods on the returned object to stream messages and
  135. /// to close the connection and wait for a final response.
  136. internal func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
  137. return try Echo_EchoCollectCallBase(channel)
  138. .start(metadata: metadata, completion: completion)
  139. }
  140. /// Asynchronous. Bidirectional-streaming.
  141. /// Use methods on the returned object to stream messages,
  142. /// to wait for replies, and to close the connection.
  143. internal func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
  144. return try Echo_EchoUpdateCallBase(channel)
  145. .start(metadata: metadata, completion: completion)
  146. }
  147. }
  148. class Echo_EchoServiceTestStub: ServiceClientTestStubBase, Echo_EchoService {
  149. var getRequests: [Echo_EchoRequest] = []
  150. var getResponses: [Echo_EchoResponse] = []
  151. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  152. getRequests.append(request)
  153. defer { getResponses.removeFirst() }
  154. return getResponses.first!
  155. }
  156. func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
  157. fatalError("not implemented")
  158. }
  159. var expandRequests: [Echo_EchoRequest] = []
  160. var expandCalls: [Echo_EchoExpandCall] = []
  161. func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
  162. expandRequests.append(request)
  163. defer { expandCalls.removeFirst() }
  164. return expandCalls.first!
  165. }
  166. var collectCalls: [Echo_EchoCollectCall] = []
  167. func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
  168. defer { collectCalls.removeFirst() }
  169. return collectCalls.first!
  170. }
  171. var updateCalls: [Echo_EchoUpdateCall] = []
  172. func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
  173. defer { updateCalls.removeFirst() }
  174. return updateCalls.first!
  175. }
  176. }
  177. /// To build a server, implement a class that conforms to this protocol.
  178. /// If one of the methods returning `ServerStatus?` returns nil,
  179. /// it is expected that you have already returned a status to the client by means of `session.close`.
  180. internal protocol Echo_EchoProvider {
  181. func get(request: Echo_EchoRequest, session: Echo_EchoGetSession) throws -> Echo_EchoResponse
  182. func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws -> ServerStatus?
  183. func collect(session: Echo_EchoCollectSession) throws -> Echo_EchoResponse?
  184. func update(session: Echo_EchoUpdateSession) throws -> ServerStatus?
  185. }
  186. internal protocol Echo_EchoGetSession: ServerSessionUnary {}
  187. fileprivate final class Echo_EchoGetSessionBase: ServerSessionUnaryBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoGetSession {}
  188. class Echo_EchoGetSessionTestStub: ServerSessionUnaryTestStub, Echo_EchoGetSession {}
  189. internal protocol Echo_EchoExpandSession: ServerSessionServerStreaming {
  190. /// Send a message to the stream. Nonblocking.
  191. func send(_ message: Echo_EchoResponse, completion: @escaping (Error?) -> Void) throws
  192. /// Do not call this directly, call `send()` in the protocol extension below instead.
  193. func _send(_ message: Echo_EchoResponse, timeout: DispatchTime) throws
  194. /// Close the connection and send the status. Non-blocking.
  195. /// This method should be called if and only if your request handler returns a nil value instead of a server status;
  196. /// otherwise SwiftGRPC will take care of sending the status for you.
  197. func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws
  198. }
  199. internal extension Echo_EchoExpandSession {
  200. /// Send a message to the stream and wait for the send operation to finish. Blocking.
  201. func send(_ message: Echo_EchoResponse, timeout: DispatchTime = .distantFuture) throws { try self._send(message, timeout: timeout) }
  202. }
  203. fileprivate final class Echo_EchoExpandSessionBase: ServerSessionServerStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandSession {}
  204. class Echo_EchoExpandSessionTestStub: ServerSessionServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandSession {}
  205. internal protocol Echo_EchoCollectSession: ServerSessionClientStreaming {
  206. /// Do not call this directly, call `receive()` in the protocol extension below instead.
  207. func _receive(timeout: DispatchTime) throws -> Echo_EchoRequest?
  208. /// Call this to wait for a result. Nonblocking.
  209. func receive(completion: @escaping (ResultOrRPCError<Echo_EchoRequest?>) -> Void) throws
  210. /// Exactly one of these two methods should be called if and only if your request handler returns nil;
  211. /// otherwise SwiftGRPC will take care of sending the response and status for you.
  212. /// Close the connection and send a single result. Non-blocking.
  213. func sendAndClose(response: Echo_EchoResponse, status: ServerStatus, completion: (() -> Void)?) throws
  214. /// Close the connection and send an error. Non-blocking.
  215. /// Use this method if you encountered an error that makes it impossible to send a response.
  216. /// Accordingly, it does not make sense to call this method with a status of `.ok`.
  217. func sendErrorAndClose(status: ServerStatus, completion: (() -> Void)?) throws
  218. }
  219. internal extension Echo_EchoCollectSession {
  220. /// Call this to wait for a result. Blocking.
  221. func receive(timeout: DispatchTime = .distantFuture) throws -> Echo_EchoRequest? { return try self._receive(timeout: timeout) }
  222. }
  223. fileprivate final class Echo_EchoCollectSessionBase: ServerSessionClientStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession {}
  224. class Echo_EchoCollectSessionTestStub: ServerSessionClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession {}
  225. internal protocol Echo_EchoUpdateSession: ServerSessionBidirectionalStreaming {
  226. /// Do not call this directly, call `receive()` in the protocol extension below instead.
  227. func _receive(timeout: DispatchTime) throws -> Echo_EchoRequest?
  228. /// Call this to wait for a result. Nonblocking.
  229. func receive(completion: @escaping (ResultOrRPCError<Echo_EchoRequest?>) -> Void) throws
  230. /// Send a message to the stream. Nonblocking.
  231. func send(_ message: Echo_EchoResponse, completion: @escaping (Error?) -> Void) throws
  232. /// Do not call this directly, call `send()` in the protocol extension below instead.
  233. func _send(_ message: Echo_EchoResponse, timeout: DispatchTime) throws
  234. /// Close the connection and send the status. Non-blocking.
  235. /// This method should be called if and only if your request handler returns a nil value instead of a server status;
  236. /// otherwise SwiftGRPC will take care of sending the status for you.
  237. func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws
  238. }
  239. internal extension Echo_EchoUpdateSession {
  240. /// Call this to wait for a result. Blocking.
  241. func receive(timeout: DispatchTime = .distantFuture) throws -> Echo_EchoRequest? { return try self._receive(timeout: timeout) }
  242. }
  243. internal extension Echo_EchoUpdateSession {
  244. /// Send a message to the stream and wait for the send operation to finish. Blocking.
  245. func send(_ message: Echo_EchoResponse, timeout: DispatchTime = .distantFuture) throws { try self._send(message, timeout: timeout) }
  246. }
  247. fileprivate final class Echo_EchoUpdateSessionBase: ServerSessionBidirectionalStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateSession {}
  248. class Echo_EchoUpdateSessionTestStub: ServerSessionBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateSession {}
  249. /// Main server for generated service
  250. internal final class Echo_EchoServer: ServiceServer {
  251. private let provider: Echo_EchoProvider
  252. internal init(address: String, provider: Echo_EchoProvider) {
  253. self.provider = provider
  254. super.init(address: address)
  255. }
  256. internal init?(address: String, certificateURL: URL, keyURL: URL, rootCertsURL: URL? = nil, provider: Echo_EchoProvider) {
  257. self.provider = provider
  258. super.init(address: address, certificateURL: certificateURL, keyURL: keyURL, rootCertsURL: rootCertsURL)
  259. }
  260. internal init?(address: String, certificateString: String, keyString: String, rootCerts: String? = nil, provider: Echo_EchoProvider) {
  261. self.provider = provider
  262. super.init(address: address, certificateString: certificateString, keyString: keyString, rootCerts: rootCerts)
  263. }
  264. /// Determines and calls the appropriate request handler, depending on the request's method.
  265. /// Throws `HandleMethodError.unknownMethod` for methods not handled by this service.
  266. internal override func handleMethod(_ method: String, handler: Handler) throws -> ServerStatus? {
  267. let provider = self.provider
  268. switch method {
  269. case "/echo.Echo/Get":
  270. return try Echo_EchoGetSessionBase(
  271. handler: handler,
  272. providerBlock: { try provider.get(request: $0, session: $1 as! Echo_EchoGetSessionBase) })
  273. .run()
  274. case "/echo.Echo/Expand":
  275. return try Echo_EchoExpandSessionBase(
  276. handler: handler,
  277. providerBlock: { try provider.expand(request: $0, session: $1 as! Echo_EchoExpandSessionBase) })
  278. .run()
  279. case "/echo.Echo/Collect":
  280. return try Echo_EchoCollectSessionBase(
  281. handler: handler,
  282. providerBlock: { try provider.collect(session: $0 as! Echo_EchoCollectSessionBase) })
  283. .run()
  284. case "/echo.Echo/Update":
  285. return try Echo_EchoUpdateSessionBase(
  286. handler: handler,
  287. providerBlock: { try provider.update(session: $0 as! Echo_EchoUpdateSessionBase) })
  288. .run()
  289. default:
  290. throw HandleMethodError.unknownMethod
  291. }
  292. }
  293. }