client.swift 7.0 KB

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