initial commit
This commit is contained in:
commit
d306f261bd
8 files changed
+302
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user