azure-core
operation.hpp
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // SPDX-License-Identifier: MIT
3 
9 #pragma once
10 
11 #include "azure/core/context.hpp"
13 #include "azure/core/response.hpp"
14 
15 #include <chrono>
16 #include <memory>
17 #include <stdexcept>
18 #include <string>
19 
20 namespace 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  virtual Azure::Core::Http::RawResponse const& GetRawResponseInternal() const = 0;
34 
35  protected:
36  std::unique_ptr<Azure::Core::Http::RawResponse> m_rawResponse = nullptr;
38 
43  Operation() = default;
44 
45  // Define how an Operation<T> can be move-constructed from rvalue other. Parameter `other`
46  // gave up ownership for the rawResponse.
53  : m_rawResponse(std::move(other.m_rawResponse)), m_status(other.m_status)
54  {
55  }
56 
57  // Define how an Operation<T> can be copy-constructed from some other Operation reference.
58  // Operation will create a clone of the rawResponse from `other`.
64  Operation(Operation const& other)
65  : m_rawResponse(std::make_unique<Http::RawResponse>(other.GetRawResponse())),
66  m_status(other.m_status)
67  {
68  }
69 
70  // Define how an Operation<T> can be move-assigned from rvalue other. Parameter `other`
71  // gave up ownership for the rawResponse.
80  {
81  this->m_rawResponse = std::move(other.m_rawResponse);
82  this->m_status = other.m_status;
83  return *this;
84  }
85 
86  // Define how an Operation<T> can be copy-assigned from some other Operation reference.
87  // Operation will create a clone of the rawResponse from `other`.
95  Operation& operator=(Operation const& other)
96  {
97  this->m_rawResponse = std::make_unique<Http::RawResponse>(other.GetRawResponse());
98  this->m_status = other.m_status;
99  return *this;
100  }
101 
102  public:
107  virtual ~Operation() {}
108 
114  virtual T Value() const = 0;
115 
122  virtual std::string GetResumeToken() const = 0;
123 
130  {
131  if (!m_rawResponse)
132  {
133  throw std::runtime_error("The raw response was not yet set for the Operation.");
134  }
135  return *m_rawResponse;
136  }
137 
142  OperationStatus Status() const noexcept { return m_status; }
143 
149  bool IsDone() const noexcept
150  {
151  return (
152  m_status == OperationStatus::Succeeded || m_status == OperationStatus::Cancelled
153  || m_status == OperationStatus::Failed);
154  }
155 
163  bool HasValue() const noexcept { return (m_status == OperationStatus::Succeeded); }
164 
171  {
172  // In the cases where the customer doesn't want to use a context we new one up and pass it
173  // through
175  }
176 
184  Http::RawResponse const& Poll(Context const& context)
185  {
186  context.ThrowIfCancelled();
187  m_rawResponse = PollInternal(context);
188  return *m_rawResponse;
189  }
190 
198  Response<T> PollUntilDone(std::chrono::milliseconds period)
199  {
200  // In the cases where the customer doesn't want to use a context we new one up and pass it
201  // through
203  }
204 
213  Response<T> PollUntilDone(std::chrono::milliseconds period, Context& context)
214  {
215  context.ThrowIfCancelled();
216  return PollUntilDoneInternal(period, context);
217  }
218  };
219 }} // namespace Azure::Core
Azure::Core::Operation::Poll
Http::RawResponse const & Poll()
Gets updated status of the long-running operation.
Definition: operation.hpp:170
Azure::Core::OperationStatus::Succeeded
static AZ_CORE_DLLEXPORT const OperationStatus Succeeded
The Azure::Core::Operation Succeeded.
Definition: operation_status.hpp:91
Azure::Core::Context::ThrowIfCancelled
void ThrowIfCancelled() const
Checks if the context is cancelled.
Definition: context.hpp:248
Azure::Core::Operation::IsDone
bool IsDone() const noexcept
Checks if the long-running operation is completed.
Definition: operation.hpp:149
Azure::Core::Operation::PollUntilDone
Response< T > PollUntilDone(std::chrono::milliseconds period)
Periodically polls till the long-running operation completes.
Definition: operation.hpp:198
Azure::Core::Operation::GetRawResponse
Azure::Core::Http::RawResponse const & GetRawResponse() const
Gets the raw HTTP response.
Definition: operation.hpp:129
context.hpp
Context for canceling long running operations.
Azure::Core::Operation::~Operation
virtual ~Operation()
Destructs the Operation.
Definition: operation.hpp:107
Azure::Core::OperationStatus::NotStarted
static AZ_CORE_DLLEXPORT const OperationStatus NotStarted
The Azure::Core::Operation is Not Started.
Definition: operation_status.hpp:81
Azure::Core::Operation::operator=
Operation & operator=(Operation &&other)
Assigns an instance of Operation by moving in another instance.
Definition: operation.hpp:79
Azure::Core::Operation::GetResumeToken
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...
Azure::Core::Operation
Methods starting long-running operations return Operation<T> types.
Definition: operation.hpp:27
Azure::Core::Operation::Operation
Operation(Operation &&other)
Constructs an instance of Operation by moving in another instance.
Definition: operation.hpp:52
response.hpp
Wraps the raw HTTP response from a request made to the service into a response of a specific type.
Azure::Response
Represents the result of an Azure operation over HTTP by wrapping the raw HTTP response from a reques...
Definition: response.hpp:25
Azure::Core::Http::RawResponse
After receiving and interpreting a request message, a server responds with an HTTP response message.
Definition: raw_response.hpp:24
Azure
Azure SDK abstractions.
Definition: azure_assert.hpp:55
Azure::Core::Operation::Operation
Operation(Operation const &other)
Constructs an instance of Operation by copying another instance.
Definition: operation.hpp:64
Azure::Core::Operation::Poll
Http::RawResponse const & Poll(Context const &context)
Gets updated status of the long-running operation.
Definition: operation.hpp:184
Azure::Core::OperationStatus
Long-running operation states.
Definition: operation_status.hpp:24
Azure::Core::OperationStatus::Failed
static AZ_CORE_DLLEXPORT const OperationStatus Failed
The Azure::Core::Operation Failed.
Definition: operation_status.hpp:101
Azure::Core::Operation::operator=
Operation & operator=(Operation const &other)
Assigns another Operation instance by copying.
Definition: operation.hpp:95
Azure::Core::Operation::PollUntilDone
Response< T > PollUntilDone(std::chrono::milliseconds period, Context &context)
Periodically polls till the long-running operation completes;.
Definition: operation.hpp:213
Azure::Core::Operation::HasValue
bool HasValue() const noexcept
Checks if the long-running operation completed successfully and has produced a final result.
Definition: operation.hpp:163
Azure::Core::Operation::Operation
Operation()=default
Constructs a default instance of Operation.
Azure::Core::OperationStatus::Cancelled
static AZ_CORE_DLLEXPORT const OperationStatus Cancelled
The Azure::Core::Operation was Cancelled.
Definition: operation_status.hpp:96
Azure::Core::Operation::Value
virtual T Value() const =0
Final result of the long-running operation.
operation_status.hpp
Valid states for long-running Operations. Services can extend upon the default set of values.
Azure::Core::Context
A context is a node within a tree that represents deadlines and key/value pairs.
Definition: context.hpp:45
Azure::Core::Operation::Status
OperationStatus Status() const noexcept
Gets the current Azure::Core::OperationStatus of the long-running operation.
Definition: operation.hpp:142
Azure::Core::Context::ApplicationContext
static AZ_CORE_DLLEXPORT Context ApplicationContext
The application context (root).
Definition: context.hpp:276