mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-04-19 00:27:23 -05:00
Add Http action
This commit is contained in:
parent
9704d5f1f4
commit
c9177c4429
|
|
@ -100,6 +100,8 @@ target_sources(
|
|||
src/macro-core/macro-action-filter.hpp
|
||||
src/macro-core/macro-action-hotkey.cpp
|
||||
src/macro-core/macro-action-hotkey.hpp
|
||||
src/macro-core/macro-action-http.cpp
|
||||
src/macro-core/macro-action-http.hpp
|
||||
src/macro-core/macro-action-macro.cpp
|
||||
src/macro-core/macro-action-macro.hpp
|
||||
src/macro-core/macro-action-media.cpp
|
||||
|
|
|
|||
|
|
@ -494,6 +494,12 @@ AdvSceneSwitcher.action.sequence.restart="Restart from beginning once end of lis
|
|||
AdvSceneSwitcher.action.sequence.continueFrom="Continue with selected item"
|
||||
AdvSceneSwitcher.action.websocket="Websocket"
|
||||
AdvSceneSwitcher.action.websocket.entry="Send scene switcher message via {{connection}}"
|
||||
AdvSceneSwitcher.action.http="Http"
|
||||
AdvSceneSwitcher.action.http.type.get="GET"
|
||||
AdvSceneSwitcher.action.http.type.post="POST"
|
||||
AdvSceneSwitcher.action.http.entry.line1="Send {{method}} to {{url}}"
|
||||
AdvSceneSwitcher.action.http.entry.line2="Timeout: {{timeout}} seconds"
|
||||
|
||||
|
||||
; Transition Tab
|
||||
AdvSceneSwitcher.transitionTab.title="Transition"
|
||||
|
|
@ -792,6 +798,7 @@ AdvSceneSwitcher.selectProfile="--select profile--"
|
|||
AdvSceneSwitcher.selectSceneCollection="--select scene collection--"
|
||||
AdvSceneSwitcher.enterPath="--enter path--"
|
||||
AdvSceneSwitcher.enterText="--enter text--"
|
||||
AdvSceneSwitcher.enterURL="--enter URL--"
|
||||
AdvSceneSwitcher.invaildEntriesWillNotBeSaved="invalid entries will not be saved"
|
||||
AdvSceneSwitcher.selectWindowTip="Use \"OBS\" to specify OBS window\nUse \"Task Switching\"to specify ALT + TAB"
|
||||
AdvSceneSwitcher.sceneItemSelection.all="All"
|
||||
|
|
|
|||
233
src/macro-core/macro-action-http.cpp
Normal file
233
src/macro-core/macro-action-http.cpp
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
#include "macro-action-http.hpp"
|
||||
#include "advanced-scene-switcher.hpp"
|
||||
#include "utility.hpp"
|
||||
#include "curl-helper.hpp"
|
||||
|
||||
const std::string MacroActionHttp::id = "http";
|
||||
|
||||
bool MacroActionHttp::_registered = MacroActionFactory::Register(
|
||||
MacroActionHttp::id,
|
||||
{MacroActionHttp::Create, MacroActionHttpEdit::Create,
|
||||
"AdvSceneSwitcher.action.http"});
|
||||
|
||||
const static std::map<MacroActionHttp::Method, std::string> methods = {
|
||||
{MacroActionHttp::Method::GET, "AdvSceneSwitcher.action.http.type.get"},
|
||||
{MacroActionHttp::Method::POST,
|
||||
"AdvSceneSwitcher.action.http.type.post"},
|
||||
};
|
||||
|
||||
size_t WriteCB(void *, size_t size, size_t nmemb, std::string *)
|
||||
{
|
||||
// Just drop the data
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
void MacroActionHttp::Get()
|
||||
{
|
||||
|
||||
f_curl_setopt(switcher->curl, CURLOPT_URL, _url.c_str());
|
||||
f_curl_setopt(switcher->curl, CURLOPT_HTTPGET, 1L);
|
||||
f_curl_setopt(switcher->curl, CURLOPT_TIMEOUT_MS,
|
||||
_timeout.seconds * 1000);
|
||||
|
||||
std::string response;
|
||||
f_curl_setopt(switcher->curl, CURLOPT_WRITEFUNCTION, WriteCB);
|
||||
f_curl_setopt(switcher->curl, CURLOPT_WRITEDATA, &response);
|
||||
|
||||
f_curl_perform(switcher->curl);
|
||||
}
|
||||
|
||||
void MacroActionHttp::Post()
|
||||
{
|
||||
f_curl_setopt(switcher->curl, CURLOPT_URL, _url.c_str());
|
||||
f_curl_setopt(switcher->curl, CURLOPT_POSTFIELDS, _data.c_str());
|
||||
f_curl_setopt(switcher->curl, CURLOPT_TIMEOUT_MS,
|
||||
_timeout.seconds * 1000);
|
||||
f_curl_perform(switcher->curl);
|
||||
}
|
||||
|
||||
bool MacroActionHttp::PerformAction()
|
||||
{
|
||||
if (!switcher->curl) {
|
||||
blog(LOG_WARNING,
|
||||
"cannot perform http action (curl not found)");
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (_method) {
|
||||
case MacroActionHttp::Method::GET:
|
||||
Get();
|
||||
break;
|
||||
case MacroActionHttp::Method::POST:
|
||||
Post();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MacroActionHttp::LogAction()
|
||||
{
|
||||
auto it = methods.find(_method);
|
||||
if (it != methods.end()) {
|
||||
vblog(LOG_INFO,
|
||||
"sent http request \"%s\" to \"%s\" with data \"%s\"",
|
||||
it->second.c_str(), _url.c_str(), _data.c_str());
|
||||
} else {
|
||||
blog(LOG_WARNING, "ignored unknown http action %d",
|
||||
static_cast<int>(_method));
|
||||
}
|
||||
}
|
||||
|
||||
bool MacroActionHttp::Save(obs_data_t *obj)
|
||||
{
|
||||
MacroAction::Save(obj);
|
||||
obs_data_set_string(obj, "url", _url.c_str());
|
||||
obs_data_set_string(obj, "data", _data.c_str());
|
||||
obs_data_set_int(obj, "method", static_cast<int>(_method));
|
||||
_timeout.Save(obj);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MacroActionHttp::Load(obs_data_t *obj)
|
||||
{
|
||||
MacroAction::Load(obj);
|
||||
_url = obs_data_get_string(obj, "url");
|
||||
_data = obs_data_get_string(obj, "data");
|
||||
_method = static_cast<Method>(obs_data_get_int(obj, "method"));
|
||||
_timeout.Load(obj);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string MacroActionHttp::GetShortDesc()
|
||||
{
|
||||
return _url;
|
||||
}
|
||||
|
||||
static inline void populateMethodSelection(QComboBox *list)
|
||||
{
|
||||
for (auto entry : methods) {
|
||||
list->addItem(obs_module_text(entry.second.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
MacroActionHttpEdit::MacroActionHttpEdit(
|
||||
QWidget *parent, std::shared_ptr<MacroActionHttp> entryData)
|
||||
: QWidget(parent),
|
||||
_url(new QLineEdit()),
|
||||
_methods(new QComboBox()),
|
||||
_data(new ResizingPlainTextEdit(this)),
|
||||
_timeout(new DurationSelection(this, false))
|
||||
{
|
||||
populateMethodSelection(_methods);
|
||||
|
||||
QWidget::connect(_url, SIGNAL(editingFinished()), this,
|
||||
SLOT(URLChanged()));
|
||||
QWidget::connect(_data, SIGNAL(textChanged()), this,
|
||||
SLOT(DataChanged()));
|
||||
QWidget::connect(_methods, SIGNAL(currentIndexChanged(int)), this,
|
||||
SLOT(MethodChanged(int)));
|
||||
QWidget::connect(_timeout, SIGNAL(DurationChanged(double)), this,
|
||||
SLOT(TimeoutChanged(double)));
|
||||
|
||||
std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
|
||||
{"{{url}}", _url},
|
||||
{"{{method}}", _methods},
|
||||
{"{{data}}", _data},
|
||||
{"{{timeout}}", _timeout},
|
||||
};
|
||||
auto *actionLayout = new QHBoxLayout;
|
||||
placeWidgets(
|
||||
obs_module_text("AdvSceneSwitcher.action.http.entry.line1"),
|
||||
actionLayout, widgetPlaceholders);
|
||||
auto *timeoutLayout = new QHBoxLayout;
|
||||
placeWidgets(
|
||||
obs_module_text("AdvSceneSwitcher.action.http.entry.line2"),
|
||||
timeoutLayout, widgetPlaceholders);
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||
mainLayout->addLayout(actionLayout);
|
||||
mainLayout->addWidget(_data);
|
||||
mainLayout->addLayout(timeoutLayout);
|
||||
setLayout(mainLayout);
|
||||
|
||||
_entryData = entryData;
|
||||
UpdateEntryData();
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
void MacroActionHttpEdit::UpdateEntryData()
|
||||
{
|
||||
if (!_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
_url->setText(QString::fromStdString(_entryData->_url));
|
||||
_data->setPlainText(QString::fromStdString(_entryData->_data));
|
||||
_methods->setCurrentIndex(static_cast<int>(_entryData->_method));
|
||||
_timeout->SetDuration(_entryData->_timeout);
|
||||
SetWidgetVisibility();
|
||||
}
|
||||
|
||||
void MacroActionHttpEdit::URLChanged()
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_url = _url->text().toStdString();
|
||||
emit(HeaderInfoChanged(_url->text()));
|
||||
}
|
||||
|
||||
void MacroActionHttpEdit::MethodChanged(int value)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_method = static_cast<MacroActionHttp::Method>(value);
|
||||
SetWidgetVisibility();
|
||||
}
|
||||
|
||||
void MacroActionHttpEdit::TimeoutChanged(double seconds)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_timeout.seconds = seconds;
|
||||
}
|
||||
|
||||
void MacroActionHttpEdit::SetWidgetVisibility()
|
||||
{
|
||||
switch (_entryData->_method) {
|
||||
case MacroActionHttp::Method::GET:
|
||||
_data->hide();
|
||||
break;
|
||||
case MacroActionHttp::Method::POST:
|
||||
_data->show();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
void MacroActionHttpEdit::DataChanged()
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_data = _data->toPlainText().toUtf8().constData();
|
||||
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
}
|
||||
76
src/macro-core/macro-action-http.hpp
Normal file
76
src/macro-core/macro-action-http.hpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#pragma once
|
||||
#include "macro-action-edit.hpp"
|
||||
#include "resizing-text-edit.hpp"
|
||||
#include "duration-control.hpp"
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QComboBox>
|
||||
|
||||
class MacroActionHttp : public MacroAction {
|
||||
public:
|
||||
MacroActionHttp(Macro *m) : MacroAction(m) {}
|
||||
bool PerformAction();
|
||||
void LogAction();
|
||||
bool Save(obs_data_t *obj);
|
||||
bool Load(obs_data_t *obj);
|
||||
std::string GetShortDesc();
|
||||
std::string GetId() { return id; };
|
||||
static std::shared_ptr<MacroAction> Create(Macro *m)
|
||||
{
|
||||
return std::make_shared<MacroActionHttp>(m);
|
||||
}
|
||||
|
||||
enum class Method {
|
||||
GET = 0,
|
||||
POST,
|
||||
};
|
||||
|
||||
std::string _url = obs_module_text("AdvSceneSwitcher.enterURL");
|
||||
std::string _data = obs_module_text("AdvSceneSwitcher.enterText");
|
||||
Method _method = Method::GET;
|
||||
Duration _timeout;
|
||||
|
||||
private:
|
||||
void Get();
|
||||
void Post();
|
||||
|
||||
static bool _registered;
|
||||
static const std::string id;
|
||||
};
|
||||
|
||||
class MacroActionHttpEdit : public QWidget {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MacroActionHttpEdit(
|
||||
QWidget *parent,
|
||||
std::shared_ptr<MacroActionHttp> entryData = nullptr);
|
||||
void UpdateEntryData();
|
||||
static QWidget *Create(QWidget *parent,
|
||||
std::shared_ptr<MacroAction> action)
|
||||
{
|
||||
return new MacroActionHttpEdit(
|
||||
parent,
|
||||
std::dynamic_pointer_cast<MacroActionHttp>(action));
|
||||
}
|
||||
|
||||
private slots:
|
||||
void DataChanged();
|
||||
void URLChanged();
|
||||
void MethodChanged(int);
|
||||
void TimeoutChanged(double seconds);
|
||||
signals:
|
||||
void HeaderInfoChanged(const QString &);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<MacroActionHttp> _entryData;
|
||||
|
||||
private:
|
||||
void SetWidgetVisibility();
|
||||
|
||||
QLineEdit *_url;
|
||||
QComboBox *_methods;
|
||||
ResizingPlainTextEdit *_data;
|
||||
DurationSelection *_timeout;
|
||||
bool _loading = true;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user