1 /*
2 obs-websocket
3 Copyright (C) 2016-2019 Stéphane Lepin <stephane.lepin@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <https://www.gnu.org/licenses/>
17 */
18
19 #include "RpcRequest.h"
20 #include "RpcResponse.h"
21
RpcRequest(const QString & messageId,const QString & methodName,obs_data_t * params)22 RpcRequest::RpcRequest(const QString& messageId, const QString& methodName, obs_data_t* params) :
23 _messageId(messageId),
24 _methodName(methodName),
25 _parameters(nullptr)
26 {
27 if (params) {
28 _parameters = obs_data_create();
29 obs_data_apply(_parameters, params);
30 }
31 }
32
success(obs_data_t * additionalFields) const33 const RpcResponse RpcRequest::success(obs_data_t* additionalFields) const
34 {
35 return RpcResponse::ok(*this, additionalFields);
36 }
37
failed(const QString & errorMessage,obs_data_t * additionalFields) const38 const RpcResponse RpcRequest::failed(const QString& errorMessage, obs_data_t* additionalFields) const
39 {
40 return RpcResponse::fail(*this, errorMessage, additionalFields);
41 }
42
hasField(QString name,obs_data_type expectedFieldType,obs_data_number_type expectedNumberType) const43 const bool RpcRequest::hasField(QString name, obs_data_type expectedFieldType, obs_data_number_type expectedNumberType) const
44 {
45 if (!_parameters || name.isEmpty() || name.isNull()) {
46 return false;
47 }
48
49 OBSDataItemAutoRelease dataItem = obs_data_item_byname(_parameters, name.toUtf8());
50 if (!dataItem) {
51 return false;
52 }
53
54 if (expectedFieldType != OBS_DATA_NULL) {
55 obs_data_type fieldType = obs_data_item_gettype(dataItem);
56 if (fieldType != expectedFieldType) {
57 return false;
58 }
59
60 if (fieldType == OBS_DATA_NUMBER && expectedNumberType != OBS_DATA_NUM_INVALID) {
61 obs_data_number_type numberType = obs_data_item_numtype(dataItem);
62 if (numberType != expectedNumberType) {
63 return false;
64 }
65 }
66 }
67
68 return true;
69 }
70
hasBool(QString fieldName) const71 const bool RpcRequest::hasBool(QString fieldName) const
72 {
73 return this->hasField(fieldName, OBS_DATA_BOOLEAN);
74 }
75
hasString(QString fieldName) const76 const bool RpcRequest::hasString(QString fieldName) const
77 {
78 return this->hasField(fieldName, OBS_DATA_STRING);
79 }
80
hasNumber(QString fieldName,obs_data_number_type expectedNumberType) const81 const bool RpcRequest::hasNumber(QString fieldName, obs_data_number_type expectedNumberType) const
82 {
83 return this->hasField(fieldName, OBS_DATA_NUMBER, expectedNumberType);
84 }
85
hasInteger(QString fieldName) const86 const bool RpcRequest::hasInteger(QString fieldName) const
87 {
88 return this->hasNumber(fieldName, OBS_DATA_NUM_INT);
89 }
90
hasDouble(QString fieldName) const91 const bool RpcRequest::hasDouble(QString fieldName) const
92 {
93 return this->hasNumber(fieldName, OBS_DATA_NUM_DOUBLE);
94 }
95
hasArray(QString fieldName) const96 const bool RpcRequest::hasArray(QString fieldName) const
97 {
98 return this->hasField(fieldName, OBS_DATA_ARRAY);
99 }
100
hasObject(QString fieldName) const101 const bool RpcRequest::hasObject(QString fieldName) const
102 {
103 return this->hasField(fieldName, OBS_DATA_OBJECT);
104 }
105