client.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //-{% for service in file.services %}
  2. /// Type for errors thrown from generated client code.
  3. {{ access }} enum {{ .|clienterror:file,service }} : Error {
  4. case endOfStream
  5. case invalidMessageReceived
  6. case error(c: CallResult)
  7. }
  8. //-{% for method in service.methods %}
  9. //-{% if method|methodIsUnary %}
  10. //-{% include "client-call-unary.swift" %}
  11. //-{% endif %}
  12. //-{% if method|methodIsServerStreaming %}
  13. //-{% include "client-call-serverstreaming.swift" %}
  14. //-{% endif %}
  15. //-{% if method|methodIsClientStreaming %}
  16. //-{% include "client-call-clientstreaming.swift" %}
  17. //-{% endif %}
  18. //-{% if method|methodIsBidiStreaming %}
  19. //-{% include "client-call-bidistreaming.swift" %}
  20. //-{% endif %}
  21. //-{% endfor %}
  22. /// Instantiate {{ .|serviceclass:file,service }}Impl, then call methods of this protocol to make API calls.
  23. {{ access }} protocol {{ .|serviceclass:file,service }} {
  24. var channel: Channel { get }
  25. /// This metadata will be sent with all requests.
  26. var metadata: Metadata { get }
  27. /// This property allows the service host name to be overridden.
  28. /// For example, it can be used to make calls to "localhost:8080"
  29. /// appear to be to "example.com".
  30. var host : String { get }
  31. /// This property allows the service timeout to be overridden.
  32. var timeout : TimeInterval { get }
  33. //-{% for method in service.methods %}
  34. //-{% if method|methodIsUnary %}
  35. /// Synchronous. Unary.
  36. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }}
  37. /// Asynchronous. Unary.
  38. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
  39. completion: @escaping ({{ method|output }}?, CallResult)->()) throws -> {{ .|call:file,service,method }}
  40. //-{% endif %}
  41. //-{% if method|methodIsServerStreaming %}
  42. /// Asynchronous. Server-streaming.
  43. /// Send the initial message.
  44. /// Use methods on the returned object to get streamed responses.
  45. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping (CallResult)->())
  46. throws -> {{ .|call:file,service,method }}
  47. //-{% endif %}
  48. //-{% if method|methodIsClientStreaming %}
  49. /// Asynchronous. Client-streaming.
  50. /// Use methods on the returned object to stream messages and
  51. /// to close the connection and wait for a final response.
  52. func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  53. throws -> {{ .|call:file,service,method }}
  54. //-{% endif %}
  55. //-{% if method|methodIsBidiStreaming %}
  56. /// Asynchronous. Bidirectional-streaming.
  57. /// Use methods on the returned object to stream messages,
  58. /// to wait for replies, and to close the connection.
  59. func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  60. throws -> {{ .|call:file,service,method }}
  61. //-{% endif %}
  62. //-{% endfor %}
  63. }
  64. {{ access }} final class {{ .|serviceclass:file,service }}Client: {{ .|serviceclass:file,service }} {
  65. {{ access }} private(set) var channel: Channel
  66. {{ access }} var metadata : Metadata
  67. {{ access }} var host : String {
  68. get {
  69. return self.channel.host
  70. }
  71. set {
  72. self.channel.host = newValue
  73. }
  74. }
  75. {{ access }} var timeout : TimeInterval {
  76. get {
  77. return self.channel.timeout
  78. }
  79. set {
  80. self.channel.timeout = newValue
  81. }
  82. }
  83. /// Create a client.
  84. {{ access }} init(address: String, secure: Bool = true) {
  85. gRPC.initialize()
  86. channel = Channel(address:address, secure:secure)
  87. metadata = Metadata()
  88. }
  89. /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
  90. {{ access }} init(address: String, certificates: String, host: String?) {
  91. gRPC.initialize()
  92. channel = Channel(address:address, certificates:certificates, host:host)
  93. metadata = Metadata()
  94. }
  95. //-{% for method in service.methods %}
  96. //-{% if method|methodIsUnary %}
  97. /// Synchronous. Unary.
  98. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }})
  99. throws
  100. -> {{ method|output }} {
  101. return try {{ .|call:file,service,method }}Impl(channel).run(request:request, metadata:metadata)
  102. }
  103. /// Asynchronous. Unary.
  104. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
  105. completion: @escaping ({{ method|output }}?, CallResult)->())
  106. throws
  107. -> {{ .|call:file,service,method }} {
  108. return try {{ .|call:file,service,method }}Impl(channel).start(request:request,
  109. metadata:metadata,
  110. completion:completion)
  111. }
  112. //-{% endif %}
  113. //-{% if method|methodIsServerStreaming %}
  114. /// Asynchronous. Server-streaming.
  115. /// Send the initial message.
  116. /// Use methods on the returned object to get streamed responses.
  117. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping (CallResult)->())
  118. throws
  119. -> {{ .|call:file,service,method }} {
  120. return try {{ .|call:file,service,method }}Impl(channel).start(request:request, metadata:metadata, completion:completion)
  121. }
  122. //-{% endif %}
  123. //-{% if method|methodIsClientStreaming %}
  124. /// Asynchronous. Client-streaming.
  125. /// Use methods on the returned object to stream messages and
  126. /// to close the connection and wait for a final response.
  127. {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  128. throws
  129. -> {{ .|call:file,service,method }} {
  130. return try {{ .|call:file,service,method }}Impl(channel).start(metadata:metadata, completion:completion)
  131. }
  132. //-{% endif %}
  133. //-{% if method|methodIsBidiStreaming %}
  134. /// Asynchronous. Bidirectional-streaming.
  135. /// Use methods on the returned object to stream messages,
  136. /// to wait for replies, and to close the connection.
  137. {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  138. throws
  139. -> {{ .|call:file,service,method }} {
  140. return try {{ .|call:file,service,method }}Impl(channel).start(metadata:metadata, completion:completion)
  141. }
  142. //-{% endif %}
  143. //-{% endfor %}
  144. }
  145. //-{% if generateTestStubs %}
  146. /// Simple fake implementation of {{ .|serviceclass:file,service }} that returns a previously-defined set of results
  147. /// and stores request values passed into it for later verification.
  148. /// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
  149. class {{ .|serviceclass:file,service }}TestStub: {{ .|serviceclass:file,service }} {
  150. var channel: Channel { fatalError("not implemented") }
  151. var metadata = Metadata()
  152. var host = ""
  153. var timeout: TimeInterval = 0
  154. //-{% for method in service.methods %}
  155. //-{% if method|methodIsUnary %}
  156. var {{ method|methodDescriptorName|lowercase }}Requests: [{{ method|input }}] = []
  157. var {{ method|methodDescriptorName|lowercase }}Responses: [{{ method|output }}] = []
  158. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }} {
  159. {{ method|methodDescriptorName|lowercase }}Requests.append(request)
  160. defer { {{ method|methodDescriptorName|lowercase }}Responses.removeFirst() }
  161. return {{ method|methodDescriptorName|lowercase }}Responses.first!
  162. }
  163. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
  164. completion: @escaping ({{ method|output }}?, CallResult)->()) throws -> {{ .|call:file,service,method }} {
  165. fatalError("not implemented")
  166. }
  167. //-{% endif %}
  168. //-{% if method|methodIsServerStreaming %}
  169. var {{ method|methodDescriptorName|lowercase }}Requests: [{{ method|input }}] = []
  170. var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
  171. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping (CallResult)->())
  172. throws -> {{ .|call:file,service,method }} {
  173. {{ method|methodDescriptorName|lowercase }}Requests.append(request)
  174. defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
  175. return {{ method|methodDescriptorName|lowercase }}Calls.first!
  176. }
  177. //-{% endif %}
  178. //-{% if method|methodIsClientStreaming %}
  179. var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
  180. func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  181. throws -> {{ .|call:file,service,method }} {
  182. defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
  183. return {{ method|methodDescriptorName|lowercase }}Calls.first!
  184. }
  185. //-{% endif %}
  186. //-{% if method|methodIsBidiStreaming %}
  187. var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
  188. func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  189. throws -> {{ .|call:file,service,method }} {
  190. defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
  191. return {{ method|methodDescriptorName|lowercase }}Calls.first!
  192. }
  193. //-{% endif %}
  194. //-{% endfor %}
  195. }
  196. //-{% endif %}
  197. //-{% endfor %}