MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
response.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mbgl/util/chrono.hpp>
4 
5 #include <optional>
6 #include <string>
7 #include <memory>
8 
9 namespace mbgl {
10 
11 class Response {
12 public:
13  Response() = default;
14  Response(const Response&);
16 
17 public:
18  class Error;
19  // When this object is empty, the response was successful.
20  std::unique_ptr<const Error> error;
21 
22  // This is set to true for 204 Not Modified responses, and, for backward compatibility,
23  // for 404 Not Found responses for tiles.
24  bool noContent = false;
25 
26  // This is set to true for 304 Not Modified responses.
27  bool notModified = false;
28 
29  // This is set to true when the server requested that no expired resources be used by
30  // specifying "Cache-Control: must-revalidate".
31  bool mustRevalidate = false;
32 
33  // The actual data of the response. Present only for non-error, non-notModified responses.
34  std::shared_ptr<const std::string> data;
35 
36  std::optional<Timestamp> modified;
37  std::optional<Timestamp> expires;
38  std::optional<std::string> etag;
39 
40  bool isFresh() const {
41  return expires ? *expires > util::now() : !error;
42  }
43 
44  // Indicates whether we are allowed to use this response according to HTTP caching rules.
45  // It may or may not be stale.
46  bool isUsable() const {
47  return !mustRevalidate || (expires && *expires > util::now());
48  }
49 };
50 
52 public:
53  enum class Reason : uint8_t {
54  Success = 1,
55  NotFound = 2,
56  Server = 3,
57  Connection = 4,
58  RateLimit = 5,
59  Other = 6,
61 
62  // An error message from the request handler, e.g. a server message or a system message
63  // informing the user about the reason for the failure.
65 
66  std::optional<Timestamp> retryAfter;
67 
68 public:
69  Error(Reason, std::string = "", std::optional<Timestamp> = std::nullopt);
70 };
71 
72 } // namespace mbgl
enum mbgl::Response::Error::Reason reason
std::string message
Definition: response.hpp:64
std::optional< Timestamp > retryAfter
Definition: response.hpp:66
Error(Reason, std::string="", std::optional< Timestamp >=std::nullopt)
Response()=default
bool isUsable() const
Definition: response.hpp:46
bool mustRevalidate
Definition: response.hpp:31
Response & operator=(const Response &)
bool isFresh() const
Definition: response.hpp:40
std::shared_ptr< const std::string > data
Definition: response.hpp:34
std::optional< Timestamp > expires
Definition: response.hpp:37
std::unique_ptr< const Error > error
Definition: response.hpp:18
Response(const Response &)
std::optional< Timestamp > modified
Definition: response.hpp:36
bool notModified
Definition: response.hpp:27
std::optional< std::string > etag
Definition: response.hpp:38
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
Timestamp now()
Definition: chrono.hpp:25
Definition: actor.hpp:15