client.swift 8.6 KB

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