|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Threading; |
| 18 | +using System.Threading.Tasks; |
| 19 | + |
| 20 | +namespace Amazon.Lambda.RuntimeSupport |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Interface for writing streaming responses in AWS Lambda functions. |
| 24 | + /// Obtained by calling ResponseStreamFactory.CreateStream() within a handler. |
| 25 | + /// </summary> |
| 26 | + public interface IResponseStream : IDisposable |
| 27 | + { |
| 28 | + /// <summary> |
| 29 | + /// Asynchronously writes a byte array to the response stream. |
| 30 | + /// </summary> |
| 31 | + /// <param name="buffer">The byte array to write.</param> |
| 32 | + /// <param name="cancellationToken">Optional cancellation token.</param> |
| 33 | + /// <returns>A task representing the asynchronous operation.</returns> |
| 34 | + /// <exception cref="InvalidOperationException">Thrown if the stream is already completed or an error has been reported.</exception> |
| 35 | + /// <exception cref="InvalidOperationException">Thrown if writing would exceed the 20 MiB limit.</exception> |
| 36 | + Task WriteAsync(byte[] buffer, CancellationToken cancellationToken = default); |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Asynchronously writes a portion of a byte array to the response stream. |
| 40 | + /// </summary> |
| 41 | + /// <param name="buffer">The byte array containing data to write.</param> |
| 42 | + /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes.</param> |
| 43 | + /// <param name="count">The number of bytes to write.</param> |
| 44 | + /// <param name="cancellationToken">Optional cancellation token.</param> |
| 45 | + /// <returns>A task representing the asynchronous operation.</returns> |
| 46 | + /// <exception cref="InvalidOperationException">Thrown if the stream is already completed or an error has been reported.</exception> |
| 47 | + /// <exception cref="InvalidOperationException">Thrown if writing would exceed the 20 MiB limit.</exception> |
| 48 | + Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default); |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Asynchronously writes a memory buffer to the response stream. |
| 52 | + /// </summary> |
| 53 | + /// <param name="buffer">The memory buffer to write.</param> |
| 54 | + /// <param name="cancellationToken">Optional cancellation token.</param> |
| 55 | + /// <returns>A task representing the asynchronous operation.</returns> |
| 56 | + /// <exception cref="InvalidOperationException">Thrown if the stream is already completed or an error has been reported.</exception> |
| 57 | + /// <exception cref="InvalidOperationException">Thrown if writing would exceed the 20 MiB limit.</exception> |
| 58 | + Task WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Reports an error that occurred during streaming. |
| 62 | + /// This will send error information via HTTP trailing headers. |
| 63 | + /// </summary> |
| 64 | + /// <param name="exception">The exception to report.</param> |
| 65 | + /// <param name="cancellationToken">Optional cancellation token.</param> |
| 66 | + /// <returns>A task representing the asynchronous operation.</returns> |
| 67 | + /// <exception cref="InvalidOperationException">Thrown if the stream is already completed or an error has already been reported.</exception> |
| 68 | + Task ReportErrorAsync(Exception exception, CancellationToken cancellationToken = default); |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Gets the total number of bytes written to the stream so far. |
| 72 | + /// </summary> |
| 73 | + long BytesWritten { get; } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Gets whether the stream has been completed. |
| 77 | + /// </summary> |
| 78 | + bool IsCompleted { get; } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Gets whether an error has been reported. |
| 82 | + /// </summary> |
| 83 | + bool HasError { get; } |
| 84 | + } |
| 85 | +} |
0 commit comments