Changed coding style
This commit is contained in:
@@ -30,220 +30,220 @@
|
||||
namespace API::Subsonic
|
||||
{
|
||||
|
||||
enum class ResponseFormat
|
||||
{
|
||||
xml,
|
||||
json,
|
||||
};
|
||||
enum class ResponseFormat
|
||||
{
|
||||
xml,
|
||||
json,
|
||||
};
|
||||
|
||||
std::string ResponseFormatToMimeType(ResponseFormat format);
|
||||
std::string ResponseFormatToMimeType(ResponseFormat format);
|
||||
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
enum class Code
|
||||
{
|
||||
Generic = 0,
|
||||
RequiredParameterMissing = 10,
|
||||
ClientMustUpgrade = 20,
|
||||
ServerMustUpgrade = 30,
|
||||
WrongUsernameOrPassword = 40,
|
||||
TokenAuthenticationNotSupportedForLDAPUsers = 41,
|
||||
UserNotAuthorized = 50,
|
||||
RequestedDataNotFound = 70,
|
||||
};
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
enum class Code
|
||||
{
|
||||
Generic = 0,
|
||||
RequiredParameterMissing = 10,
|
||||
ClientMustUpgrade = 20,
|
||||
ServerMustUpgrade = 30,
|
||||
WrongUsernameOrPassword = 40,
|
||||
TokenAuthenticationNotSupportedForLDAPUsers = 41,
|
||||
UserNotAuthorized = 50,
|
||||
RequestedDataNotFound = 70,
|
||||
};
|
||||
|
||||
Error(Code code) : _code {code} {}
|
||||
Error(Code code) : _code{ code } {}
|
||||
|
||||
virtual std::string getMessage() const = 0;
|
||||
virtual std::string getMessage() const = 0;
|
||||
|
||||
Code getCode() const { return _code; }
|
||||
Code getCode() const { return _code; }
|
||||
|
||||
private:
|
||||
const Code _code;
|
||||
};
|
||||
private:
|
||||
const Code _code;
|
||||
};
|
||||
|
||||
class GenericError : public Error
|
||||
{
|
||||
public:
|
||||
GenericError() : Error {Code::Generic} {}
|
||||
};
|
||||
class GenericError : public Error
|
||||
{
|
||||
public:
|
||||
GenericError() : Error{ Code::Generic } {}
|
||||
};
|
||||
|
||||
class RequiredParameterMissingError : public Error
|
||||
{
|
||||
public:
|
||||
RequiredParameterMissingError(std::string_view param)
|
||||
: Error {Code::RequiredParameterMissing}
|
||||
, _param {param}
|
||||
{}
|
||||
class RequiredParameterMissingError : public Error
|
||||
{
|
||||
public:
|
||||
RequiredParameterMissingError(std::string_view param)
|
||||
: Error{ Code::RequiredParameterMissing }
|
||||
, _param{ param }
|
||||
{}
|
||||
|
||||
private:
|
||||
std::string getMessage() const override { return "Required parameter '" + _param + "' is missing."; }
|
||||
std::string _param;
|
||||
};
|
||||
private:
|
||||
std::string getMessage() const override { return "Required parameter '" + _param + "' is missing."; }
|
||||
std::string _param;
|
||||
};
|
||||
|
||||
class ClientMustUpgradeError : public Error
|
||||
{
|
||||
public:
|
||||
ClientMustUpgradeError() : Error {Code::ClientMustUpgrade} {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Incompatible Subsonic REST protocol version. Client must upgrade."; }
|
||||
};
|
||||
class ClientMustUpgradeError : public Error
|
||||
{
|
||||
public:
|
||||
ClientMustUpgradeError() : Error{ Code::ClientMustUpgrade } {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Incompatible Subsonic REST protocol version. Client must upgrade."; }
|
||||
};
|
||||
|
||||
class ServerMustUpgradeError : public Error
|
||||
{
|
||||
public:
|
||||
ServerMustUpgradeError() : Error {Code::ServerMustUpgrade} {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Incompatible Subsonic REST protocol version. Server must upgrade."; }
|
||||
};
|
||||
class ServerMustUpgradeError : public Error
|
||||
{
|
||||
public:
|
||||
ServerMustUpgradeError() : Error{ Code::ServerMustUpgrade } {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Incompatible Subsonic REST protocol version. Server must upgrade."; }
|
||||
};
|
||||
|
||||
class WrongUsernameOrPasswordError : public Error
|
||||
{
|
||||
public:
|
||||
WrongUsernameOrPasswordError() : Error {Code::WrongUsernameOrPassword} {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Wrong username or password."; }
|
||||
};
|
||||
class WrongUsernameOrPasswordError : public Error
|
||||
{
|
||||
public:
|
||||
WrongUsernameOrPasswordError() : Error{ Code::WrongUsernameOrPassword } {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Wrong username or password."; }
|
||||
};
|
||||
|
||||
class TokenAuthenticationNotSupportedForLDAPUsersError : public Error
|
||||
{
|
||||
public:
|
||||
TokenAuthenticationNotSupportedForLDAPUsersError() : Error {Code::TokenAuthenticationNotSupportedForLDAPUsers} {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Token authentication not supported for LDAP users."; }
|
||||
};
|
||||
class TokenAuthenticationNotSupportedForLDAPUsersError : public Error
|
||||
{
|
||||
public:
|
||||
TokenAuthenticationNotSupportedForLDAPUsersError() : Error{ Code::TokenAuthenticationNotSupportedForLDAPUsers } {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Token authentication not supported for LDAP users."; }
|
||||
};
|
||||
|
||||
class UserNotAuthorizedError : public Error
|
||||
{
|
||||
public:
|
||||
UserNotAuthorizedError () : Error {Code::UserNotAuthorized} {}
|
||||
private:
|
||||
std::string getMessage() const override { return "User is not authorized for the given operation."; }
|
||||
};
|
||||
class UserNotAuthorizedError : public Error
|
||||
{
|
||||
public:
|
||||
UserNotAuthorizedError() : Error{ Code::UserNotAuthorized } {}
|
||||
private:
|
||||
std::string getMessage() const override { return "User is not authorized for the given operation."; }
|
||||
};
|
||||
|
||||
class RequestedDataNotFoundError : public Error
|
||||
{
|
||||
public:
|
||||
RequestedDataNotFoundError() : Error {Code::RequestedDataNotFound} {}
|
||||
private:
|
||||
std::string getMessage() const override { return "The requested data was not found."; }
|
||||
};
|
||||
class RequestedDataNotFoundError : public Error
|
||||
{
|
||||
public:
|
||||
RequestedDataNotFoundError() : Error{ Code::RequestedDataNotFound } {}
|
||||
private:
|
||||
std::string getMessage() const override { return "The requested data was not found."; }
|
||||
};
|
||||
|
||||
class InternalErrorGenericError : public GenericError
|
||||
{
|
||||
public:
|
||||
InternalErrorGenericError(const std::string& message) : _message {message} {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Internal error: " + _message; }
|
||||
const std::string _message;
|
||||
};
|
||||
class InternalErrorGenericError : public GenericError
|
||||
{
|
||||
public:
|
||||
InternalErrorGenericError(const std::string& message) : _message{ message } {}
|
||||
private:
|
||||
std::string getMessage() const override { return "Internal error: " + _message; }
|
||||
const std::string _message;
|
||||
};
|
||||
|
||||
class LoginThrottledGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Login throttled, too many attempts"; }
|
||||
};
|
||||
class LoginThrottledGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Login throttled, too many attempts"; }
|
||||
};
|
||||
|
||||
class NotImplementedGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Not implemented"; }
|
||||
};
|
||||
class NotImplementedGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Not implemented"; }
|
||||
};
|
||||
|
||||
class UnknownEntryPointGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Unknown API method"; }
|
||||
};
|
||||
class UnknownEntryPointGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Unknown API method"; }
|
||||
};
|
||||
|
||||
class PasswordTooWeakGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Password too weak"; }
|
||||
};
|
||||
class PasswordTooWeakGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Password too weak"; }
|
||||
};
|
||||
|
||||
class PasswordMustMatchLoginNameGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Password must match login name"; }
|
||||
};
|
||||
class PasswordMustMatchLoginNameGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Password must match login name"; }
|
||||
};
|
||||
|
||||
class DemoUserCannotChangePasswordGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Demo user cannot change its password"; }
|
||||
};
|
||||
class DemoUserCannotChangePasswordGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "Demo user cannot change its password"; }
|
||||
};
|
||||
|
||||
class UserAlreadyExistsGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "User already exists"; }
|
||||
};
|
||||
class UserAlreadyExistsGenericError : public GenericError
|
||||
{
|
||||
std::string getMessage() const override { return "User already exists"; }
|
||||
};
|
||||
|
||||
class BadParameterGenericError : public GenericError
|
||||
{
|
||||
public:
|
||||
BadParameterGenericError(const std::string& parameterName) : _parameterName {parameterName} {}
|
||||
class BadParameterGenericError : public GenericError
|
||||
{
|
||||
public:
|
||||
BadParameterGenericError(const std::string& parameterName) : _parameterName{ parameterName } {}
|
||||
|
||||
private:
|
||||
std::string getMessage() const override { return "Parameter '" + _parameterName + "': bad value"; }
|
||||
private:
|
||||
std::string getMessage() const override { return "Parameter '" + _parameterName + "': bad value"; }
|
||||
|
||||
const std::string _parameterName;
|
||||
};
|
||||
const std::string _parameterName;
|
||||
};
|
||||
|
||||
class Response
|
||||
{
|
||||
public:
|
||||
class Node
|
||||
{
|
||||
public:
|
||||
void setAttribute(std::string_view key, std::string_view value);
|
||||
class Response
|
||||
{
|
||||
public:
|
||||
class Node
|
||||
{
|
||||
public:
|
||||
void setAttribute(std::string_view key, std::string_view value);
|
||||
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr>
|
||||
void setAttribute(std::string_view key, T value)
|
||||
{
|
||||
if constexpr (std::is_same<bool, T>::value)
|
||||
_attributes[std::string {key}] = value;
|
||||
else
|
||||
_attributes[std::string {key}] = static_cast<long long>(value);
|
||||
}
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr>
|
||||
void setAttribute(std::string_view key, T value)
|
||||
{
|
||||
if constexpr (std::is_same<bool, T>::value)
|
||||
_attributes[std::string{ key }] = value;
|
||||
else
|
||||
_attributes[std::string{ key }] = static_cast<long long>(value);
|
||||
}
|
||||
|
||||
// A Node has either a value or some children
|
||||
void setValue(std::string_view value);
|
||||
void setValue(long long value);
|
||||
Node& createChild(const std::string& key);
|
||||
Node& createArrayChild(const std::string& key);
|
||||
// A Node has either a value or some children
|
||||
void setValue(std::string_view value);
|
||||
void setValue(long long value);
|
||||
Node& createChild(const std::string& key);
|
||||
Node& createArrayChild(const std::string& key);
|
||||
|
||||
void addChild(const std::string& key, Node node);
|
||||
void addArrayChild(const std::string& key, Node node);
|
||||
void addChild(const std::string& key, Node node);
|
||||
void addArrayChild(const std::string& key, Node node);
|
||||
|
||||
private:
|
||||
void setVersionAttribute(ProtocolVersion version);
|
||||
private:
|
||||
void setVersionAttribute(ProtocolVersion version);
|
||||
|
||||
friend class Response;
|
||||
using ValueType = std::variant<std::string, bool, long long>;
|
||||
std::map<std::string, ValueType> _attributes;
|
||||
std::optional<ValueType> _value;
|
||||
std::map<std::string, std::vector<Node>> _children;
|
||||
std::map<std::string, std::vector<Node>> _childrenArrays;
|
||||
};
|
||||
friend class Response;
|
||||
using ValueType = std::variant<std::string, bool, long long>;
|
||||
std::map<std::string, ValueType> _attributes;
|
||||
std::optional<ValueType> _value;
|
||||
std::map<std::string, std::vector<Node>> _children;
|
||||
std::map<std::string, std::vector<Node>> _childrenArrays;
|
||||
};
|
||||
|
||||
static Response createOkResponse(ProtocolVersion protocolVersion);
|
||||
static Response createFailedResponse(ProtocolVersion protocolVersion, const Error& error);
|
||||
static Response createOkResponse(ProtocolVersion protocolVersion);
|
||||
static Response createFailedResponse(ProtocolVersion protocolVersion, const Error& error);
|
||||
|
||||
virtual ~Response() {}
|
||||
Response(const Response&) = delete;
|
||||
Response& operator=(const Response&) = delete;
|
||||
Response(Response&&) = default;
|
||||
Response& operator=(Response&&) = default;
|
||||
virtual ~Response() {}
|
||||
Response(const Response&) = delete;
|
||||
Response& operator=(const Response&) = delete;
|
||||
Response(Response&&) = default;
|
||||
Response& operator=(Response&&) = default;
|
||||
|
||||
void addNode(const std::string& key, Node node);
|
||||
Node& createNode(const std::string& key);
|
||||
Node& createArrayNode(const std::string& key);
|
||||
void addNode(const std::string& key, Node node);
|
||||
Node& createNode(const std::string& key);
|
||||
Node& createArrayNode(const std::string& key);
|
||||
|
||||
void write(std::ostream& os, ResponseFormat format);
|
||||
void write(std::ostream& os, ResponseFormat format);
|
||||
|
||||
private:
|
||||
void writeJSON(std::ostream& os);
|
||||
void writeXML(std::ostream& os);
|
||||
private:
|
||||
void writeJSON(std::ostream& os);
|
||||
void writeXML(std::ostream& os);
|
||||
|
||||
Response() = default;
|
||||
Node _root;
|
||||
};
|
||||
Response() = default;
|
||||
Node _root;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
Reference in New Issue
Block a user