/src/grpc-swift/Sources/GRPC/ClientCalls/UnaryCall.swift
Line | Count | Source |
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 Logging |
18 | | import NIOCore |
19 | | import NIOHPACK |
20 | | import NIOHTTP1 |
21 | | import NIOHTTP2 |
22 | | import SwiftProtobuf |
23 | | |
24 | | /// A unary gRPC call. The request is sent on initialization. |
25 | | /// |
26 | | /// Note: while this object is a `struct`, its implementation delegates to ``Call``. It therefore |
27 | | /// has reference semantics. |
28 | | public struct UnaryCall<RequestPayload, ResponsePayload>: UnaryResponseClientCall { |
29 | | private let call: Call<RequestPayload, ResponsePayload> |
30 | | private let responseParts: UnaryResponseParts<ResponsePayload> |
31 | | |
32 | | /// The options used to make the RPC. |
33 | 0 | public var options: CallOptions { |
34 | 0 | return self.call.options |
35 | 0 | } |
36 | | |
37 | | /// The path used to make the RPC. |
38 | 0 | public var path: String { |
39 | 0 | return self.call.path |
40 | 0 | } |
41 | | |
42 | | /// The `Channel` used to transport messages for this RPC. |
43 | 0 | public var subchannel: EventLoopFuture<Channel> { |
44 | 0 | return self.call.channel |
45 | 0 | } |
46 | | |
47 | | /// The `EventLoop` this call is running on. |
48 | 0 | public var eventLoop: EventLoop { |
49 | 0 | return self.call.eventLoop |
50 | 0 | } |
51 | | |
52 | | /// Cancel this RPC if it hasn't already completed. |
53 | 0 | public func cancel(promise: EventLoopPromise<Void>?) { |
54 | 0 | self.call.cancel(promise: promise) |
55 | 0 | } |
56 | | |
57 | | // MARK: - Response Parts |
58 | | |
59 | | /// The initial metadata returned from the server. |
60 | 0 | public var initialMetadata: EventLoopFuture<HPACKHeaders> { |
61 | 0 | return self.responseParts.initialMetadata |
62 | 0 | } |
63 | | |
64 | | /// The response returned by the server. |
65 | 0 | public var response: EventLoopFuture<ResponsePayload> { |
66 | 0 | return self.responseParts.response |
67 | 0 | } |
68 | | |
69 | | /// The trailing metadata returned from the server. |
70 | 0 | public var trailingMetadata: EventLoopFuture<HPACKHeaders> { |
71 | 0 | return self.responseParts.trailingMetadata |
72 | 0 | } |
73 | | |
74 | | /// The final status of the the RPC. |
75 | 0 | public var status: EventLoopFuture<GRPCStatus> { |
76 | 0 | return self.responseParts.status |
77 | 0 | } |
78 | | |
79 | 0 | internal init(call: Call<RequestPayload, ResponsePayload>) { |
80 | 0 | self.call = call |
81 | 0 | self.responseParts = UnaryResponseParts(on: call.eventLoop) |
82 | 0 | } |
83 | | |
84 | 0 | internal func invoke(_ request: RequestPayload) { |
85 | 0 | self.call.invokeUnaryRequest( |
86 | 0 | request, |
87 | 0 | onStart: {}, |
88 | 0 | onError: self.responseParts.handleError(_:), Unexecuted instantiation: $s4GRPC9UnaryCallV6invokeyyxFys5Error_pcAA0B13ResponsePartsCyq_Gcfu_ Unexecuted instantiation: $s4GRPC9UnaryCallV6invokeyyxFys5Error_pcAA0B13ResponsePartsCyq_Gcfu_ysAE_pcfu0_ |
89 | 0 | onResponsePart: self.responseParts.handle(_:) Unexecuted instantiation: $s4GRPC9UnaryCallV6invokeyyxFyAA22GRPCClientResponsePartOyq_GcAA0bF5PartsCyq_Gcfu1_ Unexecuted instantiation: $s4GRPC9UnaryCallV6invokeyyxFyAA22GRPCClientResponsePartOyq_GcAA0bF5PartsCyq_Gcfu1_yAGcfu2_ |
90 | 0 | ) |
91 | 0 | } |
92 | | } |