Fawkes API  Fawkes Development Version
PluginOpRequest.cpp
1 
2 /****************************************************************************
3  * PluginOpRequest
4  * (auto-generated, do not modify directly)
5  *
6  * Fawkes Plugin REST API.
7  * List, load, and unload plugins.
8  *
9  * API Contact: Tim Niemueller <niemueller@kbsg.rwth-aachen.de>
10  * API Version: v1beta1
11  * API License: Apache 2.0
12  ****************************************************************************/
13 
14 #include "PluginOpRequest.h"
15 
16 #include <rapidjson/document.h>
17 #include <rapidjson/prettywriter.h>
18 #include <rapidjson/stringbuffer.h>
19 #include <rapidjson/writer.h>
20 
21 #include <sstream>
22 
24 {
25 }
26 
27 PluginOpRequest::PluginOpRequest(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 PluginOpRequest::PluginOpRequest(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 PluginOpRequest::to_json(bool pretty) const
43 {
44  rapidjson::Document d;
45 
46  to_json_value(d, d);
47 
48  rapidjson::StringBuffer buffer;
49  if (pretty) {
50  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
51  d.Accept(writer);
52  } else {
53  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
54  d.Accept(writer);
55  }
56 
57  return buffer.GetString();
58 }
59 
60 void
61 PluginOpRequest::to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
62 {
63  rapidjson::Document::AllocatorType &allocator = d.GetAllocator();
64  v.SetObject();
65  // Avoid unused variable warnings
66  (void)allocator;
67 
68  if (kind_) {
69  rapidjson::Value v_kind;
70  v_kind.SetString(*kind_, allocator);
71  v.AddMember("kind", v_kind, allocator);
72  }
73  if (apiVersion_) {
74  rapidjson::Value v_apiVersion;
75  v_apiVersion.SetString(*apiVersion_, allocator);
76  v.AddMember("apiVersion", v_apiVersion, allocator);
77  }
78  if (desired_state_) {
79  rapidjson::Value v_desired_state;
80  v_desired_state.SetString(*desired_state_, allocator);
81  v.AddMember("desired_state", v_desired_state, allocator);
82  }
83 }
84 
85 void
86 PluginOpRequest::from_json(const std::string &json)
87 {
88  rapidjson::Document d;
89  d.Parse(json);
90 
91  from_json_value(d);
92 }
93 
94 void
95 PluginOpRequest::from_json_value(const rapidjson::Value &d)
96 {
97  if (d.HasMember("kind") && d["kind"].IsString()) {
98  kind_ = d["kind"].GetString();
99  }
100  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
101  apiVersion_ = d["apiVersion"].GetString();
102  }
103  if (d.HasMember("desired_state") && d["desired_state"].IsString()) {
104  desired_state_ = d["desired_state"].GetString();
105  }
106 }
107 
108 void
109 PluginOpRequest::validate(bool subcall) const
110 {
111  std::vector<std::string> missing;
112  if (!kind_)
113  missing.push_back("kind");
114  if (!apiVersion_)
115  missing.push_back("apiVersion");
116  if (!desired_state_)
117  missing.push_back("desired_state");
118 
119  if (!missing.empty()) {
120  if (subcall) {
121  throw missing;
122  } else {
123  std::ostringstream s;
124  s << "PluginOpRequest is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
125  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
126  s << missing[i];
127  if (i < (missing.size() - 1)) {
128  s << ", ";
129  }
130  }
131  throw std::runtime_error(s.str());
132  }
133  }
134 }
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
virtual ~PluginOpRequest()
Destructor.
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
PluginOpRequest()
Constructor.