virthttp  0.0
libvirt http interface
json_utils.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <rapidjson/document.h>
3 #include <virt_wrap/Error.hpp>
4 #include <virt_wrap/utility.hpp>
5 #include "wrapper/error_msg.hpp"
6 
7 template <typename T> struct JsonSpan {
8  T& wrapped;
9  constexpr JsonSpan() = delete;
10  constexpr explicit JsonSpan(T& ref) : wrapped(ref) {}
11  auto begin() const noexcept(noexcept(wrapped.Begin())) { return wrapped.Begin(); }
12  auto begin() noexcept(noexcept(wrapped.Begin())) { return wrapped.Begin(); }
13  auto end() const noexcept(noexcept(wrapped.End())) { return wrapped.End(); }
14  auto end() noexcept(noexcept(wrapped.End())) { return wrapped.End(); }
15 };
16 
21 struct JsonRes : public rapidjson::Document {
22  private:
23  [[nodiscard]] decltype(auto) json() const noexcept { return static_cast<const rapidjson::Document&>(*this); };
24  [[nodiscard]] decltype(auto) json() noexcept { return static_cast<rapidjson::Document&>(*this); };
25 
26  public:
27  JsonRes() {
28  SetObject();
29  rapidjson::Value results{}, success{}, errors{}, messages{};
30  results.SetArray();
31  success.SetBool(false);
32  errors.SetArray();
33  messages.SetArray();
34 
35  AddMember("results", results, GetAllocator());
36  AddMember("success", true, GetAllocator());
37  AddMember("errors", errors, GetAllocator());
38  AddMember("messages", messages, GetAllocator());
39  }
40 
45  template <typename T> void result(T&& val) { (*this)["results"].PushBack(val, GetAllocator()); }
50  template <typename T> void message(T&& val) { (*this)["messages"].PushBack(val, GetAllocator()); }
57  void error(int code) {
58  (*this)["success"] = false;
59  rapidjson::Value err{};
60  err.SetObject();
61  err.AddMember("code", code, GetAllocator());
62  {
63  const auto msg = error_messages[code];
64  err.AddMember("message", rapidjson::StringRef(msg.data(), msg.length()), GetAllocator());
65  }
66 
67  if (code > 100 || code < 0) {
68  if (auto vir_err = virt::extractLastError(); vir_err) {
69  const auto& msg = vir_err.message;
70  err.AddMember("libvirt", rapidjson::Value(msg.data(), msg.length(), GetAllocator()), GetAllocator());
71  }
72  }
73 
74  (*this)["errors"].PushBack(err, GetAllocator());
75  };
76 };
void error(int code)
Definition: json_utils.hpp:57
Definition: json_utils.hpp:7
class ErrorMessages error_messages
auto begin() const noexcept(noexcept(wrapped.Begin()))
Definition: json_utils.hpp:11
Definition: json_utils.hpp:21
auto begin() noexcept(noexcept(wrapped.Begin()))
Definition: json_utils.hpp:12
constexpr JsonSpan()=delete
constexpr JsonSpan(T &ref)
Definition: json_utils.hpp:10
void result(T &&val)
Definition: json_utils.hpp:45
auto end() noexcept(noexcept(wrapped.End()))
Definition: json_utils.hpp:14
auto end() const noexcept(noexcept(wrapped.End()))
Definition: json_utils.hpp:13
T & wrapped
Definition: json_utils.hpp:8
void message(T &&val)
Definition: json_utils.hpp:50
auto extractLastError() -> Error
Definition: Error.hpp:44
JsonRes()
Definition: json_utils.hpp:27