-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BRAYNS-634 Refactor JSON-RPC. (#1257)
- Loading branch information
1 parent
28b76e8
commit 5f271e5
Showing
38 changed files
with
735 additions
and
45 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project | ||
* All rights reserved. Do not distribute without permission. | ||
* | ||
* Responsible Author: [email protected] | ||
* | ||
* This file is part of Brayns <https://github.com/BlueBrain/Brayns> | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License version 3.0 as published | ||
* by the Free Software Foundation. | ||
* | ||
* This library 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 Lesser General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#include "Errors.h" | ||
|
||
namespace brayns::experimental | ||
{ | ||
template<> | ||
struct JsonReflector<JsonSchemaError> | ||
{ | ||
static JsonValue serialize(const JsonSchemaError &value) | ||
{ | ||
auto object = createJsonObject(); | ||
|
||
auto path = toString(value.path); | ||
object->set("path", path); | ||
|
||
auto error = toString(value.error); | ||
object->set("error", error); | ||
|
||
return object; | ||
} | ||
}; | ||
|
||
JsonRpcException::JsonRpcException(int code, const std::string &message, const JsonValue &data): | ||
std::runtime_error(message), | ||
_code(code), | ||
_data(data) | ||
{ | ||
} | ||
|
||
int JsonRpcException::getCode() const | ||
{ | ||
return _code; | ||
} | ||
|
||
const JsonValue &JsonRpcException::getData() const | ||
{ | ||
return _data; | ||
} | ||
|
||
ParseError::ParseError(const std::string &message): | ||
JsonRpcException(-32700, message) | ||
{ | ||
} | ||
|
||
InvalidRequest::InvalidRequest(const std::string &message): | ||
JsonRpcException(-32600, message) | ||
{ | ||
} | ||
|
||
InvalidRequest::InvalidRequest(const std::string &message, const std::vector<JsonSchemaError> &errors): | ||
JsonRpcException(-32600, message, serializeToJson(errors)) | ||
{ | ||
} | ||
|
||
MethodNotFound::MethodNotFound(const std::string &method): | ||
JsonRpcException(-32601, fmt::format("Method not found: '{}'", method)) | ||
{ | ||
} | ||
|
||
InvalidParams::InvalidParams(const std::string &message): | ||
JsonRpcException(-32602, message) | ||
{ | ||
} | ||
|
||
InvalidParams::InvalidParams(const std::string &message, const std::vector<JsonSchemaError> &errors): | ||
JsonRpcException(-32602, message, serializeToJson(errors)) | ||
{ | ||
} | ||
|
||
InternalError::InternalError(const std::string &message): | ||
JsonRpcException(-32603, message) | ||
{ | ||
} | ||
} // namespace brayns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project | ||
* All rights reserved. Do not distribute without permission. | ||
* | ||
* Responsible Author: [email protected] | ||
* | ||
* This file is part of Brayns <https://github.com/BlueBrain/Brayns> | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License version 3.0 as published | ||
* by the Free Software Foundation. | ||
* | ||
* This library 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 Lesser General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <stdexcept> | ||
|
||
#include <brayns/core/jsonv2/Json.h> | ||
|
||
namespace brayns::experimental | ||
{ | ||
class JsonRpcException : public std::runtime_error | ||
{ | ||
public: | ||
explicit JsonRpcException(int code, const std::string &message, const JsonValue &data = {}); | ||
|
||
int getCode() const; | ||
const JsonValue &getData() const; | ||
|
||
private: | ||
int _code = 0; | ||
JsonValue _data; | ||
}; | ||
|
||
class ParseError : public JsonRpcException | ||
{ | ||
public: | ||
explicit ParseError(const std::string &message); | ||
}; | ||
|
||
class InvalidRequest : public JsonRpcException | ||
{ | ||
public: | ||
explicit InvalidRequest(const std::string &message); | ||
explicit InvalidRequest(const std::string &message, const std::vector<JsonSchemaError> &errors); | ||
}; | ||
|
||
class MethodNotFound : public JsonRpcException | ||
{ | ||
public: | ||
explicit MethodNotFound(const std::string &method); | ||
}; | ||
|
||
class InvalidParams : public JsonRpcException | ||
{ | ||
public: | ||
explicit InvalidParams(const std::string &message); | ||
explicit InvalidParams(const std::string &message, const std::vector<JsonSchemaError> &errors); | ||
}; | ||
|
||
class InternalError : public JsonRpcException | ||
{ | ||
public: | ||
explicit InternalError(const std::string &message); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project | ||
* All rights reserved. Do not distribute without permission. | ||
* | ||
* Responsible Author: [email protected] | ||
* | ||
* This file is part of Brayns <https://github.com/BlueBrain/Brayns> | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License version 3.0 as published | ||
* by the Free Software Foundation. | ||
* | ||
* This library 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 Lesser General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <brayns/core/jsonv2/Json.h> | ||
|
||
namespace brayns::experimental | ||
{ | ||
using JsonRpcId = std::variant<NullJson, int, std::string>; | ||
|
||
struct JsonRpcRequest | ||
{ | ||
JsonRpcId id; | ||
std::string method; | ||
JsonValue params; | ||
std::string binary = {}; | ||
}; | ||
|
||
template<> | ||
struct JsonObjectReflector<JsonRpcRequest> | ||
{ | ||
static auto reflect() | ||
{ | ||
auto builder = JsonObjectInfoBuilder<JsonRpcRequest>(); | ||
builder.constant("jsonrpc", "2.0"); | ||
builder.field("id", [](auto &object) { return &object.id; }).required(false); | ||
builder.field("method", [](auto &object) { return &object.method; }); | ||
builder.field("params", [](auto &object) { return &object.params; }).required(false); | ||
return builder.build(); | ||
} | ||
}; | ||
|
||
struct JsonRpcResponse | ||
{ | ||
JsonRpcId id; | ||
JsonValue result; | ||
std::string binary = {}; | ||
}; | ||
|
||
template<> | ||
struct JsonObjectReflector<JsonRpcResponse> | ||
{ | ||
static auto reflect() | ||
{ | ||
auto builder = JsonObjectInfoBuilder<JsonRpcResponse>(); | ||
builder.constant("jsonrpc", "2.0"); | ||
builder.field("id", [](auto &object) { return &object.id; }); | ||
builder.field("result", [](auto &object) { return &object.result; }); | ||
return builder.build(); | ||
} | ||
}; | ||
|
||
struct JsonRpcError | ||
{ | ||
int code; | ||
std::string message; | ||
JsonValue data; | ||
}; | ||
|
||
template<> | ||
struct JsonObjectReflector<JsonRpcError> | ||
{ | ||
static auto reflect() | ||
{ | ||
auto builder = JsonObjectInfoBuilder<JsonRpcError>(); | ||
builder.field("code", [](auto &object) { return &object.code; }); | ||
builder.field("message", [](auto &object) { return &object.message; }); | ||
builder.field("data", [](auto &object) { return &object.data; }).required(false); | ||
return builder.build(); | ||
} | ||
}; | ||
|
||
struct JsonRpcErrorResponse | ||
{ | ||
JsonRpcId id; | ||
JsonRpcError error; | ||
}; | ||
|
||
template<> | ||
struct JsonObjectReflector<JsonRpcErrorResponse> | ||
{ | ||
static auto reflect() | ||
{ | ||
auto builder = JsonObjectInfoBuilder<JsonRpcErrorResponse>(); | ||
builder.constant("jsonrpc", "2.0"); | ||
builder.field("id", [](auto &object) { return &object.id; }); | ||
builder.field("error", [](auto &object) { return &object.error; }); | ||
return builder.build(); | ||
} | ||
}; | ||
|
||
struct JsonRpcProgress | ||
{ | ||
JsonRpcId id; | ||
std::string operation; | ||
float amount; | ||
}; | ||
|
||
template<> | ||
struct JsonObjectReflector<JsonRpcProgress> | ||
{ | ||
static auto reflect() | ||
{ | ||
auto builder = JsonObjectInfoBuilder<JsonRpcProgress>(); | ||
builder.field("id", [](auto &object) { return &object.id; }); | ||
builder.field("operation", [](auto &object) { return &object.operation; }); | ||
builder.field("amount", [](auto &object) { return &object.amount; }); | ||
return builder.build(); | ||
} | ||
}; | ||
|
||
struct JsonRpcNotification | ||
{ | ||
JsonRpcProgress params; | ||
}; | ||
|
||
template<> | ||
struct JsonObjectReflector<JsonRpcNotification> | ||
{ | ||
static auto reflect() | ||
{ | ||
auto builder = JsonObjectInfoBuilder<JsonRpcNotification>(); | ||
builder.constant("jsonrpc", "2.0"); | ||
builder.field("params", [](auto &object) { return &object.params; }); | ||
return builder.build(); | ||
} | ||
}; | ||
} |
Oops, something went wrong.