initial commit

This commit is contained in:
shell committed 2026-06-18 00:20:39 +08:00
commit d306f261bd
8 files changed
+302

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
//
// Created by Shell on 2026/6/18.
//
#ifndef SEADISK_AUTH_TOKEN_HPP
#define SEADISK_AUTH_TOKEN_HPP
namespace seafile {
struct AuthToken final {
std::string token;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(AuthToken, token);
};
}
#endif //SEADISK_AUTH_TOKEN_HPP
+18
View File
@@ -0,0 +1,18 @@
//
// Created by Shell on 2026/6/17.
//
#ifndef SEADISK_EXCEPTION_HPP
#define SEADISK_EXCEPTION_HPP
#include <stdexcept>
namespace seafile {
class SeafileException : public std::runtime_error {
public:
explicit SeafileException(const std::string &msg)
: std::runtime_error(msg) {
}
};
}
#endif //SEADISK_EXCEPTION_HPP
+115
View File
@@ -0,0 +1,115 @@
//
// Created by Shell on 2026/6/17.
//
#ifndef SEADISK_SEAFILE_API_HPP
#define SEADISK_SEAFILE_API_HPP
#include <string>
#include <utility>
#include <nlohmann/json.hpp>
#include "exception.hpp"
#include <tl/expected.hpp>
#include <ada.h>
#include <cpr/cpr.h>
#include "auth_token.hpp"
#include "server_info.hpp"
namespace seafile {
class SeaFileApi final {
//base url of the seafile api server
std::string base_url;
/**
* make a url string from given path
* @param path url path
* @return the whole url string
*/
std::string makeUrl(const std::string &path) {
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) {
return tl::make_unexpected(
SeafileException(
response.error.message));
}
if (response.status_code != 200) {
return tl::make_unexpected(
SeafileException(
std::format("Http Error: {}",
response.status_code)));
}
try {
return nlohmann::json::parse(response.text)
.get<T>();
} catch (const std::exception &e) {
return tl::make_unexpected(
SeafileException(e.what()));
}
}
/**
* 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) {
const auto url = makeUrl(path);
auto response = cpr::Post(
cpr::Url{url},
cpr::Header{
{"accept", "application/json"},
{"content-type", "application/json"}
}, cpr::Body{body.dump()});
return parseResponse<T>(response);
}
public:
/**
* primary constructor
* @param base_url base url of seafile server
*/
explicit SeaFileApi(std::string base_url) : base_url(std::move(base_url)) {
}
/**
*
* @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) {
const nlohmann::json body = {
{"username", username},
{"password", password}
};
return this->post<AuthToken>("/api2/auth-token/", body);
}
tl::expected<ServerInfo, SeafileException>
serverInfo() {
auto response = cpr::Get(
cpr::Url{makeUrl("/api2/server-info/")},
cpr::Header{
{"accept", "application/json"}
});
return parseResponse<ServerInfo>(response);
}
};
}
#endif //SEADISK_SEAFILE_API_HPP
+32
View File
@@ -0,0 +1,32 @@
//
// Created by Shell on 2026/6/17.
//
#ifndef SEADISK_SERVER_INFO_HPP
#define SEADISK_SERVER_INFO_HPP
#include <string>
#include <vector>
namespace seafile {
/**
* seafile server info
*/
struct ServerInfo final {
/**
* server version
*/
std::string version;
/**
* encrypted library version
*/
int encrypted_library_version = 0;
/**
* supported features
*/
std::vector<std::string> features;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(ServerInfo, version, encrypted_library_version, features);
};
}
#endif //SEADISK_SERVER_INFO_HPP