Browse Source

get resource on torrent implemented

master
Jørgen Lien Sellæg 9 years ago
parent
commit
05f8d866ab
  1. 17
      toREST/include/torrent.hpp
  2. 9
      toREST/include/torrents.hpp
  3. 17
      toREST/include/util.hpp
  4. 6
      toREST/tests/include/TorrentContext.hpp
  5. 6
      toREST/tests/stubs/TorrentContext.cpp
  6. 2
      toREST/tests/torrent_test.cpp

17
toREST/include/torrent.hpp

@ -3,9 +3,12 @@
#include <boost/filesystem.hpp>
#include <http.hpp>
#include <util.hpp>
#include <libtorrent/sha1_hash.hpp>
#include <util.hpp>
namespace tr {
namespace torrent {
template <class torrent_t>
static nlohmann::json to_json(torrent_t torrent) {
std::stringstream ss; // TODO optimize
@ -23,8 +26,8 @@ static nlohmann::json to_json(torrent_t torrent) {
{"down_limit", torrent.download_limit()},
{"name", status.name}};
}
}
namespace tr {
namespace session {
namespace torrents {
namespace id {
@ -42,10 +45,14 @@ void get(torrent_session &session, response resp, request req) {
return respond(http::service_unavailable);
}
std::string hash = req->path.substr(18, req->path.size() - 18);
const auto hash = req->path.substr(18, req->path.size() - 18); //TODO hacky
const auto torrent = session.find_torrent(util::sha1::string(hash));
if (!torrent.is_valid()) {
return respond(http::bad_request);
}
http_response.set_body(nlohmann::json::object(
{{"info_hash", hash}}));
http_response.set_body(torrent::to_json(torrent));
http_response.set_status(http::ok);
*resp << http_response;
}

9
toREST/include/torrents.hpp

@ -26,7 +26,7 @@ void get(torrent_session &session, response resp, request req) {
auto torrents_json = nlohmann::json::array();
for (auto &torrent : torrents) {
if (torrent.is_valid()) {
torrents_json.push_back(to_json(torrent));
torrents_json.push_back(tr::torrent::to_json(torrent));
}
}
response_object["torrents"] = torrents_json;
@ -83,12 +83,7 @@ void del(torrent_session &session, response resp, request req) {
options = remove_files;
}
std::string sha1_hash_in = obj;
libtorrent::sha1_hash sha1_hash;
std::stringstream ss;
ss << sha1_hash_in;
ss >> sha1_hash;
auto handle = session.find_torrent(sha1_hash);
const auto handle = session.find_torrent(util::sha1::string(obj));
if (handle.is_valid()) {
session.remove_torrent(handle, options);

17
toREST/include/util.hpp

@ -2,6 +2,7 @@
#define _TR_UTIL_HPP_
#include <json.hpp>
#include <libtorrent/sha1_hash.hpp>
#include <unordered_map>
namespace util {
@ -39,6 +40,22 @@ public:
}
};
struct sha1 {
static auto string(const std::string &hex_str) {
libtorrent::sha1_hash sha1_hash;
std::stringstream ss;
ss << hex_str;
ss >> sha1_hash;
return sha1_hash;
};
static auto info_hash(const libtorrent::sha1_hash &info_hash) {
std::stringstream ss;
ss << info_hash;
std::string res = ss.str();
return res;
};
};
class json {
public:
/*! @brief Wrapper for nlohmann::json::parse. If the parse fails, result.is_null() will be */

6
toREST/tests/include/TorrentContext.hpp

@ -20,16 +20,16 @@ class TestTorrent {
public:
TestTorrent(const libtorrent::add_torrent_params &params);
TestTorrent() {}
bool is_valid();
bool is_valid() const;
libtorrent::sha1_hash info_hash() const;
TorrentStatus status(int type = 0);
int query_name = 1;
int query_save_path = 2;
bool valid = true;
int upload_limit_ = 0;
int upload_limit();
int upload_limit() const;
int download_limit_ = 0;
int download_limit();
int download_limit() const;
libtorrent::sha1_hash hash_;
TorrentStatus status_;
};

6
toREST/tests/stubs/TorrentContext.cpp

@ -4,15 +4,15 @@ TorrentStatus TestTorrent::status(int type) {
return status_;
}
bool TestTorrent::is_valid() {
bool TestTorrent::is_valid() const {
return valid;
}
int TestTorrent::upload_limit() {
int TestTorrent::upload_limit() const {
return upload_limit_;
}
int TestTorrent::download_limit() {
int TestTorrent::download_limit() const {
return download_limit_;
}

2
toREST/tests/torrent_test.cpp

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

Loading…
Cancel
Save