Subsonic API: handle extensions

This commit is contained in:
emeric
2023-10-09 18:30:53 +02:00
parent caf558b6a0
commit 042cd54171
8 changed files with 120 additions and 71 deletions
+1
View File
@@ -8,6 +8,7 @@ add_library(lmssubsonic SHARED
impl/entrypoints/MediaRetrieval.cpp
impl/entrypoints/Playlists.cpp
impl/entrypoints/Searching.cpp
impl/entrypoints/System.cpp
impl/entrypoints/UserManagement.cpp
impl/responses/Album.cpp
impl/responses/Artist.cpp
+4 -20
View File
@@ -42,6 +42,7 @@
#include "entrypoints/MediaRetrieval.hpp"
#include "entrypoints/Playlists.hpp"
#include "entrypoints/Searching.hpp"
#include "entrypoints/System.hpp"
#include "entrypoints/UserManagement.hpp"
#include "ParameterParsing.hpp"
#include "ProtocolVersion.hpp"
@@ -119,29 +120,11 @@ namespace API::Subsonic
throw UserNotAuthorizedError{};
}
Response handlePingRequest(RequestContext& context)
{
return Response::createOkResponse(context.serverProtocolVersion);
}
Response handleGetLicenseRequest(RequestContext& context)
{
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
Response::Node& licenseNode{ response.createNode("license") };
licenseNode.setAttribute("licenseExpires", "2025-09-03T14:46:43");
licenseNode.setAttribute("email", "foo@bar.com");
licenseNode.setAttribute("valid", true);
return response;
}
Response handleNotImplemented(RequestContext&)
{
throw NotImplementedGenericError{};
}
using RequestHandlerFunc = std::function<Response(RequestContext& context)>;
using CheckImplementedFunc = std::function<void()>;
struct RequestEntryPointInfo
@@ -154,8 +137,9 @@ namespace API::Subsonic
static const std::unordered_map<std::string_view, RequestEntryPointInfo> requestEntryPoints
{
// System
{"/ping", {handlePingRequest}},
{"/getLicense", {handleGetLicenseRequest}},
{"/ping", {handlePingRequest}},
{"/getLicense", {handleGetLicenseRequest}},
{"/getOpenSubsonicExtensions", {handleGetOpenSubsonicExtensions}},
// Browsing
{"/getMusicFolders", {handleGetMusicFoldersRequest}},
+44 -49
View File
@@ -19,6 +19,7 @@
#include "SubsonicResponse.hpp"
#include <cassert>
#include <Wt/Json/Array.h>
#include <Wt/Json/Object.h>
#include <Wt/Json/Value.h>
@@ -46,17 +47,13 @@ namespace API::Subsonic
void Response::Node::setValue(std::string_view value)
{
if (!_children.empty() || !_childrenArrays.empty() || !_childrenValues.empty())
throw LmsException{ "Node already has children" };
assert(_children.empty() && _childrenArrays.empty() && _childrenValues.empty());
_value = std::string{ value };
}
void Response::Node::setValue(long long value)
{
if (!_children.empty() || !_childrenArrays.empty() || !_childrenValues.empty())
throw LmsException{ "Node already has children" };
assert(_children.empty() && _childrenArrays.empty() && _childrenValues.empty());
_value = value;
}
@@ -67,42 +64,42 @@ namespace API::Subsonic
void Response::Node::addChild(const std::string& key, Node node)
{
if (_value)
throw LmsException{ "Node already has a value" };
assert(!_value);
_children[key].emplace_back(std::move(node));
}
void Response::Node::createEmptyArrayChild(std::string_view key)
{
if (_value)
throw LmsException{ "Node already has a value" };
assert(!_value);
_childrenArrays.emplace(key, std::vector<Node>{});
}
void Response::Node::addArrayChild(std::string_view key, Node node)
{
if (_value)
throw LmsException{ "Node already has a value" };
assert(!_value);
_childrenArrays[std::string{ key }].emplace_back(std::move(node));
}
void Response::Node::createEmptyArrayValue(std::string_view key)
{
if (_value)
throw LmsException{ "Node already has a value" };
_childrenValues.emplace(key, std::vector<std::string>{});
assert (!_value);
_childrenValues.emplace(key, ValuesType{});
}
void Response::Node::addArrayValue(std::string_view key, std::string_view value)
{
if (_value)
throw LmsException{ "Node already has a value" };
assert(!_value);
auto& values{ _childrenValues[std::string{ key }] };
values.push_back(std::string{ value });
assert(std::all_of(std::cbegin(values) + 1, std::cend(values), [&](const ValueType& value) {return value.index() == values.front().index();}));
}
_childrenValues[std::string{ key }].push_back(std::string{ value });
void Response::Node::addArrayValue(std::string_view key, long long value)
{
assert(!_value);
auto& values {_childrenValues[std::string{ key }]};
values.push_back(value);
assert(std::all_of(std::cbegin(values) + 1, std::cend(values), [&](const ValueType& value) {return value.index() == values.front().index();}));
}
Response::Node& Response::Node::createChild(const std::string& key)
@@ -184,7 +181,7 @@ namespace API::Subsonic
void Response::writeXML(std::ostream& os)
{
std::function<boost::property_tree::ptree(const Response::Node&)> nodeToPropertyTree = [&](const Response::Node& node)
std::function<boost::property_tree::ptree(const Node&)> nodeToPropertyTree = [&](const Node& node)
{
boost::property_tree::ptree res;
@@ -200,37 +197,39 @@ namespace API::Subsonic
res.put("<xmlattr>." + itAttribute.first, std::get<long long>(itAttribute.second));
}
auto valueToPropertyTree = [](const Node::ValueType& value)
{
boost::property_tree::ptree res;
std::visit([&](const auto& rawValue)
{
res.put_value(rawValue);
}, value);
return res;
};
if (node._value)
{
const auto& value{ *node._value };
if (std::holds_alternative<std::string>(value))
res.put_value(std::get<std::string>(value));
else if (std::holds_alternative<bool>(value))
res.put_value(std::get<bool>(value));
else if (std::holds_alternative<float>(value))
res.put_value(std::get<float>(value));
else if (std::holds_alternative<long long>(value))
res.put_value(std::get<long long>(value));
res = valueToPropertyTree(*node._value);
}
else
{
for (const auto& [key, childNodes] : node._children)
{
for (const Response::Node& childNode : childNodes)
for (const Node& childNode : childNodes)
res.add_child(key, nodeToPropertyTree(childNode));
}
for (const auto& [key, childArrayNodes] : node._childrenArrays)
{
for (const Response::Node& childNode : childArrayNodes)
for (const Node& childNode : childArrayNodes)
res.add_child(key, nodeToPropertyTree(childNode));
}
for (const auto& [key, childArrayValues] : node._childrenValues)
{
for (const std::string& value : childArrayValues)
res.add_child(key, boost::property_tree::ptree{ value });
for (const Response::Node::ValueType& value : childArrayValues)
res.add_child(key, valueToPropertyTree(value));
}
}
@@ -251,16 +250,12 @@ namespace API::Subsonic
auto valueToJsonValue{ [](const Node::ValueType& value) -> Json::Value
{
if (std::holds_alternative<std::string>(value))
return Json::Value {std::get<std::string>(value)};
else if (std::holds_alternative<bool>(value))
return Json::Value {std::get<bool>(value)};
else if (std::holds_alternative<float>(value))
return Json::Value{ std::get<float>(value) };
else if (std::holds_alternative<long long>(value))
return Json::Value {std::get<long long>(value)};
throw LmsException("Unexpected value type");
Json::Value res;
std::visit([&](const auto& rawValue)
{
res = Json::Value{ rawValue };
}, value);
return res;
} };
for (auto itAttribute : node._attributes)
@@ -290,8 +285,8 @@ namespace API::Subsonic
for (const auto& [key, childValues] : node._childrenValues)
{
Json::Array array;
for (const std::string& childValue : childValues)
array.emplace_back(Json::Value{ childValue });
for (const Node::ValueType& childValue : childValues)
array.emplace_back(valueToJsonValue(childValue));
res[key] = std::move(array);
}
+4 -1
View File
@@ -217,6 +217,7 @@ namespace API::Subsonic
void addArrayChild(std::string_view key, Node node);
void createEmptyArrayValue(std::string_view key);
void addArrayValue(std::string_view key, std::string_view value);
void addArrayValue(std::string_view key, long long value);
private:
void setVersionAttribute(ProtocolVersion version);
@@ -227,7 +228,9 @@ namespace API::Subsonic
std::optional<ValueType> _value;
std::map<std::string, std::vector<Node>> _children;
std::map<std::string, std::vector<Node>> _childrenArrays;
std::map<std::string, std::vector<std::string>> _childrenValues;
using ValuesType = std::vector<ValueType>;
std::map<std::string, ValuesType> _childrenValues;
};
static Response createOkResponse(ProtocolVersion protocolVersion);
@@ -0,0 +1,34 @@
#include "entrypoints/System.hpp"
namespace API::Subsonic
{
Response handlePingRequest(RequestContext& context)
{
return Response::createOkResponse(context.serverProtocolVersion);
}
Response handleGetLicenseRequest(RequestContext& context)
{
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
Response::Node& licenseNode{ response.createNode("license") };
licenseNode.setAttribute("licenseExpires", "2025-09-03T14:46:43");
licenseNode.setAttribute("email", "foo@bar.com");
licenseNode.setAttribute("valid", true);
return response;
}
Response handleGetOpenSubsonicExtensions(RequestContext& context)
{
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
{
Response::Node& transcodeOffsetNode{ response.createNode("openSubsonicExtensions") };
transcodeOffsetNode.setAttribute("name", "transcodeOffset");
transcodeOffsetNode.addArrayValue("versions", 1);
}
return response;
};
}
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RequestContext.hpp"
#include "SubsonicResponse.hpp"
namespace API::Subsonic
{
Response handlePingRequest(RequestContext& context);
Response handleGetLicenseRequest(RequestContext& context);
Response handleGetOpenSubsonicExtensions(RequestContext& context);
}