client.pb.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: {{ file.name }}
  6. *
  7. */
  8. /*
  9. *
  10. * Copyright 2017, Google Inc.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are
  15. * met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following disclaimer
  21. * in the documentation and/or other materials provided with the
  22. * distribution.
  23. * * Neither the name of Google Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from
  25. * this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. import Foundation
  41. import Dispatch
  42. import gRPC
  43. //-{% for service in file.service %}
  44. /// Type for errors thrown from generated client code.
  45. public enum {{ .|clienterror:file,service }} : Error {
  46. case endOfStream
  47. case invalidMessageReceived
  48. case error(c: CallResult)
  49. }
  50. //-{% for method in service.method %}
  51. //-{% if not method.clientStreaming and not method.serverStreaming %}
  52. //-{% include "client-call-unary.swift" %}
  53. //-{% endif %}
  54. //-{% if not method.clientStreaming and method.serverStreaming %}
  55. //-{% include "client-call-serverstreaming.swift" %}
  56. //-{% endif %}
  57. //-{% if method.clientStreaming and not method.serverStreaming %}
  58. //-{% include "client-call-clientstreaming.swift" %}
  59. //-{% endif %}
  60. //-{% if method.clientStreaming and method.serverStreaming %}
  61. //-{% include "client-call-bidistreaming.swift" %}
  62. //-{% endif %}
  63. //-{% endfor %}
  64. /// Call methods of this class to make API calls.
  65. public class {{ .|serviceclass:file,service }} {
  66. private var channel: Channel
  67. /// This metadata will be sent with all requests.
  68. public var metadata : Metadata
  69. /// This property allows the service host name to be overridden.
  70. /// For example, it can be used to make calls to "localhost:8080"
  71. /// appear to be to "example.com".
  72. public var host : String {
  73. get {
  74. return self.channel.host
  75. }
  76. set {
  77. self.channel.host = newValue
  78. }
  79. }
  80. /// Create a client that makes insecure connections.
  81. public init(address: String) {
  82. gRPC.initialize()
  83. channel = Channel(address:address)
  84. metadata = Metadata()
  85. }
  86. /// Create a client that makes secure connections.
  87. public init(address: String, certificates: String?, host: String?) {
  88. gRPC.initialize()
  89. channel = Channel(address:address, certificates:certificates, host:host)
  90. metadata = Metadata()
  91. }
  92. //-{% for method in service.method %}
  93. //-{% if not method.clientStreaming and not method.serverStreaming %}
  94. /// Synchronous. Unary.
  95. public func {{ method.name|lowercase }}(_ request: {{ method|input }})
  96. throws
  97. -> {{ method|output }} {
  98. return try {{ .|call:file,service,method }}(channel).run(request:request, metadata:metadata)
  99. }
  100. /// Asynchronous. Unary.
  101. public func {{ method.name|lowercase }}(_ request: {{ method|input }},
  102. completion: @escaping ({{ method|output }}?, CallResult)->())
  103. throws
  104. -> {{ .|call:file,service,method }} {
  105. return try {{ .|call:file,service,method }}(channel).start(request:request,
  106. metadata:metadata,
  107. completion:completion)
  108. }
  109. //-{% endif %}
  110. //-{% if not method.clientStreaming and method.serverStreaming %}
  111. /// Asynchronous. Server-streaming.
  112. /// Send the initial message.
  113. /// Use methods on the returned object to get streamed responses.
  114. public func {{ method.name|lowercase }}(_ request: {{ method|input }}, completion: @escaping (CallResult)->())
  115. throws
  116. -> {{ .|call:file,service,method }} {
  117. return try {{ .|call:file,service,method }}(channel).start(request:request, metadata:metadata, completion:completion)
  118. }
  119. //-{% endif %}
  120. //-{% if method.clientStreaming and not method.serverStreaming %}
  121. /// Asynchronous. Client-streaming.
  122. /// Use methods on the returned object to stream messages and
  123. /// to close the connection and wait for a final response.
  124. public func {{ method.name|lowercase }}(completion: @escaping (CallResult)->())
  125. throws
  126. -> {{ .|call:file,service,method }} {
  127. return try {{ .|call:file,service,method }}(channel).start(metadata:metadata, completion:completion)
  128. }
  129. //-{% endif %}
  130. //-{% if method.clientStreaming and method.serverStreaming %}
  131. /// Asynchronous. Bidirectional-streaming.
  132. /// Use methods on the returned object to stream messages,
  133. /// to wait for replies, and to close the connection.
  134. public func {{ method.name|lowercase }}(completion: @escaping (CallResult)->())
  135. throws
  136. -> {{ .|call:file,service,method }} {
  137. return try {{ .|call:file,service,method }}(channel).start(metadata:metadata, completion:completion)
  138. }
  139. //-{% endif %}
  140. //-{% endfor %}
  141. }
  142. //-{% endfor %}