NormalizationProvider.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2021, 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 GRPC
  17. import NIOCore
  18. import SwiftProtobuf
  19. final class NormalizationProvider: Normalization_NormalizationProvider {
  20. let interceptors: Normalization_NormalizationServerInterceptorFactoryProtocol? = nil
  21. // MARK: Unary
  22. private func unary(
  23. context: StatusOnlyCallContext,
  24. function: String = #function
  25. ) -> EventLoopFuture<Normalization_FunctionName> {
  26. return context.eventLoop.makeSucceededFuture(.with { $0.functionName = function })
  27. }
  28. func Unary(
  29. request: Google_Protobuf_Empty,
  30. context: StatusOnlyCallContext
  31. ) -> EventLoopFuture<Normalization_FunctionName> {
  32. return self.unary(context: context)
  33. }
  34. func unary(
  35. request: Google_Protobuf_Empty,
  36. context: StatusOnlyCallContext
  37. ) -> EventLoopFuture<Normalization_FunctionName> {
  38. return self.unary(context: context)
  39. }
  40. // MARK: Server Streaming
  41. private func serverStreaming(
  42. context: StreamingResponseCallContext<Normalization_FunctionName>,
  43. function: String = #function
  44. ) -> EventLoopFuture<GRPCStatus> {
  45. context.sendResponse(.with { $0.functionName = function }, promise: nil)
  46. return context.eventLoop.makeSucceededFuture(.ok)
  47. }
  48. func ServerStreaming(
  49. request: Google_Protobuf_Empty,
  50. context: StreamingResponseCallContext<Normalization_FunctionName>
  51. ) -> EventLoopFuture<GRPCStatus> {
  52. return self.serverStreaming(context: context)
  53. }
  54. func serverStreaming(
  55. request: Google_Protobuf_Empty,
  56. context: StreamingResponseCallContext<Normalization_FunctionName>
  57. ) -> EventLoopFuture<GRPCStatus> {
  58. return self.serverStreaming(context: context)
  59. }
  60. // MARK: Client Streaming
  61. private func _clientStreaming(
  62. context: UnaryResponseCallContext<Normalization_FunctionName>,
  63. function: String = #function
  64. ) -> EventLoopFuture<(StreamEvent<Google_Protobuf_Empty>) -> Void> {
  65. func handle(_ event: StreamEvent<Google_Protobuf_Empty>) {
  66. switch event {
  67. case .message:
  68. ()
  69. case .end:
  70. context.responsePromise.succeed(.with { $0.functionName = function })
  71. }
  72. }
  73. return context.eventLoop.makeSucceededFuture(handle(_:))
  74. }
  75. func ClientStreaming(
  76. context: UnaryResponseCallContext<Normalization_FunctionName>
  77. ) -> EventLoopFuture<(StreamEvent<Google_Protobuf_Empty>) -> Void> {
  78. return self._clientStreaming(context: context)
  79. }
  80. func clientStreaming(
  81. context: UnaryResponseCallContext<Normalization_FunctionName>
  82. ) -> EventLoopFuture<(StreamEvent<Google_Protobuf_Empty>) -> Void> {
  83. return self._clientStreaming(context: context)
  84. }
  85. // MARK: Bidirectional Streaming
  86. private func _bidirectionalStreaming(
  87. context: StreamingResponseCallContext<Normalization_FunctionName>,
  88. function: String = #function
  89. ) -> EventLoopFuture<(StreamEvent<Google_Protobuf_Empty>) -> Void> {
  90. func handle(_ event: StreamEvent<Google_Protobuf_Empty>) {
  91. switch event {
  92. case .message:
  93. ()
  94. case .end:
  95. context.sendResponse(.with { $0.functionName = function }, promise: nil)
  96. context.statusPromise.succeed(.ok)
  97. }
  98. }
  99. return context.eventLoop.makeSucceededFuture(handle(_:))
  100. }
  101. func BidirectionalStreaming(
  102. context: StreamingResponseCallContext<Normalization_FunctionName>
  103. ) -> EventLoopFuture<(StreamEvent<Google_Protobuf_Empty>) -> Void> {
  104. return self._bidirectionalStreaming(context: context)
  105. }
  106. func bidirectionalStreaming(
  107. context: StreamingResponseCallContext<Normalization_FunctionName>
  108. ) -> EventLoopFuture<(StreamEvent<Google_Protobuf_Empty>) -> Void> {
  109. return self._bidirectionalStreaming(context: context)
  110. }
  111. }