client.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 }}: ServiceClient {
  18. //-{% for method in service.methods %}
  19. //-{% if method|methodIsUnary %}
  20. /// Synchronous. Unary.
  21. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }}
  22. /// Asynchronous. Unary.
  23. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }}
  24. //-{% endif %}
  25. //-{% if method|methodIsServerStreaming %}
  26. /// Asynchronous. Server-streaming.
  27. /// Send the initial message.
  28. /// Use methods on the returned object to get streamed responses.
  29. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
  30. //-{% endif %}
  31. //-{% if method|methodIsClientStreaming %}
  32. /// Asynchronous. Client-streaming.
  33. /// Use methods on the returned object to stream messages and
  34. /// to close the connection and wait for a final response.
  35. func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
  36. //-{% endif %}
  37. //-{% if method|methodIsBidiStreaming %}
  38. /// Asynchronous. Bidirectional-streaming.
  39. /// Use methods on the returned object to stream messages,
  40. /// to wait for replies, and to close the connection.
  41. func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
  42. //-{% endif %}
  43. //-{% endfor %}
  44. }
  45. {{ access }} final class {{ .|serviceclass:file,service }}Client: ServiceClientBase, {{ .|serviceclass:file,service }} {
  46. //-{% for method in service.methods %}
  47. //-{% if method|methodIsUnary %}
  48. /// Synchronous. Unary.
  49. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }} {
  50. return try {{ .|call:file,service,method }}Impl(channel)
  51. .run(request: request, metadata: metadata)
  52. }
  53. /// Asynchronous. Unary.
  54. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
  55. completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }} {
  56. return try {{ .|call:file,service,method }}Impl(channel)
  57. .start(request: request, metadata: metadata, completion: completion)
  58. }
  59. //-{% endif %}
  60. //-{% if method|methodIsServerStreaming %}
  61. /// Asynchronous. Server-streaming.
  62. /// Send the initial message.
  63. /// Use methods on the returned object to get streamed responses.
  64. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
  65. return try {{ .|call:file,service,method }}Impl(channel)
  66. .start(request:request, metadata:metadata, completion:completion)
  67. }
  68. //-{% endif %}
  69. //-{% if method|methodIsClientStreaming %}
  70. /// Asynchronous. Client-streaming.
  71. /// Use methods on the returned object to stream messages and
  72. /// to close the connection and wait for a final response.
  73. {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
  74. return try {{ .|call:file,service,method }}Impl(channel)
  75. .start(metadata: metadata, completion: completion)
  76. }
  77. //-{% endif %}
  78. //-{% if method|methodIsBidiStreaming %}
  79. /// Asynchronous. Bidirectional-streaming.
  80. /// Use methods on the returned object to stream messages,
  81. /// to wait for replies, and to close the connection.
  82. {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
  83. return try {{ .|call:file,service,method }}Impl(channel)
  84. .start(metadata: metadata, completion: completion)
  85. }
  86. //-{% endif %}
  87. //-{% endfor %}
  88. }
  89. //-{% if generateTestStubs %}
  90. class {{ .|serviceclass:file,service }}TestStub: ServiceClientTestStubBase, {{ .|serviceclass:file,service }} {
  91. //-{% for method in service.methods %}
  92. //-{% if method|methodIsUnary %}
  93. var {{ method|methodDescriptorName|lowercase }}Requests: [{{ method|input }}] = []
  94. var {{ method|methodDescriptorName|lowercase }}Responses: [{{ method|output }}] = []
  95. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }} {
  96. {{ method|methodDescriptorName|lowercase }}Requests.append(request)
  97. defer { {{ method|methodDescriptorName|lowercase }}Responses.removeFirst() }
  98. return {{ method|methodDescriptorName|lowercase }}Responses.first!
  99. }
  100. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }} {
  101. fatalError("not implemented")
  102. }
  103. //-{% endif %}
  104. //-{% if method|methodIsServerStreaming %}
  105. var {{ method|methodDescriptorName|lowercase }}Requests: [{{ method|input }}] = []
  106. var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
  107. func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
  108. {{ method|methodDescriptorName|lowercase }}Requests.append(request)
  109. defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
  110. return {{ method|methodDescriptorName|lowercase }}Calls.first!
  111. }
  112. //-{% endif %}
  113. //-{% if method|methodIsClientStreaming %}
  114. var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
  115. func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
  116. defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
  117. return {{ method|methodDescriptorName|lowercase }}Calls.first!
  118. }
  119. //-{% endif %}
  120. //-{% if method|methodIsBidiStreaming %}
  121. var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
  122. func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
  123. defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
  124. return {{ method|methodDescriptorName|lowercase }}Calls.first!
  125. }
  126. //-{% endif %}
  127. //-{% endfor %}
  128. }
  129. //-{% endif %}
  130. //-{% endfor %}