mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-15 01:01:46 +02:00
request id can be int, string or null
This commit is contained in:
parent
310aefac6e
commit
0a879301c9
4 changed files with 113 additions and 21 deletions
86
server/json/jsonRequestId.h
Normal file
86
server/json/jsonRequestId.h
Normal file
|
@ -0,0 +1,86 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2016 Johannes Pohl
|
||||
|
||||
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 3 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 <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#ifndef JSON_REQUEST_ID_H
|
||||
#define JSON_REQUEST_ID_H
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include "externals/json.hpp"
|
||||
|
||||
|
||||
using Json = nlohmann::json;
|
||||
|
||||
|
||||
struct req_id
|
||||
{
|
||||
enum class value_t : uint8_t
|
||||
{
|
||||
null = 0,
|
||||
string,
|
||||
integer
|
||||
};
|
||||
|
||||
req_id() : type(value_t::null)
|
||||
{
|
||||
}
|
||||
|
||||
req_id(Json json_id) : type(value_t::null)
|
||||
{
|
||||
if (json_id.is_null())
|
||||
{
|
||||
type = value_t::null;
|
||||
}
|
||||
else if (json_id.is_number_integer())
|
||||
{
|
||||
int_id = json_id.get<int>();
|
||||
type = value_t::integer;
|
||||
}
|
||||
else if (json_id.is_string())
|
||||
{
|
||||
string_id = json_id.get<std::string>();
|
||||
type = value_t::string;
|
||||
}
|
||||
else
|
||||
throw std::invalid_argument("id must be integer, string or null");
|
||||
}
|
||||
|
||||
Json toJson() const
|
||||
{
|
||||
if (type == value_t::string)
|
||||
return string_id;
|
||||
else if (type == value_t::integer)
|
||||
return int_id;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<< (std::ostream &out, const req_id &id)
|
||||
{
|
||||
out << id.toJson();
|
||||
return out;
|
||||
}
|
||||
|
||||
value_t type;
|
||||
std::string string_id;
|
||||
int int_id;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -51,10 +51,16 @@ void JsonRequest::parse(const std::string& json)
|
|||
}
|
||||
|
||||
if (json_.count("id") == 0)
|
||||
throw JsonInvalidRequestException("id is missing", -1);
|
||||
id = json_["id"].get<int>();
|
||||
if (id < 0)
|
||||
throw JsonInvalidRequestException("id must be a positive integer", id);
|
||||
throw JsonInvalidRequestException("id is missing");
|
||||
|
||||
try
|
||||
{
|
||||
id = req_id(json_["id"]);
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
throw JsonInvalidRequestException(e.what());
|
||||
}
|
||||
|
||||
if (json_.count("jsonrpc") == 0)
|
||||
throw JsonInvalidRequestException("jsonrpc is missing", id);
|
||||
|
@ -98,7 +104,7 @@ Json JsonRequest::getResponse(const Json& result)
|
|||
{
|
||||
Json response = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"id", id},
|
||||
{"id", id.toJson()},
|
||||
{"result", result}
|
||||
};
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <vector>
|
||||
#include "externals/json.hpp"
|
||||
#include "jsonrpcException.h"
|
||||
#include "jsonRequestId.h"
|
||||
|
||||
|
||||
using Json = nlohmann::json;
|
||||
|
@ -40,7 +41,7 @@ public:
|
|||
JsonRequest();
|
||||
|
||||
void parse(const std::string& json);
|
||||
int id;
|
||||
req_id id;
|
||||
std::string method;
|
||||
std::map<std::string, Json> params;
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <string>
|
||||
#include "externals/json.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "jsonRequestId.h"
|
||||
|
||||
|
||||
using Json = nlohmann::json;
|
||||
|
@ -30,13 +31,14 @@ using Json = nlohmann::json;
|
|||
|
||||
class JsonRequestException : public SnapException
|
||||
{
|
||||
int errorCode_, id_;
|
||||
int errorCode_;
|
||||
req_id id_;
|
||||
public:
|
||||
JsonRequestException(const char* text, int errorCode = 0, int requestId = -1) : SnapException(text), errorCode_(errorCode), id_(requestId)
|
||||
JsonRequestException(const char* text, int errorCode = 0, const req_id& requestId = req_id()) : SnapException(text), errorCode_(errorCode), id_(requestId)
|
||||
{
|
||||
}
|
||||
|
||||
JsonRequestException(const std::string& text, int errorCode = 0, int requestId = -1) : SnapException(text), errorCode_(errorCode), id_(requestId)
|
||||
JsonRequestException(const std::string& text, int errorCode = 0, const req_id& requestId = req_id()) : SnapException(text), errorCode_(errorCode), id_(requestId)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -65,11 +67,8 @@ public:
|
|||
{"code", errorCode},
|
||||
{"message", what()}
|
||||
}},
|
||||
{"id", id_.toJson()}
|
||||
};
|
||||
if (id_ == -1)
|
||||
response["id"] = nullptr;
|
||||
else
|
||||
response["id"] = id_;
|
||||
|
||||
return response;
|
||||
}
|
||||
|
@ -84,11 +83,11 @@ public:
|
|||
class JsonInvalidRequestException : public JsonRequestException
|
||||
{
|
||||
public:
|
||||
JsonInvalidRequestException(int requestId = -1) : JsonRequestException("invalid request", -32600, requestId)
|
||||
JsonInvalidRequestException(const req_id& requestId = req_id()) : JsonRequestException("invalid request", -32600, requestId)
|
||||
{
|
||||
}
|
||||
|
||||
JsonInvalidRequestException(const std::string& message, int requestId = -1) : JsonRequestException(message, -32600, requestId)
|
||||
JsonInvalidRequestException(const std::string& message, const req_id& requestId = req_id()) : JsonRequestException(message, -32600, requestId)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
@ -98,11 +97,11 @@ public:
|
|||
class JsonMethodNotFoundException : public JsonRequestException
|
||||
{
|
||||
public:
|
||||
JsonMethodNotFoundException(int requestId = -1) : JsonRequestException("method not found", -32601, requestId)
|
||||
JsonMethodNotFoundException(const req_id& requestId = req_id()) : JsonRequestException("method not found", -32601, requestId)
|
||||
{
|
||||
}
|
||||
|
||||
JsonMethodNotFoundException(const std::string& message, int requestId = -1) : JsonRequestException(message, -32601, requestId)
|
||||
JsonMethodNotFoundException(const std::string& message, const req_id& requestId = req_id()) : JsonRequestException(message, -32601, requestId)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
@ -112,11 +111,11 @@ public:
|
|||
class JsonInvalidParamsException : public JsonRequestException
|
||||
{
|
||||
public:
|
||||
JsonInvalidParamsException(int requestId = -1) : JsonRequestException("invalid params", -32602, requestId)
|
||||
JsonInvalidParamsException(const req_id& requestId = req_id()) : JsonRequestException("invalid params", -32602, requestId)
|
||||
{
|
||||
}
|
||||
|
||||
JsonInvalidParamsException(const std::string& message, int requestId = -1) : JsonRequestException(message, -32602, requestId)
|
||||
JsonInvalidParamsException(const std::string& message, const req_id& requestId = req_id()) : JsonRequestException(message, -32602, requestId)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
@ -125,11 +124,11 @@ public:
|
|||
class JsonInternalErrorException : public JsonRequestException
|
||||
{
|
||||
public:
|
||||
JsonInternalErrorException(int requestId = -1) : JsonRequestException("internal error", -32603, requestId)
|
||||
JsonInternalErrorException(const req_id& requestId = req_id()) : JsonRequestException("internal error", -32603, requestId)
|
||||
{
|
||||
}
|
||||
|
||||
JsonInternalErrorException(const std::string& message, int requestId = -1) : JsonRequestException(message, -32603, requestId)
|
||||
JsonInternalErrorException(const std::string& message, const req_id& requestId = req_id()) : JsonRequestException(message, -32603, requestId)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue