161 lines
3.7 KiB
C++
161 lines
3.7 KiB
C++
#include <echoion/plugins/short_url/ExternalShortener.hpp>
|
|
|
|
#include <ExtensionPlugin.hpp>
|
|
#include <Void/Plugin/Plugin.hpp>
|
|
#include <Void/Service/Service.hpp>
|
|
#include <Void/ShortUrl/ShortUrl.hpp>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
|
|
namespace Echoion::Plugins::ShortUrl
|
|
{
|
|
namespace
|
|
{
|
|
constexpr std::string_view kPluginId{"url-shortener-external"};
|
|
}
|
|
|
|
class ExternalShortenerPlugin final : public Api::IExtensionPlugin,
|
|
public Void::ShortUrl::IShortener
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] std::string_view name() const noexcept override
|
|
{
|
|
return kPluginId;
|
|
}
|
|
|
|
[[nodiscard]] std::string_view provider_id() const noexcept override
|
|
{
|
|
return kPluginId;
|
|
}
|
|
|
|
void applyConfig(const char* config)
|
|
{
|
|
configPayload_ = config != nullptr ? config : "";
|
|
}
|
|
|
|
void on_load() override
|
|
{
|
|
shortener_.reset();
|
|
activationError_.clear();
|
|
dormant_ = !hasExternalConfiguration(configPayload_);
|
|
if (dormant_) {
|
|
activationError_ = "plugin is not configured";
|
|
return;
|
|
}
|
|
auto config = parseExternalConfig(configPayload_);
|
|
if (!config) {
|
|
activationError_ = std::move(config).error();
|
|
return;
|
|
}
|
|
auto shortener = ExternalShortener::create(std::move(*config));
|
|
if (!shortener) {
|
|
activationError_ = std::move(shortener).error();
|
|
return;
|
|
}
|
|
shortener_ = std::move(*shortener);
|
|
}
|
|
|
|
void on_unload() override { shortener_.reset(); }
|
|
|
|
[[nodiscard]] bool ready() const noexcept
|
|
{
|
|
return dormant_ || shortener_ != nullptr;
|
|
}
|
|
|
|
[[nodiscard]] Void::ShortUrl::Health health() const override
|
|
{
|
|
if (shortener_ != nullptr) {
|
|
return shortener_->health();
|
|
}
|
|
return {
|
|
.status = Void::ShortUrl::HealthStatus::unhealthy,
|
|
.message = activationError_.empty()
|
|
? "plugin is not configured"
|
|
: activationError_,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] Void::ShortUrl::ShortenResult
|
|
shorten(const Void::ShortUrl::ShortenRequest& request) override
|
|
{
|
|
if (shortener_ == nullptr) {
|
|
return {
|
|
.short_url = {},
|
|
.key = {},
|
|
.expires_at_unix_seconds = 0,
|
|
.error =
|
|
{
|
|
.code = Void::ShortUrl::ErrorCode::unavailable,
|
|
.message = "external shortener is unavailable",
|
|
},
|
|
};
|
|
}
|
|
return shortener_->shorten(request);
|
|
}
|
|
|
|
private:
|
|
|
|
std::string configPayload_;
|
|
std::string activationError_;
|
|
std::unique_ptr<ExternalShortener> shortener_;
|
|
bool dormant_ = false;
|
|
};
|
|
|
|
} // namespace Echoion::Plugins::ShortUrl
|
|
|
|
extern "C" {
|
|
|
|
void* void_plugin_create()
|
|
{
|
|
return std::make_unique<
|
|
Echoion::Plugins::ShortUrl::ExternalShortenerPlugin>()
|
|
.release();
|
|
}
|
|
|
|
void void_plugin_destroy(void* instance)
|
|
{
|
|
std::unique_ptr<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin>{
|
|
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
|
instance)};
|
|
}
|
|
|
|
Void::Plugin::PluginInfo void_plugin_info()
|
|
{
|
|
return {
|
|
.abi_version = Void::Plugin::ABI_VERSION,
|
|
.name = "url-shortener-external",
|
|
.plugin_version = "1.0.0",
|
|
.description = "Configurable external URL-shortening adapter",
|
|
};
|
|
}
|
|
|
|
void void_plugin_configure(void* instance, const char* config)
|
|
{
|
|
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(instance)
|
|
->applyConfig(config);
|
|
}
|
|
|
|
int void_plugin_ready(void* instance)
|
|
{
|
|
return static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
|
instance)
|
|
->ready()
|
|
? 0
|
|
: 1;
|
|
}
|
|
|
|
Void::Service::IService* void_plugin_service(void* instance)
|
|
{
|
|
auto* plugin =
|
|
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
|
instance);
|
|
return static_cast<Void::Service::IService*>(
|
|
static_cast<Void::ShortUrl::IShortener*>(plugin));
|
|
}
|
|
|
|
} // extern "C"
|