SOIL C++
C++ Unified Device Interface
Function.cpp
Go to the documentation of this file.
1#include "Function.h"
2
3
4SOIL::Function::Function(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string ontology) : Element(parent, uuid, name, description, ontology)
5{
6 if (uuid.substr(0, 3) != "FUN")
7 {
8 throw std::logic_error("UUIDs for functions must start with FUN!");
9 }
10
11 allowed_methods = { HTTP::Methods::GET, HTTP::Methods::POST, HTTP::Methods::OPTIONS, HTTP::Methods::HEAD };
12}
13
15{
16}
17
18std::shared_ptr<SOIL::Function> SOIL::Function::create(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string ontology)
19{
20 Function* function = new Function(parent, uuid, name, description, ontology);
21 return function->ptr();
22}
23
25{
26 HTTP::Json json_root = Element::wjson();
27 json_root[U("arguments")] = HTTP::Json::array();
28 int i = 0;
29 for (auto& a : arguments)
30 {
31 json_root[U("arguments")][i] = a.second->wjson();
32 i++;
33 }
34
35 json_root[U("returns")] = HTTP::Json::array();
36 i = 0;
37 for (auto& r : returns)
38 {
39 json_root[U("returns")][i] = r.second->wjson();
40 i++;
41 }
42 json_root[U("errors")] = HTTP::Json::array();
43 return json_root;
44}
45
47{
48 HTTP::Response response;
49 response.set_body(this->wjson());
50 response.set_status_code(HTTP::Status::OK);
51 return response;
52}
53
55{
56 auto task = request.extract_json();
57 task.wait();
58
59 std::map<std::string, HTTP::Json> arguments;
60 HTTP::Json body = task.get();
61
62 auto json_arguments = body[U("arguments")].as_array();
63
64 for (auto i = json_arguments.begin(); i != json_arguments.end(); i++)
65 {
66 auto json_argument = i->as_object();
67 std::string uuid = SOIL::to_value<std::string>(json_argument[U("uuid")]);
68 HTTP::Json value = json_argument[U("value")];
69
70 arguments[uuid] = value;
71 }
72
73 std::map<std::string, HTTP::Json> returns = this->invoke(arguments);
74
75 std::vector<web::json::value> json_returns;
76
77 for (auto i = returns.begin(); i != returns.end(); i++)
78 {
79 web::json::value local;
80 local[U("uuid")] = SOIL::to_json<std::string>(i->first);
81 local[U("value")] = i ->second;
82 json_returns.push_back(local);
83 }
84
86 json[U("returns")] =HTTP::Json::array(json_returns);
87
88 HTTP::Response response;
89 response.set_body(json);
90 response.set_status_code(HTTP::Status::OK);
91
92 return response;
93}
94
95std::map<std::string, HTTP::Json> SOIL::Function::invoke(std::map<std::string, HTTP::Json> arguments)
96{
97 throw std::logic_error("The method has not been implemented!");
98}
nlohmann::json json
std::vector< web::http::method > allowed_methods
Allowed methods.
Definition: Resource.h:42
SOIL Base Element.
Definition: Element.h:24
std::string uuid
Local UUID.
Definition: Element.h:53
virtual HTTP::Json wjson(void)
HTTP JSON.
Definition: Element.cpp:130
Function Class.
Definition: Function.h:23
DLL std::shared_ptr< Function > ptr(void)
Get Pointer.
Definition: Function.h:223
static std::shared_ptr< Function > create(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Create new Function.
Definition: Function.cpp:18
virtual DLL HTTP::Response handle_post(HTTP::Request request, std::smatch match=std::smatch())
Handle HTTP POST request.
Definition: Function.cpp:54
virtual DLL HTTP::Response handle_get(HTTP::Request request, std::smatch match=std::smatch())
Handle HTTP GET request.
Definition: Function.cpp:46
DLL ~Function()
Destructor.
Definition: Function.cpp:14
virtual DLL std::map< std::string, HTTP::Json > invoke(std::map< std::string, HTTP::Json > arguments)
Core Invocation.
Definition: Function.cpp:95
DLL Function(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Constructor.
Definition: Function.cpp:4
DLL HTTP::Json wjson(void)
HTTP JSON.
Definition: Function.cpp:24
web::json::value Json
HTTP JSON.
Definition: Types.h:39
web::http::http_request Request
HTTP Request.
Definition: Types.h:11
web::http::http_response Response
HTTP Response.
Definition: Types.h:18