UnaryCall.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2019, 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 NIO
  19. import NIOHTTP1
  20. import NIOHTTP2
  21. import Logging
  22. /// A unary gRPC call. The request is sent on initialization.
  23. ///
  24. /// The following futures are available to the caller:
  25. /// - `initialMetadata`: the initial metadata returned from the server,
  26. /// - `response`: the response from the unary call,
  27. /// - `status`: the status of the gRPC call after it has ended,
  28. /// - `trailingMetadata`: any metadata returned from the server alongside the `status`.
  29. public final class UnaryCall<RequestPayload: GRPCPayload, ResponsePayload: GRPCPayload>
  30. : BaseClientCall<RequestPayload, ResponsePayload>,
  31. UnaryResponseClientCall {
  32. public let response: EventLoopFuture<ResponsePayload>
  33. public init(
  34. connection: ClientConnection,
  35. path: String,
  36. request: RequestPayload,
  37. callOptions: CallOptions,
  38. errorDelegate: ClientErrorDelegate?
  39. ) {
  40. let requestID = callOptions.requestIDProvider.requestID()
  41. let logger = Logger(subsystem: .clientChannelCall, metadata: [MetadataKey.requestID: "\(requestID)"])
  42. logger.debug("starting rpc", metadata: ["path": "\(path)"])
  43. let responsePromise = connection.channel.eventLoop.makePromise(of: ResponsePayload.self)
  44. self.response = responsePromise.futureResult
  45. let responseHandler = GRPCClientUnaryResponseChannelHandler<ResponsePayload>(
  46. initialMetadataPromise: connection.channel.eventLoop.makePromise(),
  47. trailingMetadataPromise: connection.channel.eventLoop.makePromise(),
  48. responsePromise: responsePromise,
  49. statusPromise: connection.channel.eventLoop.makePromise(),
  50. errorDelegate: errorDelegate,
  51. timeout: callOptions.timeout,
  52. logger: logger
  53. )
  54. let requestHead = _GRPCRequestHead(
  55. scheme: connection.configuration.httpProtocol.scheme,
  56. path: path,
  57. host: connection.configuration.target.host,
  58. requestID: requestID,
  59. encoding: connection.configuration.messageEncoding,
  60. options: callOptions
  61. )
  62. let requestHandler = _UnaryRequestChannelHandler<RequestPayload>(
  63. requestHead: requestHead,
  64. request: .init(request)
  65. )
  66. super.init(
  67. eventLoop: connection.channel.eventLoop,
  68. multiplexer: connection.multiplexer,
  69. callType: .unary,
  70. responseHandler: responseHandler,
  71. requestHandler: requestHandler,
  72. logger: logger
  73. )
  74. }
  75. }