client.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 }} class {{ .|serviceclass:file,service }} {
  24. public 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. /// Create a client.
  39. {{ access }} init(address: String, secure: Bool = true) {
  40. gRPC.initialize()
  41. channel = Channel(address:address, secure:secure)
  42. metadata = Metadata()
  43. }
  44. /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
  45. {{ access }} init(address: String, certificates: String, host: String?) {
  46. gRPC.initialize()
  47. channel = Channel(address:address, certificates:certificates, host:host)
  48. metadata = Metadata()
  49. }
  50. //-{% for method in service.methods %}
  51. //-{% if method|methodIsUnary %}
  52. /// Synchronous. Unary.
  53. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }})
  54. throws
  55. -> {{ method|output }} {
  56. return try {{ .|call:file,service,method }}(channel).run(request:request, metadata:metadata)
  57. }
  58. /// Asynchronous. Unary.
  59. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
  60. completion: @escaping ({{ method|output }}?, CallResult)->())
  61. throws
  62. -> {{ .|call:file,service,method }} {
  63. return try {{ .|call:file,service,method }}(channel).start(request:request,
  64. metadata:metadata,
  65. completion:completion)
  66. }
  67. //-{% endif %}
  68. //-{% if method|methodIsServerStreaming %}
  69. /// Asynchronous. Server-streaming.
  70. /// Send the initial message.
  71. /// Use methods on the returned object to get streamed responses.
  72. {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping (CallResult)->())
  73. throws
  74. -> {{ .|call:file,service,method }} {
  75. return try {{ .|call:file,service,method }}(channel).start(request:request, metadata:metadata, completion:completion)
  76. }
  77. //-{% endif %}
  78. //-{% if method|methodIsClientStreaming %}
  79. /// Asynchronous. Client-streaming.
  80. /// Use methods on the returned object to stream messages and
  81. /// to close the connection and wait for a final response.
  82. {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  83. throws
  84. -> {{ .|call:file,service,method }} {
  85. return try {{ .|call:file,service,method }}(channel).start(metadata:metadata, completion:completion)
  86. }
  87. //-{% endif %}
  88. //-{% if method|methodIsBidiStreaming %}
  89. /// Asynchronous. Bidirectional-streaming.
  90. /// Use methods on the returned object to stream messages,
  91. /// to wait for replies, and to close the connection.
  92. {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: @escaping (CallResult)->())
  93. throws
  94. -> {{ .|call:file,service,method }} {
  95. return try {{ .|call:file,service,method }}(channel).start(metadata:metadata, completion:completion)
  96. }
  97. //-{% endif %}
  98. //-{% endfor %}
  99. }
  100. //-{% endfor %}