From 83e55a54613428ed335ef524ef6a7cb10af5eed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Thu, 27 Apr 2017 08:41:44 +0200 Subject: [PATCH] Add torrents path, now working though --- toREST/src/main.cxx | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/toREST/src/main.cxx b/toREST/src/main.cxx index 200ed43..e2951e7 100644 --- a/toREST/src/main.cxx +++ b/toREST/src/main.cxx @@ -51,11 +51,24 @@ int main(int argc, char *argv[]) { *resp << response; }; - http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["GET"]=[&session](std::shared_ptr resp, std::shared_ptr req) { + http_server.resource["^/session(\\?.*)?\\/?$"]["PATCH"]=[&](std::shared_ptr resp, std::shared_ptr req) { auto response = http::response(); + + auto content_type=req->header.find("Content-Type"); + + auto json=util::json::parse(req->content); + if (session.is_valid()) { + const auto settings = session.get_settings(); response.set_body({ - { { "torrents", nlohmann::json::array() } }, + { "paused", session.is_paused() }, + { "port", session.listen_port() }, + { "dht_enabled", session.is_dht_running() }, + { "down_limit", settings.get_int(settings.download_rate_limit) }, + { "up_limit", settings.get_int(settings.upload_rate_limit) }, + { "torrents", session.get_torrents().size() }, //TODO Optimize + { "default_download_dir", default_download_dir.filename().string() }, + { "root_dir", root_dir.string() }, }); } else { auto response_code = http::code(http::service_unavailable); @@ -65,6 +78,18 @@ int main(int argc, char *argv[]) { *resp << response; }; + http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["GET"]=[&session](std::shared_ptr resp, std::shared_ptr req) { + auto response = http::response(); + if (session.is_valid()) { + response.set_body(nlohmann::json::object({{"torrents",nlohmann::json::array()}})); + } else { + auto response_code = http::code(http::service_unavailable); + response.set_body({{response_code.first, response_code.second}}); + response.set_status(response_code.first); + } + *resp << response; + }; + std::thread server_thread([&http_server](){ http_server.start(); });