Browse Source

extract torrent to own file

master
Jørgen Lien Sellæg 9 years ago
parent
commit
adcad4d282
  1. 40
      toREST/include/torrents.hpp
  2. 39
      toREST/src/main.cxx
  3. 4
      toREST/tests/torrents_test.hpp

40
toREST/include/torrents.hpp

@ -0,0 +1,40 @@
#ifndef _TR_TORRENTS_HPP_
#define _TR_TORRENTS_HPP_
#include <http.hpp>
namespace tr {
namespace session {
namespace torrents {
template <class torrent_session, class request, class response>
void get(torrent_session &session, response resp, request req) {
auto http_response = http::response();
if (session.is_valid()) {
auto torrents = session.get_torrents();
auto response_object = nlohmann::json::object();
auto torrents_json = nlohmann::json::array();
for (auto &torrent : torrents) {
if (torrent.is_valid()) {
const auto hash = torrent.info_hash().to_string();
const auto status = torrent.status(
torrent.query_name |
torrent.query_save_path);
torrents_json.push_back({{"hash", hash},
{"paused", status.paused},
{"seeding", status.is_seeding},
{"state", status.state},
{"priority", status.priority},
{"name", status.name}});
}
}
response_object["torrents"] = torrents_json;
http_response.set_status(http::ok);
http_response.set_body(response_object);
} else {
auto response_code = http::code(http::service_unavailable);
http_response.set_body({{response_code.first, response_code.second}});
http_response.set_status(response_code.first);
}
*resp << http_response;
}
}}}
#endif

39
toREST/src/main.cxx

@ -1,10 +1,11 @@
#include <server_http.hpp>
#include <boost/filesystem.hpp>
#include <http.hpp>
#include <session.hpp>
#include <libtorrent/session.hpp>
#include <libtorrent/session_stats.hpp>
#include <libtorrent/session_status.hpp>
#include <boost/filesystem.hpp>
#include <server_http.hpp>
#include <session.hpp>
#include <torrents.hpp>
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
@ -40,37 +41,7 @@ int main(int argc, char *argv[]) {
};
http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["GET"] = [&session](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) {
auto response = http::response();
if(session.is_valid()) {
auto torrents = session.get_torrents();
auto response_object = nlohmann::json::object();
auto torrents_json = nlohmann::json::array();
for(auto &torrent:torrents){
if(torrent.is_valid()){
const auto hash=torrent.info_hash().to_string();
const auto status=torrent.status(
libtorrent::torrent_handle::query_name |
libtorrent::torrent_handle::query_save_path
);
torrents_json.push_back({
{ "hash", hash },
{ "paused", status.paused },
{ "seeding", status.is_seeding },
{ "state", status.state },
{ "priority", status.priority },
{ "name",status.name }
});
}
}
response_object["torrents"] = torrents_json;
response.set_status(http::ok);
response.set_body(response_object);
} 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;
tr::session::torrents::get(session, resp, req);
};
std::thread server_thread([&http_server]() {

4
toREST/tests/torrents_test.hpp

@ -10,9 +10,9 @@ SCENARIO("We are running a GET /session resource") {
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
AND_WHEN("we recive a request") {
tr::session::get(torrent_session, response, request);
tr::session::torrents::get(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
reply_is_service_unavailable(response);
CommonResponse::service_unavailable(response);
}
}
}

Loading…
Cancel
Save