| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.IO; |
| | | 8 | | using Azure.Core; |
| | | 9 | | |
| | | 10 | | namespace Azure |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents the HTTP response from the service. |
| | | 14 | | /// </summary> |
| | | 15 | | public abstract class Response : IDisposable |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets the HTTP status code. |
| | | 19 | | /// </summary> |
| | | 20 | | public abstract int Status { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the HTTP reason phrase. |
| | | 24 | | /// </summary> |
| | | 25 | | public abstract string ReasonPhrase { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the contents of HTTP response. Returns <c>null</c> for responses without content. |
| | | 29 | | /// </summary> |
| | | 30 | | public abstract Stream? ContentStream { get; set; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the client request id that was sent to the server as <c>x-ms-client-request-id</c> headers. |
| | | 34 | | /// </summary> |
| | | 35 | | public abstract string ClientRequestId { get; set; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Get the HTTP response headers. |
| | | 39 | | /// </summary> |
| | 4952 | 40 | | public virtual ResponseHeaders Headers => new ResponseHeaders(this); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Frees resources held by this <see cref="Response"/> instance. |
| | | 44 | | /// </summary> |
| | | 45 | | public abstract void Dispose(); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Returns header value if the header is stored in the collection. If header has multiple values they are going |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="name">The header name.</param> |
| | | 51 | | /// <param name="value">The reference to populate with value.</param> |
| | | 52 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | | 53 | | protected internal abstract bool TryGetHeader(string name, [NotNullWhen(true)] out string? value); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Returns header values if the header is stored in the collection. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="name">The header name.</param> |
| | | 59 | | /// <param name="values">The reference to populate with values.</param> |
| | | 60 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | | 61 | | protected internal abstract bool TryGetHeaderValues(string name, [NotNullWhen(true)] out IEnumerable<string>? va |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Returns <c>true</c> if the header is stored in the collection. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="name">The header name.</param> |
| | | 67 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | | 68 | | protected internal abstract bool ContainsHeader(string name); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Returns an iterator for enumerating <see cref="HttpHeader"/> in the response. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <returns>The <see cref="IEnumerable{T}"/> enumerating <see cref="HttpHeader"/> in the response.</returns> |
| | | 74 | | protected internal abstract IEnumerable<HttpHeader> EnumerateHeaders(); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Creates a new instance of <see cref="Response{T}"/> with the provided value and HTTP response. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <typeparam name="T">The type of the value.</typeparam> |
| | | 80 | | /// <param name="value">The value.</param> |
| | | 81 | | /// <param name="response">The HTTP response.</param> |
| | | 82 | | /// <returns>A new instance of <see cref="Response{T}"/> with the provided value and HTTP response.</returns> |
| | | 83 | | public static Response<T> FromValue<T>(T value, Response response) |
| | | 84 | | { |
| | 22 | 85 | | return new ValueResponse<T>(response, value); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Returns the string representation of this <see cref="Response"/>. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <returns>The string representation of this <see cref="Response"/></returns> |
| | | 92 | | public override string ToString() |
| | | 93 | | { |
| | 2 | 94 | | return $"Status: {Status}, ReasonPhrase: {ReasonPhrase}"; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |