azure-core
Loading...
Searching...
No Matches
operation.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9#pragma once
10
14
15#include <chrono>
16#include <memory>
17#include <stdexcept>
18#include <string>
19
20namespace Azure { namespace Core {
21
27 template <class T> class Operation {
28 private:
29 // These are pure virtual b/c the derived class must provide an implementation
30 virtual std::unique_ptr<Http::RawResponse> PollInternal(Context const& context) = 0;
31 virtual Response<T> PollUntilDoneInternal(std::chrono::milliseconds period, Context& context)
32 = 0;
33
34 [[deprecated("Do not override and do not use.")]] virtual Azure::Core::Http::RawResponse const&
35 GetRawResponseInternal() const
36 {
37 return *m_rawResponse;
38 }
39
40 protected:
42 std::unique_ptr<Azure::Core::Http::RawResponse> m_rawResponse = nullptr;
43
46
51 Operation() = default;
52
53 // Define how an Operation<T> can be move-constructed from rvalue other. Parameter `other`
54 // gave up ownership for the rawResponse.
61 : m_rawResponse(std::move(other.m_rawResponse)), m_status(other.m_status)
62 {
63 }
64
65 // Define how an Operation<T> can be copy-constructed from some other Operation reference.
66 // Operation will create a clone of the rawResponse from `other`.
72 Operation(Operation const& other)
73 : m_rawResponse(std::make_unique<Http::RawResponse>(other.GetRawResponse())),
74 m_status(other.m_status)
75 {
76 }
77
78 // Define how an Operation<T> can be move-assigned from rvalue other. Parameter `other`
79 // gave up ownership for the rawResponse.
88 {
89 this->m_rawResponse = std::move(other.m_rawResponse);
90 this->m_status = other.m_status;
91 return *this;
92 }
93
94 // Define how an Operation<T> can be copy-assigned from some other Operation reference.
95 // Operation will create a clone of the rawResponse from `other`.
104 {
105 this->m_rawResponse = std::make_unique<Http::RawResponse>(other.GetRawResponse());
106 this->m_status = other.m_status;
107 return *this;
108 }
109
110 public:
115 virtual ~Operation() {}
116
122 virtual T Value() const = 0;
123
130 virtual std::string GetResumeToken() const = 0;
131
138 {
139 if (!m_rawResponse)
140 {
141 throw std::runtime_error("The raw response was not yet set for the Operation.");
142 }
143 return *m_rawResponse;
144 }
145
150 OperationStatus const& Status() const noexcept { return m_status; }
151
157 bool IsDone() const noexcept
158 {
159 return (
162 }
163
171 bool HasValue() const noexcept { return (m_status == OperationStatus::Succeeded); }
172
179 {
180 // In the cases where the customer doesn't want to use a context we new one up and pass it
181 // through
182 return Poll(Context{});
183 }
184
192 Http::RawResponse const& Poll(Context const& context)
193 {
194 context.ThrowIfCancelled();
195 m_rawResponse = PollInternal(context);
196 return *m_rawResponse;
197 }
198
206 Response<T> PollUntilDone(std::chrono::milliseconds period)
207 {
208 // In the cases where the customer doesn't want to use a context we new one up and pass it
209 // through
210 Context context;
211 return PollUntilDone(period, context);
212 }
213
222 Response<T> PollUntilDone(std::chrono::milliseconds period, Context& context)
223 {
224 context.ThrowIfCancelled();
225 return PollUntilDoneInternal(period, context);
226 }
227 };
228}} // namespace Azure::Core
A context is a node within a unidirectional tree that represents deadlines and key/value pairs.
Definition context.hpp:72
void ThrowIfCancelled() const
Throws if the context is cancelled.
Definition context.hpp:364
After receiving and interpreting a request message, a server responds with an HTTP response message.
Definition raw_response.hpp:24
Methods starting long-running operations return Operation<T> types.
Definition operation.hpp:27
Operation()=default
Constructs a default instance of Operation.
std::unique_ptr< Azure::Core::Http::RawResponse > m_rawResponse
the underlying raw response for this operation.
Definition operation.hpp:42
Response< T > PollUntilDone(std::chrono::milliseconds period, Context &context)
Periodically polls till the long-running operation completes;.
Definition operation.hpp:222
bool IsDone() const noexcept
Checks if the long-running operation is completed.
Definition operation.hpp:157
Http::RawResponse const & Poll(Context const &context)
Gets updated status of the long-running operation.
Definition operation.hpp:192
virtual std::string GetResumeToken() const =0
Gets a token representing the operation that can be used to poll for the status of the long-running o...
Operation(Operation &&other)
Constructs an instance of Operation by moving in another instance.
Definition operation.hpp:60
OperationStatus m_status
the current status of the operation.
Definition operation.hpp:45
virtual ~Operation()
Destructs the Operation.
Definition operation.hpp:115
Operation(Operation const &other)
Constructs an instance of Operation by copying another instance.
Definition operation.hpp:72
Http::RawResponse const & Poll()
Gets updated status of the long-running operation.
Definition operation.hpp:178
Operation & operator=(Operation const &other)
Assigns another Operation instance by copying.
Definition operation.hpp:103
OperationStatus const & Status() const noexcept
Gets the current Azure::Core::OperationStatus of the long-running operation.
Definition operation.hpp:150
Operation & operator=(Operation &&other)
Assigns an instance of Operation by moving in another instance.
Definition operation.hpp:87
virtual T Value() const =0
Final result of the long-running operation.
Azure::Core::Http::RawResponse const & GetRawResponse() const
Gets the raw HTTP response.
Definition operation.hpp:137
bool HasValue() const noexcept
Checks if the long-running operation completed successfully and has produced a final result.
Definition operation.hpp:171
Response< T > PollUntilDone(std::chrono::milliseconds period)
Periodically polls till the long-running operation completes.
Definition operation.hpp:206
Long-running operation states.
Definition operation_status.hpp:23
static AZ_CORE_DLLEXPORT const OperationStatus Failed
The Azure::Core::Operation Failed.
Definition operation_status.hpp:100
static AZ_CORE_DLLEXPORT const OperationStatus Cancelled
The Azure::Core::Operation was Cancelled.
Definition operation_status.hpp:95
static AZ_CORE_DLLEXPORT const OperationStatus NotStarted
The Azure::Core::Operation is Not Started.
Definition operation_status.hpp:80
static AZ_CORE_DLLEXPORT const OperationStatus Succeeded
The Azure::Core::Operation Succeeded.
Definition operation_status.hpp:90
Represents the result of an Azure operation over HTTP by wrapping the raw HTTP response from a reques...
Definition response.hpp:26
Context for canceling long running operations.
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57
Valid states for long-running Operations. Services can extend upon the default set of values.
Wraps the raw HTTP response from a request made to the service into a response of a specific type.