azure-core
paged_response.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 <cstdlib>
12 #include <string>
13 
14 #include "azure/core/context.hpp"
16 #include "azure/core/nullable.hpp"
17 
18 namespace Azure { namespace Core {
19 
29  template <class T> class PagedResponse {
30  private:
31  // The field used to check when the end of the response is reached. We default it true as the
32  // starting point because all responses from a service will always come with a payload that
33  // represents at least one page. The page might or might not contain elements in the page.
34  // `m_hasPage` is then turned to `false` once `MoveToNextPage` is called on the last page.
35  bool m_hasPage = true;
36 
37  protected:
42  PagedResponse() = default;
43 
49 
55 
56  public:
61  virtual ~PagedResponse() = default;
62 
67  std::string CurrentPageToken;
68 
79 
84  std::unique_ptr<Azure::Core::Http::RawResponse> RawResponse;
85 
93  bool HasPage() const { return m_hasPage; }
94 
103  {
104  static_assert(
105  std::is_base_of<PagedResponse, T>::value,
106  "The template argument \"T\" should derive from PagedResponse<T>.");
107 
108  if (!NextPageToken.HasValue() || NextPageToken.Value().empty())
109  {
110  m_hasPage = false;
111  return;
112  }
113 
114  // Developer must make sure current page is kept unchanged if OnNextPage()
115  // throws exception.
116  static_cast<T*>(this)->OnNextPage(context);
117  }
118  };
119 
120 }} // namespace Azure::Core
Azure::Nullable::HasValue
bool HasValue() const noexcept
Check whether a value is contained.
Definition: nullable.hpp:229
Azure::Nullable::Value
const T & Value() const &noexcept
Get the contained value.
Definition: nullable.hpp:235
Azure::Core::PagedResponse::CurrentPageToken
std::string CurrentPageToken
The token used to fetch the current page.
Definition: paged_response.hpp:67
raw_response.hpp
Define the HTTP raw response.
Azure::Core::PagedResponse::NextPageToken
Azure::Nullable< std::string > NextPageToken
The token for getting the next page.
Definition: paged_response.hpp:78
context.hpp
Context for canceling long running operations.
Azure::Core::PagedResponse::PagedResponse
PagedResponse(PagedResponse &&)=default
Constructs PagedResponse by moving in another instance.
Azure::Nullable< std::string >
Azure::Core::PagedResponse::MoveToNextPage
void MoveToNextPage(const Azure::Core::Context &context=Azure::Core::Context())
Moves to the next page of the response.
Definition: paged_response.hpp:102
Azure::Core::PagedResponse::operator=
PagedResponse & operator=(PagedResponse &&)=default
Assigns another instance of PagedResponse by moving it in.
Azure::Core::PagedResponse::~PagedResponse
virtual ~PagedResponse()=default
Destructs PagedResponse.
Azure
Azure SDK abstractions.
Definition: azure_assert.hpp:55
nullable.hpp
Manages an optional contained value, i.e. a value that may or may not be present.
Azure::Core::PagedResponse
The base type and behavior for a paged response.
Definition: paged_response.hpp:29
Azure::Core::Context
A context is a node within a tree that represents deadlines and key/value pairs.
Definition: context.hpp:45
Azure::Core::PagedResponse::RawResponse
std::unique_ptr< Azure::Core::Http::RawResponse > RawResponse
The HTTP response returned by the service.
Definition: paged_response.hpp:84
Azure::Core::PagedResponse::HasPage
bool HasPage() const
Checks if a page exists.
Definition: paged_response.hpp:93
Azure::Core::PagedResponse::PagedResponse
PagedResponse()=default
Constructs a default instance of PagedResponse.