http json api
This commit is contained in:
parent
2af8578cf2
commit
db8876277c
@ -1,5 +1,7 @@
|
|||||||
|
find_package(nlohmann_json 3 REQUIRED)
|
||||||
|
|
||||||
file(GLOB_RECURSE HTTP_SOURCES "./*.cpp")
|
file(GLOB_RECURSE HTTP_SOURCES "./*.cpp")
|
||||||
add_library(http SHARED ${HTTP_SOURCES})
|
add_library(http SHARED ${HTTP_SOURCES})
|
||||||
target_link_libraries(http librum.a)
|
target_link_libraries(http librum.a nlohmann_json::nlohmann_json)
|
||||||
# target_link_libraries(http librum.a libflags-cpp.so)
|
# target_link_libraries(http librum.a libflags-cpp.so)
|
||||||
set_target_properties(http PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins")
|
set_target_properties(http PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins")
|
@ -6,9 +6,13 @@
|
|||||||
#include <rum/tcp/error.h>
|
#include <rum/tcp/error.h>
|
||||||
// #include <flags-cpp/flags-cpp.h>
|
// #include <flags-cpp/flags-cpp.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <nlohmann/detail/exceptions.hpp>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using namespace Rum::HTTP;
|
using namespace Rum::HTTP;
|
||||||
|
using json = nlohmann::json;
|
||||||
// using namespace Flags;
|
// using namespace Flags;
|
||||||
|
|
||||||
Server* server;
|
Server* server;
|
||||||
@ -52,6 +56,32 @@ int run() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
server->add_path<POST>("/add", [](const Request& req, Response& resp) {
|
||||||
|
std::cout << (std::string)req << std::endl;
|
||||||
|
// std::cout << req.get_body() << std::endl;
|
||||||
|
|
||||||
|
try {
|
||||||
|
json data = json::parse(req.get_body());
|
||||||
|
if (!data.contains("chart") || !data["chart"].is_number_integer() || !data.contains("series") || !data["series"].is_string() ||
|
||||||
|
!data.contains("points") || !data["points"].is_array()) {
|
||||||
|
resp.set_code(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<point_t> points;
|
||||||
|
for (auto point : data["points"]) {
|
||||||
|
if (!point.is_object() || !point.contains("x") || !point.contains("y") || !point["x"].is_number() || !point["y"].is_number())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
points.push_back({.x = point["x"].template get<double>(), .y = point["y"].template get<double>()});
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin.update_callback(data["chart"].template get<int>(), data["series"].template get<std::string>().c_str(), points.data(), points.size());
|
||||||
|
} catch (nlohmann::detail::parse_error e) {
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
std::cout << "starting http server" << std::endl;
|
std::cout << "starting http server" << std::endl;
|
||||||
server->listen();
|
server->listen();
|
||||||
|
|
||||||
|
38
test/test.py
38
test/test.py
@ -3,14 +3,46 @@
|
|||||||
import requests
|
import requests
|
||||||
from math import sin
|
from math import sin
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
import numpy as np
|
||||||
|
import json
|
||||||
|
|
||||||
def main():
|
def sine():
|
||||||
x = 0.0
|
x = 0.0
|
||||||
while True:
|
while True:
|
||||||
requests.get(f"http://localhost:8080/add?chart=0&series=R1&x={x}&y={sin(x)}")
|
requests.get(f"http://localhost:8080/add?chart=0&series=R1&x={x}&y={sin(x)}")
|
||||||
x += 0.01
|
x += 0.01
|
||||||
sleep(0.01)
|
sleep(0.01)
|
||||||
|
|
||||||
|
def sine_array():
|
||||||
|
x = np.linspace(0, 2*np.pi, 1)
|
||||||
|
y = np.sin(x)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"chart": 0,
|
||||||
|
"series": "R1",
|
||||||
|
"points": [ {"x": i[0], "y":i[1]} for i in zip(x,y) ]
|
||||||
|
}
|
||||||
|
|
||||||
|
r = requests.post("http://0.0.0.0:8080/add", json=data, headers={"Connection":"close"})
|
||||||
|
print(r.request.body, len(r.request.body))
|
||||||
|
|
||||||
|
def star():
|
||||||
|
points = [
|
||||||
|
(2,2),
|
||||||
|
(3,0),
|
||||||
|
(0,1),
|
||||||
|
(4,1),
|
||||||
|
(1,0),
|
||||||
|
(2,2)
|
||||||
|
]
|
||||||
|
for point in points:
|
||||||
|
requests.get(f"http://localhost:8080/add?chart=0&series=R1&x={point[0]}&y={point[1]}")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
#sine()
|
||||||
|
sine_array()
|
||||||
|
#star()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user