| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | #nullable enable |
| | | 10 | | |
| | | 11 | | namespace Azure |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Represents a long-running operation. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <typeparam name="T">The final result of the long-running operation.</typeparam> |
| | | 17 | | public abstract class Operation<T> where T : notnull |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets an ID representing the operation that can be used to poll for |
| | | 21 | | /// the status of the long-running operation. |
| | | 22 | | /// </summary> |
| | | 23 | | public abstract string Id { get; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Final result of the long-running operation. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <remarks> |
| | | 29 | | /// This property can be accessed only after the operation completes successfully (HasValue is true). |
| | | 30 | | /// </remarks> |
| | | 31 | | public abstract T Value { get; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The last HTTP response received from the server. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <remarks> |
| | | 37 | | /// The last response returned from the server during the lifecycle of this instance. |
| | | 38 | | /// An instance of Operation<typeparamref name="T"/> sends requests to a server in UpdateStatusAsync, UpdateStat |
| | | 39 | | /// Responses from these requests can be accessed using GetRawResponse. |
| | | 40 | | /// </remarks> |
| | | 41 | | public abstract Response GetRawResponse(); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Returns true if the long-running operation completed. |
| | | 45 | | /// </summary> |
| | | 46 | | public abstract bool HasCompleted { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Returns true if the long-running operation completed successfully and has produced final result (accessible |
| | | 50 | | /// </summary> |
| | | 51 | | public abstract bool HasValue { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Periodically calls the server till the long-running operation completes. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the periodical service calls.</pa |
| | | 57 | | /// <returns>The last HTTP response received from the server.</returns> |
| | | 58 | | /// <remarks> |
| | | 59 | | /// This method will periodically call UpdateStatusAsync till HasCompleted is true, then return the final result |
| | | 60 | | /// </remarks> |
| | | 61 | | public abstract ValueTask<Response<T>> WaitForCompletionAsync(CancellationToken cancellationToken = default); |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Periodically calls the server till the long-running operation completes. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="pollingInterval"> |
| | | 67 | | /// The interval between status requests to the server. |
| | | 68 | | /// The interval can change based on information returned from the server. |
| | | 69 | | /// For example, the server might communicate to the client that there is not reason to poll for status change s |
| | | 70 | | /// </param> |
| | | 71 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the periodical service calls.</pa |
| | | 72 | | /// <returns>The last HTTP response received from the server.</returns> |
| | | 73 | | /// <remarks> |
| | | 74 | | /// This method will periodically call UpdateStatusAsync till HasCompleted is true, then return the final result |
| | | 75 | | /// </remarks> |
| | | 76 | | public abstract ValueTask<Response<T>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancel |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Calls the server to get updated status of the long-running operation. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param> |
| | | 82 | | /// <returns>The HTTP response received from the server.</returns> |
| | | 83 | | /// <remarks> |
| | | 84 | | /// This operation will update the value returned from GetRawResponse and might update HasCompleted, HasValue, a |
| | | 85 | | /// </remarks> |
| | | 86 | | public abstract ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default); |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Calls the server to get updated status of the long-running operation. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param> |
| | | 92 | | /// <returns>The HTTP response received from the server.</returns> |
| | | 93 | | /// <remarks> |
| | | 94 | | /// This operation will update the value returned from GetRawResponse and might update HasCompleted, HasValue, a |
| | | 95 | | /// </remarks> |
| | | 96 | | public abstract Response UpdateStatus(CancellationToken cancellationToken = default); |
| | | 97 | | |
| | | 98 | | /// <inheritdoc /> |
| | | 99 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 100 | | public override bool Equals(object obj) => base.Equals(obj); |
| | | 101 | | |
| | | 102 | | /// <inheritdoc /> |
| | | 103 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 104 | | public override int GetHashCode() => base.GetHashCode(); |
| | | 105 | | |
| | | 106 | | /// <inheritdoc /> |
| | | 107 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 108 | | public override string ToString() => base.ToString(); |
| | | 109 | | } |
| | | 110 | | } |