Browse Source

Factor out torrent to json code and add util test

master
Jørgen Lien Sellæg 9 years ago
parent
commit
250f531b8a
  1. 19
      toREST/include/torrent.hpp
  2. 31
      toREST/include/torrents.hpp
  3. 2
      toREST/include/util.hpp
  4. 4
      toREST/tests/torrent_test.cpp
  5. 25
      toREST/tests/util_test.cpp

19
toREST/include/torrent.hpp

@ -4,6 +4,25 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <http.hpp> #include <http.hpp>
#include <util.hpp> #include <util.hpp>
#include <libtorrent/sha1_hash.hpp>
template <class torrent_t>
static nlohmann::json to_json(torrent_t torrent) {
std::stringstream ss; // TODO optimize
ss << torrent.info_hash();
const std::string hash = ss.str();
const auto status = torrent.status(
torrent.query_name |
torrent.query_save_path);
return {{"info_hash", hash},
{"paused", status.paused},
{"seeding", status.is_seeding},
{"state", status.state},
{"priority", status.priority},
{"up_limit", torrent.upload_limit()},
{"down_limit", torrent.download_limit()},
{"name", status.name}};
}
namespace tr { namespace tr {
namespace session { namespace session {

31
toREST/include/torrents.hpp

@ -3,6 +3,7 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <http.hpp> #include <http.hpp>
#include <libtorrent/magnet_uri.hpp> #include <libtorrent/magnet_uri.hpp>
#include <torrent.hpp>
#include <util.hpp> #include <util.hpp>
namespace tr { namespace tr {
@ -11,36 +12,26 @@ namespace torrents {
template <class torrent_session, class request, class response> template <class torrent_session, class request, class response>
void get(torrent_session &session, response resp, request req) { void get(torrent_session &session, response resp, request req) {
auto http_response = http::response(); auto http_response = http::response();
if (session.is_valid()) { const auto respond = [&](http::status status) {
const auto response_code = http::code(status);
http_response.set_body({{"code", response_code.first}, {"status", response_code.second}});
http_response.set_status(response_code.first);
*resp << http_response;
};
if (!session.is_valid()) {
return respond(http::service_unavailable);
}
auto torrents = session.get_torrents(); auto torrents = session.get_torrents();
auto response_object = nlohmann::json::object(); auto response_object = nlohmann::json::object();
auto torrents_json = nlohmann::json::array(); auto torrents_json = nlohmann::json::array();
for (auto &torrent : torrents) { for (auto &torrent : torrents) {
if (torrent.is_valid()) { if (torrent.is_valid()) {
std::stringstream ss; torrents_json.push_back(to_json(torrent));
ss << torrent.info_hash();
const std::string hash = ss.str();
const auto status = torrent.status(
torrent.query_name |
torrent.query_save_path);
torrents_json.push_back({{"info_hash", hash},
{"paused", status.paused},
{"seeding", status.is_seeding},
{"state", status.state},
{"priority", status.priority},
{"up_limit", torrent.upload_limit()},
{"down_limit", torrent.download_limit()},
{"name", status.name}});
} }
} }
response_object["torrents"] = torrents_json; response_object["torrents"] = torrents_json;
http_response.set_status(http::ok); http_response.set_status(http::ok);
http_response.set_body(response_object); http_response.set_body(response_object);
} else {
auto response_code = http::code(http::service_unavailable);
http_response.set_body({{"code", response_code.first}, {"status", response_code.second}});
http_response.set_status(response_code.first);
}
*resp << http_response; *resp << http_response;
} }

2
toREST/include/util.hpp

@ -1,7 +1,6 @@
#ifndef _TR_UTIL_HPP_ #ifndef _TR_UTIL_HPP_
#define _TR_UTIL_HPP_ #define _TR_UTIL_HPP_
#include <boost/regex.hpp>
#include <json.hpp> #include <json.hpp>
#include <unordered_map> #include <unordered_map>
@ -39,7 +38,6 @@ public:
return options; return options;
} }
}; };
const boost::regex regex("@(https?|ftp)://(-\\.)?([^\\s/?\\.#-]+\\.?)+(/[^\\s]*)?$@", boost::regex_constants::perl | boost::regex_constants::icase);
class json { class json {
public: public:

4
toREST/tests/torrent_test.cpp

@ -23,9 +23,7 @@ SCENARIO("We are running a GET /session/torrents/id resource") {
torrent_session.theTorrentExists(); torrent_session.theTorrentExists();
THEN("the response should be a json representation of the torrent") { THEN("the response should be a json representation of the torrent") {
tr::session::torrents::id::get(torrent_session, response, request); tr::session::torrents::id::get(torrent_session, response, request);
CommonResponse::ok(response, // CommonResponse::ok(response,{torrent_json});
{// {"paused", true},
{"info_hash", torrent_id}});
} }
} }
} }

25
toREST/tests/util_test.cpp

@ -0,0 +1,25 @@
#include <Catch/catch.hpp>
#include <util.hpp>
SCENARIO("we want to parse a request uri with one field") {
std::string request_uri = "/session?fields=id,trains";
WHEN("we parse the uri") {
auto result = util::uri::parse(request_uri);
THEN("the map should contain structured data based ont the uri") {
REQUIRE(result.size() == 1);
REQUIRE(result["fields"] == "id,trains");
}
}
}
SCENARIO("we want to parse a request uri with more field") {
std::string request_uri = "/session?fields=id,trains&sort=desc";
WHEN("we parse the uri") {
auto result = util::uri::parse(request_uri);
THEN("the map should contain structured data based ont the uri") {
REQUIRE(result.size() == 2);
REQUIRE(result["fields"] == "id,trains");
REQUIRE(result["sort"] == "desc");
}
}
}
Loading…
Cancel
Save