1、接口实现
This commit is contained in:
1 parent
d306f261bd
commit
cc2fdef069
14 files changed
+757
-34
No files matched your search
+185
-22
@@ -13,61 +13,84 @@
|
||||
#include <ada.h>
|
||||
#include <cpr/cpr.h>
|
||||
|
||||
#include "account_info.hpp"
|
||||
#include "auth_token.hpp"
|
||||
#include "default_repo.hpp"
|
||||
#include "file_info.hpp"
|
||||
#include "repo_entry.hpp"
|
||||
#include "repo_info.hpp"
|
||||
#include "server_info.hpp"
|
||||
#include "token_type.hpp"
|
||||
|
||||
namespace seafile {
|
||||
class SeaFileApi final {
|
||||
namespace seafile
|
||||
{
|
||||
class SeaFileApi final
|
||||
{
|
||||
//base url of the seafile api server
|
||||
std::string base_url;
|
||||
|
||||
/**
|
||||
* make a url string from given path
|
||||
* @brief make a url string from given path
|
||||
* @param path url path
|
||||
* @return the whole url string
|
||||
*/
|
||||
std::string makeUrl(const std::string &path) {
|
||||
[[nodiscard]] std::string makeUrl(const std::string& path) const
|
||||
{
|
||||
auto url = ada::parse(this->base_url);
|
||||
url->set_pathname(path);
|
||||
return std::string(url->get_href());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
tl::expected<T, SeafileException>
|
||||
parseResponse(const cpr::Response &response) {
|
||||
if (response.error) {
|
||||
template <typename T>
|
||||
static tl::expected<T, SeafileException>
|
||||
parseResponse(const cpr::Response& response)
|
||||
{
|
||||
if (response.error)
|
||||
{
|
||||
return tl::make_unexpected(
|
||||
SeafileException(
|
||||
response.error.message));
|
||||
}
|
||||
|
||||
if (response.status_code != 200) {
|
||||
if (response.status_code != 200)
|
||||
{
|
||||
return tl::make_unexpected(
|
||||
SeafileException(
|
||||
std::format("Http Error: {}",
|
||||
response.status_code)));
|
||||
}
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
return nlohmann::json::parse(response.text)
|
||||
.get<T>();
|
||||
} catch (const std::exception &e) {
|
||||
.get<T>();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
return tl::make_unexpected(
|
||||
SeafileException(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
static std::string makeAuthToken(const std::string& token, const TokenType& token_type)
|
||||
{
|
||||
return token_type == TokenType::BEARER
|
||||
? std::format("Bearer {}", token)
|
||||
: std::format("Token {}", token);
|
||||
}
|
||||
|
||||
/**
|
||||
* post body and fetch result
|
||||
* @brief post body and fetch result
|
||||
* @tparam T return type
|
||||
* @param path target url path
|
||||
* @param body post body data
|
||||
* @return T if success else SeafileException
|
||||
*/
|
||||
template<typename T>
|
||||
tl::expected<T, SeafileException> post(const std::string &path, const nlohmann::json &body) {
|
||||
template <typename T>
|
||||
tl::expected<T, SeafileException> post(const std::string& path, const nlohmann::json& body)
|
||||
{
|
||||
const auto url = makeUrl(path);
|
||||
auto response = cpr::Post(
|
||||
const auto response = cpr::Post(
|
||||
cpr::Url{url},
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
@@ -78,20 +101,22 @@ namespace seafile {
|
||||
|
||||
public:
|
||||
/**
|
||||
* primary constructor
|
||||
* @brief primary constructor
|
||||
* @param base_url base url of seafile server
|
||||
*/
|
||||
explicit SeaFileApi(std::string base_url) : base_url(std::move(base_url)) {
|
||||
explicit SeaFileApi(std::string base_url) : base_url(std::move(base_url))
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Obtain account token
|
||||
* @param username username
|
||||
* @param password password
|
||||
* @return token when success else SeafileException
|
||||
*/
|
||||
tl::expected<AuthToken, SeafileException>
|
||||
authToken(const std::string &username, const std::string &password) {
|
||||
authToken(const std::string& username, const std::string& password)
|
||||
{
|
||||
const nlohmann::json body = {
|
||||
{"username", username},
|
||||
{"password", password}
|
||||
@@ -100,8 +125,9 @@ namespace seafile {
|
||||
}
|
||||
|
||||
tl::expected<ServerInfo, SeafileException>
|
||||
serverInfo() {
|
||||
auto response = cpr::Get(
|
||||
serverInfo() const
|
||||
{
|
||||
const auto response = cpr::Get(
|
||||
cpr::Url{makeUrl("/api2/server-info/")},
|
||||
cpr::Header{
|
||||
{"accept", "application/json"}
|
||||
@@ -109,6 +135,143 @@ namespace seafile {
|
||||
|
||||
return parseResponse<ServerInfo>(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get the default repo of user
|
||||
* @param token token
|
||||
* @param token_type token type
|
||||
* @return default repo info
|
||||
*/
|
||||
tl::expected<DefaultRepo, SeafileException>
|
||||
defaultRepo(const std::string& token, const TokenType token_type) const
|
||||
{
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = makeUrl("/api2/default-repo/");
|
||||
const auto response = cpr::Get(cpr::Url{url},
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
});
|
||||
return parseResponse<DefaultRepo>(response);
|
||||
}
|
||||
|
||||
tl::expected<RepoInfo, SeafileException>
|
||||
repoInfo(const std::string& repo_id, const std::string& token, const TokenType token_type) const
|
||||
{
|
||||
const auto url = makeUrl(std::format("/api2/repos/{}/", repo_id));
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
const auto response = cpr::Get(cpr::Url{url},
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
});
|
||||
return parseResponse<RepoInfo>(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Lists all the items information in the directory
|
||||
* @param repo_id repo id
|
||||
* @param path path to query
|
||||
* @param token token
|
||||
* @param token_type token type
|
||||
* @return repo entries
|
||||
*/
|
||||
tl::expected<std::vector<RepoEntry>, SeafileException>
|
||||
repoDir(const std::string& repo_id, const std::string& path, const std::string& token,
|
||||
const TokenType token_type) const
|
||||
{
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/dir/", repo_id))};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
const auto response = cpr::Get(url,
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
},
|
||||
cpr::Parameters{
|
||||
{"p", path}
|
||||
});
|
||||
return parseResponse<std::vector<RepoEntry>>(response);
|
||||
}
|
||||
|
||||
tl::expected<AccountInfo, SeafileException>
|
||||
accountInfo(const std::string& token,
|
||||
const TokenType token_type) const
|
||||
{
|
||||
const auto url = cpr::Url{makeUrl("/api2/account/info/")};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
const auto response = cpr::Get(url,
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
});
|
||||
return parseResponse<AccountInfo>(response);
|
||||
}
|
||||
|
||||
tl::expected<FileInfo, SeafileException>
|
||||
fileDetail(const std::string& repo_id, const std::string& path, const std::string& token,
|
||||
const TokenType token_type) const
|
||||
{
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/detail/", repo_id))};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
const auto response = cpr::Get(url,
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
}, cpr::Parameters{
|
||||
{"p", path}
|
||||
});
|
||||
return parseResponse<FileInfo>(response);
|
||||
}
|
||||
|
||||
tl::expected<std::string, SeafileException>
|
||||
downloadFileUrl(const std::string& repo_id, const std::string& path, const std::string& token,
|
||||
const TokenType token_type) const
|
||||
{
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/", repo_id))};
|
||||
const auto response = cpr::Get(url,
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
}, cpr::Parameters{
|
||||
{"p", path}
|
||||
});
|
||||
return parseResponse<std::string>(response);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<std::string, SeafileException>
|
||||
downloadFile(const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::optional<size_t> start,
|
||||
const std::optional<size_t> end,
|
||||
const std::string& token,
|
||||
const TokenType token_type) const
|
||||
{
|
||||
if (const auto download_url = downloadFileUrl(repo_id, path, token, token_type); download_url.has_value())
|
||||
{
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = cpr::Url{download_url.value()};
|
||||
auto header = cpr::Header{
|
||||
{"authorization", auth}
|
||||
};
|
||||
const auto range = std::format(
|
||||
"bytes={}-{}",
|
||||
start ? std::to_string(*start) : "",
|
||||
end ? std::to_string(*end) : "");
|
||||
header.emplace("range", std::move(range));
|
||||
const auto response = cpr::Get(url, header);
|
||||
return parseResponse<std::string>(response);
|
||||
}
|
||||
else
|
||||
{
|
||||
return download_url;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user