handler: request and response types

This commit is contained in:
Stéphane Lepin 2019-11-15 18:58:58 +01:00
parent 9a8f248a75
commit bdf1023b16
5 changed files with 197 additions and 0 deletions

View File

@ -46,6 +46,8 @@ set(obs-websocket_SOURCES
src/WSEvents.cpp src/WSEvents.cpp
src/Config.cpp src/Config.cpp
src/Utils.cpp src/Utils.cpp
src/rpc/RpcRequest.cpp
src/rpc/RpcResponse.h
src/forms/settings-dialog.cpp) src/forms/settings-dialog.cpp)
set(obs-websocket_HEADERS set(obs-websocket_HEADERS
@ -56,6 +58,8 @@ set(obs-websocket_HEADERS
src/WSEvents.h src/WSEvents.h
src/Config.h src/Config.h
src/Utils.h src/Utils.h
src/rpc/RpcRequest.h
src/rpc/RpcResponse.h
src/forms/settings-dialog.h) src/forms/settings-dialog.h)
# --- Platform-independent build settings --- # --- Platform-independent build settings ---

27
src/rpc/RpcRequest.cpp Normal file
View File

@ -0,0 +1,27 @@
/*
obs-websocket
Copyright (C) 2016-2019 Stéphane Lepin <stephane.lepin@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>
*/
#include "RpcRequest.h"
RpcRequest::RpcRequest(const QString& messageId, const QString& methodName, obs_data_t* params) :
_messageId(messageId),
_methodName(methodName)
{
_parameters = obs_data_create();
obs_data_apply(_parameters, params);
}

46
src/rpc/RpcRequest.h Normal file
View File

@ -0,0 +1,46 @@
/*
obs-websocket
Copyright (C) 2016-2019 Stéphane Lepin <stephane.lepin@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>
*/
#pragma once
#include <obs-data.h>
#include <QtCore/QString>
#include "../obs-websocket.h"
class RpcRequest
{
public:
explicit RpcRequest(const QString& messageId, const QString& methodName, obs_data_t* params);
const QString& messageId() const {
return _messageId;
}
const QString& methodName() const {
return _methodName;
}
const obs_data_t* parameters() const {
return _parameters;
}
private:
const QString _messageId;
const QString _methodName;
OBSDataAutoRelease _parameters;
};

48
src/rpc/RpcResponse.cpp Normal file
View File

@ -0,0 +1,48 @@
/*
obs-websocket
Copyright (C) 2016-2019 Stéphane Lepin <stephane.lepin@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>
*/
#include "RpcResponse.h"
#include "RpcRequest.h"
RpcResponse::RpcResponse(
ResponseStatus status, const QString& messageId,
const QString& methodName, obs_data_t* additionalFields
) :
_status(status),
_messageId(messageId),
_methodName(methodName),
_additionalFields(nullptr)
{
if (additionalFields) {
_additionalFields = obs_data_create();
obs_data_apply(_additionalFields, additionalFields);
}
}
const RpcResponse::RpcResponse ok(const RpcRequest& request, obs_data_t* additionalFields)
{
RpcResponse response(ResponseStatus::Ok, request.messageId(), request.methodName(), additionalFields);
return response;
}
const RpcResponse::RpcResponse fail(const RpcRequest& request, const QString& errorMessage)
{
RpcResponse response(ResponseStatus::Error, request.messageId(), request.methodName());
response._errorMessage = errorMessage;
return response;
}

72
src/rpc/RpcResponse.h Normal file
View File

@ -0,0 +1,72 @@
/*
obs-websocket
Copyright (C) 2016-2019 Stéphane Lepin <stephane.lepin@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>
*/
#pragma once
#include <obs-data.h>
#include <QtCore/QString>
#include "../obs-websocket.h"
class RpcRequest;
enum ResponseStatus
{
Unknown,
Ok,
Error
};
class RpcResponse
{
public:
static RpcResponse ofRequest(const RpcRequest& request);
static const RpcResponse ok(const RpcRequest& request, obs_data_t* additionalFields = nullptr);
static const RpcResponse fail(const RpcRequest& request, const QString& errorMessage);
ResponseStatus status() {
return _status;
}
const QString& messageId() const {
return _messageId;
}
const QString& methodName() const {
return _methodName;
}
const QString& errorMessage() const {
return _errorMessage;
}
const obs_data_t* parameters() const {
return _additionalFields;
}
private:
explicit RpcResponse(
ResponseStatus status,
const QString& messageId, const QString& methodName,
obs_data_t* additionalFields = nullptr
);
const ResponseStatus _status;
const QString _messageId;
const QString _methodName;
QString _errorMessage;
OBSDataAutoRelease _additionalFields;
};