Generator-Client.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 2018, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import SwiftProtobuf
  18. import SwiftProtobufPluginLibrary
  19. extension Generator {
  20. internal func printClient() {
  21. println()
  22. printServiceClientProtocol()
  23. println()
  24. printServiceClientImplementation()
  25. }
  26. private func printServiceClientProtocol() {
  27. println("/// Usage: instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
  28. println("\(options.visibility.sourceSnippet) protocol \(serviceClassName) {")
  29. indent()
  30. for method in service.methods {
  31. self.method = method
  32. switch streamingType(method) {
  33. case .unary:
  34. println("func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions?) -> UnaryCall<\(methodInputName), \(methodOutputName)>")
  35. case .serverStreaming:
  36. println("func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions?, handler: @escaping (\(methodOutputName)) -> Void) -> ServerStreamingCall<\(methodInputName), \(methodOutputName)>")
  37. case .clientStreaming:
  38. println("func \(methodFunctionName)(callOptions: CallOptions?) -> ClientStreamingCall<\(methodInputName), \(methodOutputName)>")
  39. case .bidirectionalStreaming:
  40. println("func \(methodFunctionName)(callOptions: CallOptions?, handler: @escaping (\(methodOutputName)) -> Void) -> BidirectionalStreamingCall<\(methodInputName), \(methodOutputName)>")
  41. }
  42. }
  43. outdent()
  44. println("}")
  45. }
  46. private func printServiceClientImplementation() {
  47. println("\(access) final class \(serviceClassName)Client: GRPCClient, \(serviceClassName) {")
  48. indent()
  49. println("\(access) let connection: ClientConnection")
  50. println("\(access) var defaultCallOptions: CallOptions")
  51. println()
  52. println("/// Creates a client for the \(servicePath) service.")
  53. println("///")
  54. printParameters()
  55. println("/// - connection: `ClientConnection` to the service host.")
  56. println("/// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.")
  57. println("\(access) init(connection: ClientConnection, defaultCallOptions: CallOptions = CallOptions()) {")
  58. indent()
  59. println("self.connection = connection")
  60. println("self.defaultCallOptions = defaultCallOptions")
  61. outdent()
  62. println("}")
  63. println()
  64. for method in service.methods {
  65. self.method = method
  66. switch streamingType(method) {
  67. case .unary:
  68. println("/// Asynchronous unary call to \(method.name).")
  69. println("///")
  70. printParameters()
  71. printRequestParameter()
  72. printCallOptionsParameter()
  73. println("/// - Returns: A `UnaryCall` with futures for the metadata, status and response.")
  74. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil) -> UnaryCall<\(methodInputName), \(methodOutputName)> {")
  75. indent()
  76. println("return self.makeUnaryCall(path: \"/\(servicePath)/\(method.name)\",")
  77. println(" request: request,")
  78. println(" callOptions: callOptions ?? self.defaultCallOptions)")
  79. outdent()
  80. println("}")
  81. case .serverStreaming:
  82. println("/// Asynchronous server-streaming call to \(method.name).")
  83. println("///")
  84. printParameters()
  85. printRequestParameter()
  86. printCallOptionsParameter()
  87. printHandlerParameter()
  88. println("/// - Returns: A `ServerStreamingCall` with futures for the metadata and status.")
  89. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> ServerStreamingCall<\(methodInputName), \(methodOutputName)> {")
  90. indent()
  91. println("return self.makeServerStreamingCall(path: \"/\(servicePath)/\(method.name)\",")
  92. println(" request: request,")
  93. println(" callOptions: callOptions ?? self.defaultCallOptions,")
  94. println(" handler: handler)")
  95. outdent()
  96. println("}")
  97. case .clientStreaming:
  98. println("/// Asynchronous client-streaming call to \(method.name).")
  99. println("///")
  100. printClientStreamingDetails()
  101. println("///")
  102. printParameters()
  103. printCallOptionsParameter()
  104. println("/// - Returns: A `ClientStreamingCall` with futures for the metadata, status and response.")
  105. println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil) -> ClientStreamingCall<\(methodInputName), \(methodOutputName)> {")
  106. indent()
  107. println("return self.makeClientStreamingCall(path: \"/\(servicePath)/\(method.name)\",")
  108. println(" callOptions: callOptions ?? self.defaultCallOptions)")
  109. outdent()
  110. println("}")
  111. case .bidirectionalStreaming:
  112. println("/// Asynchronous bidirectional-streaming call to \(method.name).")
  113. println("///")
  114. printClientStreamingDetails()
  115. println("///")
  116. printParameters()
  117. printCallOptionsParameter()
  118. printHandlerParameter()
  119. println("/// - Returns: A `ClientStreamingCall` with futures for the metadata and status.")
  120. println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> BidirectionalStreamingCall<\(methodInputName), \(methodOutputName)> {")
  121. indent()
  122. println("return self.makeBidirectionalStreamingCall(path: \"/\(servicePath)/\(method.name)\",")
  123. println(" callOptions: callOptions ?? self.defaultCallOptions,")
  124. println(" handler: handler)")
  125. outdent()
  126. println("}")
  127. }
  128. println()
  129. }
  130. outdent()
  131. println("}")
  132. }
  133. private func printClientStreamingDetails() {
  134. println("/// Callers should use the `send` method on the returned object to send messages")
  135. println("/// to the server. The caller should send an `.end` after the final message has been sent.")
  136. }
  137. private func printParameters() {
  138. println("/// - Parameters:")
  139. }
  140. private func printRequestParameter() {
  141. println("/// - request: Request to send to \(method.name).")
  142. }
  143. private func printCallOptionsParameter() {
  144. println("/// - callOptions: Call options; `self.defaultCallOptions` is used if `nil`.")
  145. }
  146. private func printHandlerParameter() {
  147. println("/// - handler: A closure called when each response is received from the server.")
  148. }
  149. }