Files
EchoionPluginUrlShortenerEx…/src/Plugin.cpp
T

199 lines
4.8 KiB
C++

#include <echoion/api/module/Module.hpp>
#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;
};
std::unique_ptr<Void::Plugin::IModule> createModule()
{
return std::make_unique<ExternalShortenerPlugin>();
}
const Void::Plugin::ModuleDescriptor& moduleDescriptor()
{
static const Void::Plugin::ModuleDescriptor descriptor{
.module_id = "url-shortener-external",
.component_version = {.major = 2, .minor = 0, .patch = 0},
.abi_hash =
"sha256:251e3fa0ee8228cb2eb79b399deee58524b64fbb4764c8a2644357b3502354fe",
.abi_contract = Void::Json::object({
{"schemaVersion", 1},
{"contractId", "echoion.plugin.url-shortener-external"},
{"descriptor", "Void::Plugin::ModuleDescriptor"},
}),
.dependencies =
{
{
.module_id = "EchoionApi",
.version_range = "^2.0.0",
.abi_hash =
"sha256:336492a52097dd12f222bb6d9acab19e8eb6a502eab45bb78127bce6d733fbd3",
},
},
.activation_policy = Void::Plugin::ActivationPolicy::Reloadable,
.create = createModule,
};
return descriptor;
}
} // namespace Echoion::Plugins::ShortUrl
extern "C" {
const Void::Plugin::ModuleDescriptor* echoion_module_query()
{
return &Echoion::Plugins::ShortUrl::moduleDescriptor();
}
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 = ECHOION_COMPONENT_VERSION,
.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"