From db8876277cdf2158a82170b7bc80df0576c1fccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Fri, 17 May 2024 01:43:31 +0200 Subject: [PATCH] http json api --- src/graph/plugins/http/CMakeLists.txt | 4 ++- src/graph/plugins/http/http.cpp | 30 +++++++++++++++++++++ test/test.py | 38 ++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/graph/plugins/http/CMakeLists.txt b/src/graph/plugins/http/CMakeLists.txt index 82cf00f..ab4ce36 100644 --- a/src/graph/plugins/http/CMakeLists.txt +++ b/src/graph/plugins/http/CMakeLists.txt @@ -1,5 +1,7 @@ +find_package(nlohmann_json 3 REQUIRED) + file(GLOB_RECURSE HTTP_SOURCES "./*.cpp") 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) set_target_properties(http PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins") \ No newline at end of file diff --git a/src/graph/plugins/http/http.cpp b/src/graph/plugins/http/http.cpp index 50eb394..99c80fd 100644 --- a/src/graph/plugins/http/http.cpp +++ b/src/graph/plugins/http/http.cpp @@ -6,9 +6,13 @@ #include // #include #include +#include +#include #include +#include using namespace Rum::HTTP; +using json = nlohmann::json; // using namespace Flags; Server* server; @@ -52,6 +56,32 @@ int run() { } }); + server->add_path("/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 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(), .y = point["y"].template get()}); + } + + plugin.update_callback(data["chart"].template get(), data["series"].template get().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; server->listen(); diff --git a/test/test.py b/test/test.py index dbfcd1e..59cfdee 100755 --- a/test/test.py +++ b/test/test.py @@ -3,14 +3,46 @@ import requests from math import sin from time import sleep +import numpy as np +import json -def main(): +def sine(): x = 0.0 while True: requests.get(f"http://localhost:8080/add?chart=0&series=R1&x={x}&y={sin(x)}") x += 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__": - main() \ No newline at end of file + main()