| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using IceRpc.Slice.Operations.Internal; |
| | | 5 | | using ZeroC.Slice.Codec; |
| | | 6 | | |
| | | 7 | | namespace IceRpc.Slice.Operations; |
| | | 8 | | |
| | | 9 | | /// <summary>Provides extension methods for <see cref="IncomingResponse" /> to decode its Slice-encoded payload. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class IncomingResponseExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary>Decodes a response payload.</summary> |
| | | 14 | | /// <typeparam name="T">The type of the return value.</typeparam> |
| | | 15 | | /// <param name="response">The incoming response.</param> |
| | | 16 | | /// <param name="request">The outgoing request.</param> |
| | | 17 | | /// <param name="sender">The proxy that sent the request.</param> |
| | | 18 | | /// <param name="decodeReturnValue">A function that decodes the return value.</param> |
| | | 19 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 20 | | /// <returns>A value task that holds the operation's return value.</returns> |
| | | 21 | | /// <exception cref="DispatchException">Thrown when the status code is not <see cref="StatusCode.Ok"/>.</exception> |
| | | 22 | | public static ValueTask<T> DecodeReturnValueAsync<T>( |
| | | 23 | | this IncomingResponse response, |
| | | 24 | | OutgoingRequest request, |
| | | 25 | | ISliceProxy sender, |
| | | 26 | | DecodeFunc<T> decodeReturnValue, |
| | | 27 | | CancellationToken cancellationToken = default) |
| | 68 | 28 | | { |
| | 68 | 29 | | ISliceFeature feature = request.Features.Get<ISliceFeature>() ?? SliceFeature.Default; |
| | | 30 | | |
| | 68 | 31 | | return response.StatusCode == StatusCode.Ok ? |
| | 68 | 32 | | response.DecodeValueAsync( |
| | 68 | 33 | | feature, |
| | 68 | 34 | | feature.BaseProxy ?? sender, |
| | 68 | 35 | | decodeReturnValue, |
| | 68 | 36 | | cancellationToken) : |
| | 68 | 37 | | throw new DispatchException(response.StatusCode, response.ErrorMessage) |
| | 68 | 38 | | { |
| | 68 | 39 | | ConvertToInternalError = true |
| | 68 | 40 | | }; |
| | 67 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary>Verifies that a Slice-encoded response payload carries no return value or only tagged return values. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="response">The incoming response.</param> |
| | | 46 | | /// <param name="request">The outgoing request.</param> |
| | | 47 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 48 | | /// <returns>A value task representing the asynchronous completion of the operation.</returns> |
| | | 49 | | /// <exception cref="DispatchException">Thrown when the status code is not <see cref="StatusCode.Ok"/>.</exception> |
| | | 50 | | public static ValueTask DecodeVoidReturnValueAsync( |
| | | 51 | | this IncomingResponse response, |
| | | 52 | | OutgoingRequest request, |
| | | 53 | | CancellationToken cancellationToken = default) |
| | 21 | 54 | | { |
| | 21 | 55 | | ISliceFeature feature = request.Features.Get<ISliceFeature>() ?? SliceFeature.Default; |
| | | 56 | | |
| | 21 | 57 | | return response.StatusCode == StatusCode.Ok ? |
| | 21 | 58 | | response.DecodeVoidAsync(feature, cancellationToken) : |
| | 21 | 59 | | throw new DispatchException(response.StatusCode, response.ErrorMessage) |
| | 21 | 60 | | { |
| | 21 | 61 | | ConvertToInternalError = true |
| | 21 | 62 | | }; |
| | 20 | 63 | | } |
| | | 64 | | } |