-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrequest.cpp
More file actions
53 lines (41 loc) · 1.42 KB
/
request.cpp
File metadata and controls
53 lines (41 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* API Request.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <villas/api.hpp>
#include <villas/api/request.hpp>
#include <villas/plugin.hpp>
using namespace villas;
using namespace villas::node::api;
void Request::decode() {
body = buffer.decode();
if (!body)
throw Error::badRequest(nullptr, "Failed to decode request payload");
}
std::string Request::toString() {
return fmt::format("endpoint={}, method={}", factory->getName(),
Session::methodToString(method));
}
Request *RequestFactory::create(Session *s, const std::string &uri,
Session::Method meth, unsigned long ct) {
s->logger->debug("Lookup request handler for: uri={}", uri);
for (auto *rf : plugin::registry->lookup<RequestFactory>()) {
std::smatch mr;
if (not rf->match(uri, mr))
continue;
auto *p = rf->make(s);
for (auto m : mr)
p->matches.push_back(m.str());
p->factory = rf;
p->method = meth;
p->contentLength = ct;
p->prepare();
return p;
}
throw Error::badRequest(json_pack("{ s: s, s: s }", "uri", uri.c_str(),
"method",
Session::methodToString(meth).c_str()),
"Unknown API request");
}