aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
rest_controller.hpp
1 //
2 // rest_controller.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/fwd.hpp"
14 #include "aegis/rest/rest_reply.hpp"
15 #include <asio/ip/basic_resolver.hpp>
16 #include <asio/ip/tcp.hpp>
17 #include <string>
18 #include <map>
19 #include <functional>
20 
21 namespace aegis
22 {
23 
24 namespace rest
25 {
26 
27 enum RequestMethod
28 {
29  Get,
30  Post,
31  Put,
32  Patch,
33  Delete,
34  MAX_METHODS
35 };
36 
37 struct aegis_file
38 {
39  std::string name;
40  std::vector<char> data;
41 };
42 
44 {
45  std::string path;
46  RequestMethod method = Post;
47  std::string body;
48  std::string host;
49  std::string port = "443";
50  std::vector<std::string> headers;
51  std::string _path_ex;
52  lib::optional<aegis_file> file;
53 };
54 
56 {
57 public:
58  AEGIS_DECL rest_controller(const std::string & token, asio::io_context * _io_context);
59  AEGIS_DECL rest_controller(const std::string & token, const std::string & prefix, asio::io_context * _io_context);
60  AEGIS_DECL rest_controller(const std::string & token, const std::string & prefix, const std::string & host, asio::io_context * _io_context);
61  ~rest_controller() = default;
62 
63  rest_controller(const rest_controller &) = delete;
64  rest_controller(rest_controller &&) = delete;
65  rest_controller & operator=(const rest_controller &) = delete;
66 
68 
75  AEGIS_DECL rest_reply execute(rest::request_params && params);
76 
78 
85  AEGIS_DECL rest_reply execute2(rest::request_params && params);
86 
87  static std::string get_method(RequestMethod method) noexcept
88  {
89  switch (method)
90  {
91  case Post:
92  return "POST";
93  case Put:
94  return "PUT";
95  case Patch:
96  return "PATCH";
97  case Delete:
98  return "DELETE";
99  case Get:
100  default:
101  return "GET";
102  }
103  }
104 
105  void set_prefix(const std::string & prefix) noexcept
106  {
107  _prefix = prefix;
108  }
109 
110  std::chrono::hours tz_bias()
111  {
112  return _tz_bias;
113  }
114 
115  void tz_bias(std::chrono::hours bias)
116  {
117  _tz_bias = bias;
118  }
119 
120 private:
121  friend aegis::core;
122  std::string _token;
123  std::string _prefix;
124  std::string _host;
125  std::unordered_map<std::string, asio::ip::basic_resolver<asio::ip::tcp>::results_type> _resolver_cache;
126 
127  using rest_end_t = std::function<void(std::chrono::steady_clock::time_point, uint16_t)>;
128  rest_end_t rest_end;
129  asio::io_context * _io_context = nullptr;
130  std::chrono::hours _tz_bias = 0h;
131 };
132 
133 }
134 
135 }
136 
137 #if defined(AEGIS_HEADER_ONLY)
138 #include "aegis/rest/impl/rest_controller.cpp"
139 #endif
rest_reply execute2(rest::request_params &&params)
Performs an HTTP request using the params provided.
Definition: rest_controller.cpp:191
Definition: rest_controller.hpp:55
REST responses with error_code for possible exception throwing.
Definition: rest_reply.hpp:98
Definition: rest_controller.hpp:43
Primary class for managing a bot interface.
Definition: core.hpp:157
rest_reply execute(rest::request_params &&params)
Performs an HTTP request using the params provided.
Definition: rest_controller.cpp:60
Definition: rest_controller.hpp:37