Generator-Client.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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: GRPCServiceClient, \(serviceClassName) {")
  48. indent()
  49. println("\(access) let connection: ClientConnection")
  50. println("\(access) var serviceName: String { return \"\(servicePath)\" }")
  51. println("\(access) var defaultCallOptions: CallOptions")
  52. println()
  53. println("/// Creates a client for the \(servicePath) service.")
  54. println("///")
  55. printParameters()
  56. println("/// - connection: `ClientConnection` to the service host.")
  57. println("/// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.")
  58. println("\(access) init(connection: ClientConnection, defaultCallOptions: CallOptions = CallOptions()) {")
  59. indent()
  60. println("self.connection = connection")
  61. println("self.defaultCallOptions = defaultCallOptions")
  62. outdent()
  63. println("}")
  64. println()
  65. for method in service.methods {
  66. self.method = method
  67. switch streamingType(method) {
  68. case .unary:
  69. println("/// Asynchronous unary call to \(method.name).")
  70. println("///")
  71. printParameters()
  72. printRequestParameter()
  73. printCallOptionsParameter()
  74. println("/// - Returns: A `UnaryCall` with futures for the metadata, status and response.")
  75. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil) -> UnaryCall<\(methodInputName), \(methodOutputName)> {")
  76. indent()
  77. println("return self.makeUnaryCall(path: self.path(forMethod: \"\(method.name)\"),")
  78. println(" request: request,")
  79. println(" callOptions: callOptions ?? self.defaultCallOptions)")
  80. outdent()
  81. println("}")
  82. case .serverStreaming:
  83. println("/// Asynchronous server-streaming call to \(method.name).")
  84. println("///")
  85. printParameters()
  86. printRequestParameter()
  87. printCallOptionsParameter()
  88. printHandlerParameter()
  89. println("/// - Returns: A `ServerStreamingCall` with futures for the metadata and status.")
  90. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> ServerStreamingCall<\(methodInputName), \(methodOutputName)> {")
  91. indent()
  92. println("return self.makeServerStreamingCall(path: self.path(forMethod: \"\(method.name)\"),")
  93. println(" request: request,")
  94. println(" callOptions: callOptions ?? self.defaultCallOptions,")
  95. println(" handler: handler)")
  96. outdent()
  97. println("}")
  98. case .clientStreaming:
  99. println("/// Asynchronous client-streaming call to \(method.name).")
  100. println("///")
  101. printClientStreamingDetails()
  102. println("///")
  103. printParameters()
  104. printCallOptionsParameter()
  105. println("/// - Returns: A `ClientStreamingCall` with futures for the metadata, status and response.")
  106. println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil) -> ClientStreamingCall<\(methodInputName), \(methodOutputName)> {")
  107. indent()
  108. println("return self.makeClientStreamingCall(path: self.path(forMethod: \"\(method.name)\"),")
  109. println(" callOptions: callOptions ?? self.defaultCallOptions)")
  110. outdent()
  111. println("}")
  112. case .bidirectionalStreaming:
  113. println("/// Asynchronous bidirectional-streaming call to \(method.name).")
  114. println("///")
  115. printClientStreamingDetails()
  116. println("///")
  117. printParameters()
  118. printCallOptionsParameter()
  119. printHandlerParameter()
  120. println("/// - Returns: A `ClientStreamingCall` with futures for the metadata and status.")
  121. println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> BidirectionalStreamingCall<\(methodInputName), \(methodOutputName)> {")
  122. indent()
  123. println("return self.makeBidirectionalStreamingCall(path: self.path(forMethod: \"\(method.name)\"),")
  124. println(" callOptions: callOptions ?? self.defaultCallOptions,")
  125. println(" handler: handler)")
  126. outdent()
  127. println("}")
  128. }
  129. println()
  130. }
  131. outdent()
  132. println("}")
  133. }
  134. private func printClientStreamingDetails() {
  135. println("/// Callers should use the `send` method on the returned object to send messages")
  136. println("/// to the server. The caller should send an `.end` after the final message has been sent.")
  137. }
  138. private func printParameters() {
  139. println("/// - Parameters:")
  140. }
  141. private func printRequestParameter() {
  142. println("/// - request: Request to send to \(method.name).")
  143. }
  144. private func printCallOptionsParameter() {
  145. println("/// - callOptions: Call options; `self.defaultCallOptions` is used if `nil`.")
  146. }
  147. private func printHandlerParameter() {
  148. println("/// - handler: A closure called when each response is received from the server.")
  149. }
  150. }