client.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /// Call methods of this class to make API calls.
  23. {{ access }} final class {{ .|serviceclass:file,service }} {
  24. public private(set) var channel: Channel
  25. /// This metadata will be sent with all requests.
  26. {{ access }} var metadata : Metadata
  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. {{ access }} var host : String {
  31. get {
  32. return self.channel.host
  33. }
  34. set {
  35. self.channel.host = newValue
  36. }
  37. }
  38. /// This property allows the service timeout to be overridden.
  39. {{ access }} var timeout : TimeInterval {
  40. get {
  41. return self.channel.timeout
  42. }
  43. set {
  44. self.channel.timeout = newValue
  45. }
  46. }
  47. /// Create a client.
  48. {{ access }} init(address: String, secure: Bool = true) {
  49. gRPC.initialize()
  50. channel = Channel(address:address, secure:secure)
  51. metadata = Metadata()
  52. }
  53. /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
  54. {{ access }} init(address: String, certificates: String, host: String?) {
  55. gRPC.initialize()
  56. channel = Channel(address:address, certificates:certificates, host:host)
  57. metadata = Metadata()
  58. }
  59. //-{% for method in service.methods %}
  60. //-{% if method|methodIsUnary %}
  61. /// Synchronous. Unary.
  62. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }})
  63. throws
  64. -> {{ method|output }} {
  65. return try {{ .|call:file,service,method }}(channel).run(request:request, metadata:metadata)
  66. }
  67. /// Asynchronous. Unary.
  68. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
  69. completion: @escaping ({{ method|output }}?, CallResult)->())
  70. throws
  71. -> {{ .|call:file,service,method }} {
  72. return try {{ .|call:file,service,method }}(channel).start(request:request,
  73. metadata:metadata,
  74. completion:completion)
  75. }
  76. //-{% endif %}
  77. //-{% if method|methodIsServerStreaming %}
  78. /// Asynchronous. Server-streaming.
  79. /// Send the initial message.
  80. /// Use methods on the returned object to get streamed responses.
  81. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping (CallResult)->())
  82. throws
  83. -> {{ .|call:file,service,method }} {
  84. return try {{ .|call:file,service,method }}(channel).start(request:request, metadata:metadata, completion:completion)
  85. }
  86. //-{% endif %}
  87. //-{% if method|methodIsClientStreaming %}
  88. /// Asynchronous. Client-streaming.
  89. /// Use methods on the returned object to stream messages and
  90. /// to close the connection and wait for a final response.
  91. {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  92. throws
  93. -> {{ .|call:file,service,method }} {
  94. return try {{ .|call:file,service,method }}(channel).start(metadata:metadata, completion:completion)
  95. }
  96. //-{% endif %}
  97. //-{% if method|methodIsBidiStreaming %}
  98. /// Asynchronous. Bidirectional-streaming.
  99. /// Use methods on the returned object to stream messages,
  100. /// to wait for replies, and to close the connection.
  101. {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  102. throws
  103. -> {{ .|call:file,service,method }} {
  104. return try {{ .|call:file,service,method }}(channel).start(metadata:metadata, completion:completion)
  105. }
  106. //-{% endif %}
  107. //-{% endfor %}
  108. }
  109. //-{% endfor %}