clean seafile api

This commit is contained in:
shell committed 2026-07-21 22:00:38 +08:00
1 parent 133424f5f8
commit 3e4694ceb5
14 files changed
+476 -290

No files matched your search

+47 -25
View File
@@ -23,11 +23,14 @@
#include "seafile_api_interface.hpp"
#include "server_info.hpp"
#include "token_type.hpp"
#include "seafile/util/version.hpp"
namespace seafile::api {
class SeaFileApi final:public ISeaFileApi {
namespace seafile::api {
class SeaFileApi final : public ISeaFileApi {
//base url of the seafile api server
std::string base_url;
std::string token;
TokenType token_type = TokenType::BEARER;
/**
* @brief make a url string from given path
@@ -105,16 +108,27 @@ namespace seafile::api {
* @return token when success else SeafileException
*/
tl::expected<AuthToken, SeafileException>
authToken(const std::string &username, const std::string &password) override {
auth(const std::string &username, const std::string &password) override {
const nlohmann::json body = {
{"username", username},
{"password", password}
};
return this->post<AuthToken>("/api2/auth-token/", body);
const auto &auth_token = this->post<AuthToken>("/api2/auth-token/", body);
if (auth_token) {
this->token = auth_token->token;
if (const auto &server_info = this->serverInfo()) {
if (const auto version = util::Version(server_info->version); version.major() < 11) {
this->token_type = TokenType::TOKEN;
}
} else {
return tl::make_unexpected(server_info.error());
}
}
return auth_token;
}
tl::expected<ServerInfo, SeafileException>
serverInfo() const override{
serverInfo() const override {
const auto response = cpr::Get(
cpr::Url{makeUrl("/api2/server-info/")},
cpr::Header{
@@ -126,12 +140,10 @@ namespace seafile::api {
/**
* @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 override{
defaultRepo() const override {
const auto auth = makeAuthToken(token, token_type);
const auto url = makeUrl("/api2/default-repo/");
const auto response = cpr::Get(cpr::Url{url},
@@ -143,7 +155,7 @@ namespace seafile::api {
}
tl::expected<RepoInfo, SeafileException>
repoInfo(const std::string &repo_id, const std::string &token, const TokenType token_type) const override{
repoInfo(const std::string &repo_id) const override {
const auto url = makeUrl(std::format("/api2/repos/{}/", repo_id));
const auto auth = makeAuthToken(token, token_type);
@@ -155,17 +167,32 @@ namespace seafile::api {
return parseResponse<RepoInfo>(response);
}
tl::expected<std::vector<RepoInfo>, SeafileException>
repos(const std::string &type, const std::string &name_contains) const override {
const auto url = makeUrl("/api2/repos/");
const auto auth = makeAuthToken(token, token_type);
const auto response = cpr::Get(cpr::Url{url},
cpr::Header{
{"accept", "application/json"},
{"authorization", auth}
},
cpr::Parameters{
{"type", type},
{"nameContains", name_contains}
});
return parseResponse<std::vector<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 override{
repoDir(const std::string &repo_id, const std::string &path) const override {
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/dir/", repo_id))};
const auto auth = makeAuthToken(token, token_type);
@@ -181,8 +208,7 @@ namespace seafile::api {
}
tl::expected<AccountInfo, SeafileException>
accountInfo(const std::string &token,
const TokenType token_type) const override{
accountInfo() const override {
const auto url = cpr::Url{makeUrl("/api2/account/info/")};
const auto auth = makeAuthToken(token, token_type);
@@ -195,8 +221,7 @@ namespace seafile::api {
}
tl::expected<FileInfo, SeafileException>
fileDetail(const std::string &repo_id, const std::string &path, const std::string &token,
const TokenType token_type) const override{
fileDetail(const std::string &repo_id, const std::string &path) const override {
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/detail/", repo_id))};
const auto auth = makeAuthToken(token, token_type);
@@ -211,8 +236,7 @@ namespace seafile::api {
}
tl::expected<std::string, SeafileException>
downloadFileUrl(const std::string &repo_id, const std::string &path, const std::string &token,
const TokenType token_type) const override{
downloadFileUrl(const std::string &repo_id, const std::string &path) const override {
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,
@@ -230,10 +254,8 @@ namespace seafile::api {
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 override{
if (auto download_url = downloadFileUrl(repo_id, path, token, token_type); download_url.has_value()) {
const std::optional<size_t> end) const override {
if (auto download_url = downloadFileUrl(repo_id, path); download_url.has_value()) {
const auto auth = makeAuthToken(token, token_type);
const auto url = cpr::Url{download_url.value()};
auto header = cpr::Header{
@@ -255,8 +277,8 @@ namespace seafile::api {
}
}
tl::expected<DirInfo, SeafileException> dirDetail(const std::string &repo_id, const std::string &path,
const std::string &token, const TokenType token_type) const override{
tl::expected<DirInfo, SeafileException>
dirDetail(const std::string &repo_id, const std::string &path) const override {
const auto auth = makeAuthToken(token, token_type);
const auto url = cpr::Url{makeUrl(std::format("/api/v2.1/repos/{}/dir/detail/", repo_id))};
const auto response = cpr::Get(url, cpr::Header{