UnaryCall.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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<RequestMessage: Message, ResponseMessage: Message>
  30. : BaseClientCall<RequestMessage, ResponseMessage>,
  31. UnaryResponseClientCall {
  32. public let response: EventLoopFuture<ResponseMessage>
  33. public init(
  34. connection: ClientConnection,
  35. path: String,
  36. request: RequestMessage,
  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: ResponseMessage.self)
  44. self.response = responsePromise.futureResult
  45. let responseHandler = GRPCClientUnaryResponseChannelHandler<ResponseMessage>(
  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. options: callOptions
  60. )
  61. let requestHandler = _UnaryRequestChannelHandler<RequestMessage>(
  62. requestHead: requestHead,
  63. request: .init(request)
  64. )
  65. super.init(
  66. eventLoop: connection.channel.eventLoop,
  67. multiplexer: connection.multiplexer,
  68. callType: .unary,
  69. responseHandler: responseHandler,
  70. requestHandler: requestHandler,
  71. logger: logger
  72. )
  73. }
  74. }