/src/grpc-swift/Sources/GRPC/Interceptor/ClientInterceptorContext.swift
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020, 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 Logging |
17 | | import NIOCore |
18 | | |
19 | | public struct ClientInterceptorContext<Request, Response> { |
20 | | /// The interceptor this context is for. |
21 | | @usableFromInline |
22 | | internal let interceptor: ClientInterceptor<Request, Response> |
23 | | |
24 | | /// The pipeline this context is associated with. |
25 | | @usableFromInline |
26 | | internal let _pipeline: ClientInterceptorPipeline<Request, Response> |
27 | | |
28 | | /// The index of this context's interceptor within the pipeline. |
29 | | @usableFromInline |
30 | | internal let _index: Int |
31 | | |
32 | | /// The `EventLoop` this interceptor pipeline is being executed on. |
33 | 0 | public var eventLoop: EventLoop { |
34 | 0 | return self._pipeline.eventLoop |
35 | 0 | } |
36 | | |
37 | | /// A logger. |
38 | 0 | public var logger: Logger { |
39 | 0 | return self._pipeline.logger |
40 | 0 | } |
41 | | |
42 | | /// The type of the RPC, e.g. "unary". |
43 | 0 | public var type: GRPCCallType { |
44 | 0 | return self._pipeline.details.type |
45 | 0 | } |
46 | | |
47 | | /// The path of the RPC in the format "/Service/Method", e.g. "/echo.Echo/Get". |
48 | 0 | public var path: String { |
49 | 0 | return self._pipeline.details.path |
50 | 0 | } |
51 | | |
52 | | /// The options used to invoke the call. |
53 | 0 | public var options: CallOptions { |
54 | 0 | return self._pipeline.details.options |
55 | 0 | } |
56 | | |
57 | | /// Construct a ``ClientInterceptorContext`` for the interceptor at the given index within in |
58 | | /// interceptor pipeline. |
59 | | @inlinable |
60 | | internal init( |
61 | | for interceptor: ClientInterceptor<Request, Response>, |
62 | | atIndex index: Int, |
63 | | in pipeline: ClientInterceptorPipeline<Request, Response> |
64 | 0 | ) { |
65 | 0 | self.interceptor = interceptor |
66 | 0 | self._pipeline = pipeline |
67 | 0 | self._index = index |
68 | 0 | } |
69 | | |
70 | | /// Forwards the response part to the next inbound interceptor in the pipeline, if there is one. |
71 | | /// |
72 | | /// - Parameter part: The response part to forward. |
73 | | /// - Important: This *must* to be called from the `eventLoop`. |
74 | | @inlinable |
75 | 0 | public func receive(_ part: GRPCClientResponsePart<Response>) { |
76 | 0 | self.eventLoop.assertInEventLoop() |
77 | 0 | self._pipeline.invokeReceive(part, fromContextAtIndex: self._index) |
78 | 0 | } |
79 | | |
80 | | /// Forwards the error to the next inbound interceptor in the pipeline, if there is one. |
81 | | /// |
82 | | /// - Parameter error: The error to forward. |
83 | | /// - Important: This *must* to be called from the `eventLoop`. |
84 | | @inlinable |
85 | 0 | public func errorCaught(_ error: Error) { |
86 | 0 | self.eventLoop.assertInEventLoop() |
87 | 0 | self._pipeline.invokeErrorCaught(error, fromContextAtIndex: self._index) |
88 | 0 | } |
89 | | |
90 | | /// Forwards the request part to the next outbound interceptor in the pipeline, if there is one. |
91 | | /// |
92 | | /// - Parameters: |
93 | | /// - part: The request part to forward. |
94 | | /// - promise: The promise the complete when the part has been written. |
95 | | /// - Important: This *must* to be called from the `eventLoop`. |
96 | | @inlinable |
97 | | public func send( |
98 | | _ part: GRPCClientRequestPart<Request>, |
99 | | promise: EventLoopPromise<Void>? |
100 | 0 | ) { |
101 | 0 | self.eventLoop.assertInEventLoop() |
102 | 0 | self._pipeline.invokeSend(part, promise: promise, fromContextAtIndex: self._index) |
103 | 0 | } |
104 | | |
105 | | /// Forwards a request to cancel the RPC to the next outbound interceptor in the pipeline. |
106 | | /// |
107 | | /// - Parameter promise: The promise to complete with the outcome of the cancellation request. |
108 | | /// - Important: This *must* to be called from the `eventLoop`. |
109 | | @inlinable |
110 | 0 | public func cancel(promise: EventLoopPromise<Void>?) { |
111 | 0 | self.eventLoop.assertInEventLoop() |
112 | 0 | self._pipeline.invokeCancel(promise: promise, fromContextAtIndex: self._index) |
113 | 0 | } |
114 | | } |