aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
rest_reply.hpp
1 //
2 // rest_reply.hpp
3 // **************
4 //
5 // Copyright (c) 2019 Sharon W (sharon at aegis dot gg)
6 //
7 // Distributed under the MIT License. (See accompanying file LICENSE)
8 //
9 
10 #pragma once
11 
12 #include "aegis/config.hpp"
13 #include "aegis/utility.hpp"
14 #include "aegis/error.hpp"
15 #include <stdint.h>
16 #include <string>
17 #include <chrono>
18 
19 namespace aegis
20 {
21 
22 namespace rest
23 {
24 
25 using namespace std::chrono_literals;
26 
27 enum http_code
28 {
29  unknown = 0,
30 
32  continue_code = 100,
33  switching_protocols = 101,
34 
35  ok = 200,
36  created = 201,
37  accepted = 202,
38  non_authoritative_information = 203,
39  no_content = 204,
40  reset_content = 205,
41  partial_content = 206,
42 
43  multiple_choices = 300,
44  moved_permanently = 301,
45  found = 302,
46  see_other = 303,
47  not_modified = 304,
48  use_proxy = 305,
49  temporary_redirect = 307,
50 
51  bad_request = 400,
52  unauthorized = 401,
53  payment_required = 402,
54  forbidden = 403,
55  not_found = 404,
56  method_not_allowed = 405,
57  not_acceptable = 406,
58  proxy_authentication_required = 407,
59  request_timeout = 408,
60  conflict = 409,
61  gone = 410,
62  length_required = 411,
63  precondition_failed = 412,
64  request_entity_too_large = 413,
65  request_uri_too_long = 414,
66  unsupported_media_type = 415,
67  request_range_not_satisfiable = 416,
68  expectation_failed = 417,
69  im_a_teapot = 418,
70  upgrade_required = 426,
71  precondition_required = 428,
72  too_many_requests = 429,
73  request_header_fields_too_large = 431,
74 
75  internal_server_error = 500,
76  not_implemented = 501,
77  bad_gateway = 502,
78  service_unavailable = 503,
79  gateway_timeout = 504,
80  http_version_not_supported = 505,
81  not_extended = 510,
82  network_authentication_required = 511,
83 
85  unknown_error = 520,
86  server_down = 521,
87  timed_out = 522,
88  origin_unreachable = 523,
89  timeout_occurred = 524,
90  ssl_handshake_failed = 525,
91  invalid_ssl_certificate = 526,
92  railgun_error = 527,
93 
94  max_codes
95 };
96 
99 {
100 public:
101  explicit rest_reply(std::string const & msg) noexcept
102  : _msg{ msg }
103  , reply_code(http_code::unknown)
104  , execution_time(0ms)
105  {
106  }
107 
108  rest_reply() noexcept
109  : _msg("")
110  , reply_code(http_code::unknown)
111  , execution_time(0ms)
112  {
113 
114  }
115 
116  rest_reply(http_code reply_code, bool global, int32_t limit, int32_t remaining, int64_t reset, int32_t retry, const std::string & content, std::chrono::system_clock::time_point date, std::chrono::steady_clock::duration exec_time = 0ms) noexcept
117  : reply_code(reply_code)
118  , global(global)
119  , limit(limit)
120  , remaining(remaining)
121  , reset(reset)
122  , retry(retry)
123  , content(content)
124  , date(date)
125  , execution_time(exec_time)
126  {
127  }
128 
129  rest_reply(const std::string & msg, http_code reply_code = http_code::unknown, bool global = false, int32_t limit = 0, int32_t remaining = 0, int64_t reset = 0, int32_t retry = 0, const std::string & content = "", std::chrono::steady_clock::duration exec_time = 0ms) noexcept
130  : _msg(msg)
131  , reply_code(reply_code)
132  , global(global)
133  , limit(limit)
134  , remaining(remaining)
135  , reset(reset)
136  , retry(retry)
137  , content(content)
138  , execution_time(exec_time)
139  {
140  }
141 
142  rest_reply(rest_reply &&) = default;
143  rest_reply & operator=(const rest_reply &) = default;
144  rest_reply(const aegis::rest::rest_reply &) = default;
145 
146  operator bool()
147  {
148  if (reply_code == http_code::ok || reply_code == http_code::created || reply_code == http_code::accepted || reply_code == http_code::no_content)
149  return true;
150  return false;
151  }
152 
153  ~rest_reply() = default;
154 
155  bool success()
156  {
157  if (reply_code >= http_code::ok && reply_code <= http_code::partial_content)
158  return true;
159  return false;
160  }
161 
162 private:
163  std::string _msg;
164 
165 public:
166  http_code reply_code;
167  bool global = false;
168  int32_t limit = 0;
169  int32_t remaining = 0;
170  int64_t reset = 0;
171  int32_t retry = 0;
172  std::string content;
173  //bool permissions = true; /**< Whether the call had proper permissions */
174  std::chrono::system_clock::time_point date;
175  std::chrono::steady_clock::duration execution_time;
176  //TODO: std::map<std::string, std::string> headers; /**< Reply headers */
177 };
178 
179 }
180 
181 }
REST responses with error_code for possible exception throwing.
Definition: rest_reply.hpp:98
http_code reply_code
Definition: rest_reply.hpp:166
std::chrono::system_clock::time_point date
Definition: rest_reply.hpp:174
std::chrono::steady_clock::duration execution_time
Definition: rest_reply.hpp:175
std::string content
Definition: rest_reply.hpp:172