2
0

UnaryCall.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. init(
  34. path: String,
  35. scheme: String,
  36. authority: String,
  37. callOptions: CallOptions,
  38. eventLoop: EventLoop,
  39. multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>,
  40. errorDelegate: ClientErrorDelegate?,
  41. logger: Logger,
  42. request: RequestPayload
  43. ) {
  44. let requestID = callOptions.requestIDProvider.requestID()
  45. var logger = logger
  46. logger[metadataKey: MetadataKey.requestID] = "\(requestID)"
  47. logger.debug("starting rpc", metadata: ["path": "\(path)"])
  48. let responsePromise = eventLoop.makePromise(of: ResponsePayload.self)
  49. self.response = responsePromise.futureResult
  50. let responseHandler = GRPCClientUnaryResponseChannelHandler<ResponsePayload>(
  51. initialMetadataPromise: eventLoop.makePromise(),
  52. trailingMetadataPromise: eventLoop.makePromise(),
  53. responsePromise: responsePromise,
  54. statusPromise: eventLoop.makePromise(),
  55. errorDelegate: errorDelegate,
  56. timeout: callOptions.timeout,
  57. logger: logger
  58. )
  59. let requestHead = _GRPCRequestHead(
  60. scheme: scheme,
  61. path: path,
  62. host: authority,
  63. requestID: requestID,
  64. options: callOptions
  65. )
  66. let requestHandler = _UnaryRequestChannelHandler<RequestPayload>(
  67. requestHead: requestHead,
  68. request: .init(request, compressed: callOptions.messageEncoding.enabledForRequests)
  69. )
  70. super.init(
  71. eventLoop: eventLoop,
  72. multiplexer: multiplexer,
  73. callType: .unary,
  74. callOptions: callOptions,
  75. responseHandler: responseHandler,
  76. requestHandler: requestHandler,
  77. logger: logger
  78. )
  79. }
  80. }