Files
Echoion/include/Core/Spotify/RequestParams.hpp
T
2026-01-18 18:22:09 +03:00

496 lines
17 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <optional>
#include <map>
#include <format>
#include <ranges>
#include <algorithm>
namespace Spotify::Request {
// ============================================================================
// Base Request Parameter Class
// ============================================================================
class RequestParams {
public:
virtual ~RequestParams() = default;
virtual auto ToQueryParams() const -> std::map<std::string, std::string> = 0;
};
// ============================================================================
// Search Type Enum
// ============================================================================
enum class SearchType {
Album,
Artist,
Playlist,
Track,
Show,
Episode,
Audiobook
};
inline auto SearchTypeToString(SearchType type) -> std::string {
switch (type) {
case SearchType::Album: return "album";
case SearchType::Artist: return "artist";
case SearchType::Playlist: return "playlist";
case SearchType::Track: return "track";
case SearchType::Show: return "show";
case SearchType::Episode: return "episode";
case SearchType::Audiobook: return "audiobook";
}
return "";
}
// ============================================================================
// Time Range Enum
// ============================================================================
enum class TimeRange {
LongTerm,
MediumTerm,
ShortTerm
};
inline auto TimeRangeToString(TimeRange range) -> std::string {
switch (range) {
case TimeRange::LongTerm: return "long_term";
case TimeRange::MediumTerm: return "medium_term";
case TimeRange::ShortTerm: return "short_term";
}
return "medium_term";
}
// ============================================================================
// Pagination Parameters
// ============================================================================
struct PaginationParams : public RequestParams {
public:
std::optional<int> limit = 20;
std::optional<int> offset = 0;
std::optional<std::string> market;
auto ToQueryParams() const -> std::map<std::string, std::string> override {
std::map<std::string, std::string> params;
if (limit) params["limit"] = std::to_string(*limit);
if (offset) params["offset"] = std::to_string(*offset);
if (market) params["market"] = *market;
return params;
}
};
// ============================================================================
// Search Parameters
// ============================================================================
struct SearchParams : public RequestParams {
public:
//You can narrow down your search using field filters. The available filters are album, artist, track, year, upc, tag:hipster, tag:new, isrc, and genre. Each field filter only applies to certain result types.
//The artist and year filters can be used while searching albums, artists and tracks. You can filter on a single year or a range (e.g. 1955-1960).
//The album filter can be used while searching albums and tracks.
//The genre filter can be used while searching artists and tracks.
//The isrc and track filters can be used while searching tracks.
//The upc, tag:new and tag:hipster filters can only be used while searching albums. The tag:new filter will return albums released in the past two weeks and tag:hipster can be used to return only albums with the lowest 10% popularity.
//Example: q=remaster%2520track%3ADoxy%2520artist%3AMiles%2520Davis
std::string query;
std::vector<SearchType> types;
std::optional<std::string> market;
std::optional<int> limit = 20;
std::optional<int> offset = 0;
std::optional<bool> include_external;
auto ToQueryParams() const -> std::map<std::string, std::string> override {
std::map<std::string, std::string> params;
params["q"] = query;
std::vector<std::string> type_strings;
for (const auto& type : types) {
type_strings.push_back(SearchTypeToString(type));
}
params["type"] = std::views::join_with(type_strings, ',') | std::ranges::to<std::string>();
if (market) params["market"] = *market;
if (limit) params["limit"] = std::to_string(*limit);
if (offset) params["offset"] = std::to_string(*offset);
if (include_external) params["include_external"] = *include_external ? "audio" : "";
return params;
}
};
// ============================================================================
// Track Search Filters
// ============================================================================
struct TrackSearchFilters : public RequestParams {
public:
std::optional<std::string> track;
std::optional<std::string> artist;
std::optional<std::string> album;
std::optional<int> year;
std::optional<std::string> genre;
std::optional<std::string> isrc;
std::optional<std::string> upc;
std::optional<bool> is_new;
std::optional<bool> hipster;
auto ToQueryString() const -> std::string {
std::vector<std::string> parts;
if (track) parts.push_back(std::format("track:{}", *track));
if (artist) parts.push_back(std::format("artist:{}", *artist));
if (album) parts.push_back(std::format("album:{}", *album));
if (year) parts.push_back(std::format("year:{}", *year));
if (genre) parts.push_back(std::format("genre:{}", *genre));
if (isrc) parts.push_back(std::format("isrc:{}", *isrc));
if (upc) parts.push_back(std::format("upc:{}", *upc));
if (is_new && *is_new) parts.push_back("tag:new");
if (hipster && *hipster) parts.push_back("tag:hipster");
return std::views::join_with(parts, ' ') | std::ranges::to<std::string>();
}
auto ToQueryParams() const -> std::map<std::string, std::string> override {
return {};
}
};
// ============================================================================
// Album Search Filters
// ============================================================================
struct AlbumSearchFilters : public RequestParams {
public:
std::optional<std::string> album;
std::optional<std::string> artist;
std::optional<int> year;
std::optional<std::string> genre;
std::optional<std::string> upc;
auto ToQueryString() const -> std::string {
std::vector<std::string> parts;
if (album) parts.push_back(std::format("album:{}", *album));
if (artist) parts.push_back(std::format("artist:{}", *artist));
if (year) parts.push_back(std::format("year:{}", *year));
if (genre) parts.push_back(std::format("genre:{}", *genre));
if (upc) parts.push_back(std::format("upc:{}", *upc));
return std::views::join_with(parts, ' ') | std::ranges::to<std::string>();
}
auto ToQueryParams() const -> std::map<std::string, std::string> override {
return {};
}
};
// ============================================================================
// Artist Search Filters
// ============================================================================
struct ArtistSearchFilters : public RequestParams {
public:
std::optional<std::string> artist;
std::optional<std::string> genre;
auto ToQueryString() const -> std::string {
std::vector<std::string> parts;
if (artist) parts.push_back(std::format("artist:{}", *artist));
if (genre) parts.push_back(std::format("genre:{}", *genre));
return std::views::join_with(parts, ' ') | std::ranges::to<std::string>();
}
auto ToQueryParams() const -> std::map<std::string, std::string> override {
return {};
}
};
// ============================================================================
// Album Tracks Parameters
// ============================================================================
struct AlbumTracksParams : public RequestParams {
public:
std::optional<int> limit = 20;
std::optional<int> offset = 0;
std::optional<std::string> market;
auto ToQueryParams() const -> std::map<std::string, std::string> override {
std::map<std::string, std::string> params;
if (limit) params["limit"] = std::to_string(*limit);
if (offset) params["offset"] = std::to_string(*offset);
if (market) params["market"] = *market;
return params;
}
};
// ============================================================================
// Artist Albums Parameters
// ============================================================================
struct ArtistAlbumsParams : public RequestParams {
public:
std::optional<std::vector<std::string>> include_groups;
std::optional<std::string> market;
std::optional<int> limit = 20;
std::optional<int> offset = 0;
auto ToQueryParams() const -> std::map<std::string, std::string> override {
std::map<std::string, std::string> params;
if (include_groups && !include_groups->empty()) {
params["include_groups"] = std::views::join_with(*include_groups, ',') | std::ranges::to<std::string>();
}
if (market) params["market"] = *market;
if (limit) params["limit"] = std::to_string(*limit);
if (offset) params["offset"] = std::to_string(*offset);
return params;
}
};
// ============================================================================
// Top Items Parameters
// ============================================================================
struct TopItemsParams : public RequestParams {
public:
std::optional<TimeRange> time_range = TimeRange::MediumTerm;
std::optional<int> limit = 20;
std::optional<int> offset = 0;
auto ToQueryParams() const -> std::map<std::string, std::string> override {
std::map<std::string, std::string> params;
if (time_range) params["time_range"] = TimeRangeToString(*time_range);
if (limit) params["limit"] = std::to_string(*limit);
if (offset) params["offset"] = std::to_string(*offset);
return params;
}
};
// ============================================================================
// Recommendation Parameters
// ============================================================================
struct RecommendationParams : public RequestParams {
public:
std::optional<std::vector<std::string>> seed_artists;
std::optional<std::vector<std::string>> seed_genres;
std::optional<std::vector<std::string>> seed_tracks;
std::optional<int> limit = 20;
std::optional<std::string> market;
// Tunable attributes
std::optional<float> min_acousticness;
std::optional<float> max_acousticness;
std::optional<float> target_acousticness;
std::optional<float> min_danceability;
std::optional<float> max_danceability;
std::optional<float> target_danceability;
std::optional<int> min_duration_ms;
std::optional<int> max_duration_ms;
std::optional<int> target_duration_ms;
std::optional<float> min_energy;
std::optional<float> max_energy;
std::optional<float> target_energy;
std::optional<float> min_instrumentalness;
std::optional<float> max_instrumentalness;
std::optional<float> target_instrumentalness;
std::optional<int> min_key;
std::optional<int> max_key;
std::optional<int> target_key;
std::optional<float> min_liveness;
std::optional<float> max_liveness;
std::optional<float> target_liveness;
std::optional<float> min_loudness;
std::optional<float> max_loudness;
std::optional<float> target_loudness;
std::optional<int> min_mode;
std::optional<int> max_mode;
std::optional<int> target_mode;
std::optional<int> min_popularity;
std::optional<int> max_popularity;
std::optional<int> target_popularity;
std::optional<float> min_speechiness;
std::optional<float> max_speechiness;
std::optional<float> target_speechiness;
std::optional<float> min_tempo;
std::optional<float> max_tempo;
std::optional<float> target_tempo;
std::optional<int> min_time_signature;
std::optional<int> max_time_signature;
std::optional<int> target_time_signature;
std::optional<float> min_valence;
std::optional<float> max_valence;
std::optional<float> target_valence;
auto ToQueryParams() const -> std::map<std::string, std::string> override {
std::map<std::string, std::string> params;
if (seed_artists && !seed_artists->empty()) {
params["seed_artists"] = std::views::join_with(*seed_artists, ',') | std::ranges::to<std::string>();
}
if (seed_genres && !seed_genres->empty()) {
params["seed_genres"] = std::views::join_with(*seed_genres, ',') | std::ranges::to<std::string>();
}
if (seed_tracks && !seed_tracks->empty()) {
params["seed_tracks"] = std::views::join_with(*seed_tracks, ',') | std::ranges::to<std::string>();
}
if (limit) params["limit"] = std::to_string(*limit);
if (market) params["market"] = *market;
// Add all tunable attributes
if (min_acousticness) params["min_acousticness"] = std::to_string(*min_acousticness);
if (max_acousticness) params["max_acousticness"] = std::to_string(*max_acousticness);
if (target_acousticness) params["target_acousticness"] = std::to_string(*target_acousticness);
if (min_danceability) params["min_danceability"] = std::to_string(*min_danceability);
if (max_danceability) params["max_danceability"] = std::to_string(*max_danceability);
if (target_danceability) params["target_danceability"] = std::to_string(*target_danceability);
if (min_energy) params["min_energy"] = std::to_string(*min_energy);
if (max_energy) params["max_energy"] = std::to_string(*max_energy);
if (target_energy) params["target_energy"] = std::to_string(*target_energy);
if (min_popularity) params["min_popularity"] = std::to_string(*min_popularity);
if (max_popularity) params["max_popularity"] = std::to_string(*max_popularity);
if (target_popularity) params["target_popularity"] = std::to_string(*target_popularity);
// Add remaining attributes as needed...
return params;
}
};
// ============================================================================
// Create Playlist Parameters
// ============================================================================
struct CreatePlaylistParams {
std::string name;
std::optional<bool> public_playlist = true;
std::optional<bool> collaborative = false;
std::optional<std::string> description;
};
// ============================================================================
// Update Playlist Parameters
// ============================================================================
struct UpdatePlaylistParams {
std::optional<std::string> name;
std::optional<bool> public_playlist;
std::optional<bool> collaborative;
std::optional<std::string> description;
};
// ============================================================================
// Playlist Track Operations
// ============================================================================
struct AddTracksToPlaylistParams {
std::vector<std::string> uris;
std::optional<int> position;
};
struct RemoveTracksFromPlaylistParams {
std::vector<std::string> uris;
std::optional<std::string> snapshot_id;
};
struct ReorderPlaylistParams {
int range_start;
int insert_before;
std::optional<int> range_length = 1;
std::optional<std::string> snapshot_id;
};
struct ReplacePlaylistTracksParams {
std::vector<std::string> uris;
};
// ============================================================================
// Playback Control Parameters
// ============================================================================
struct PlayParams {
std::optional<std::string> context_uri;
std::optional<std::vector<std::string>> uris;
std::optional<std::map<std::string, int>> offset;
std::optional<int> position_ms;
};
struct TransferPlaybackParams {
std::vector<std::string> device_ids;
std::optional<bool> play;
};
struct QueueParams {
std::string uri;
std::optional<std::string> device_id;
};
struct RequestBody {
std::vector<std::string> ids;
};
// ============================================================================
// Follow Parameters
// ============================================================================
struct ErrorResponse {
struct Error {
int status;
std::string message;
} error;
};
enum class FollowType {
Artist,
User
};
inline auto FollowTypeToString(FollowType type) -> std::string {
return type == FollowType::Artist ? "artist" : "user";
}
struct FollowParams {
FollowType type;
std::vector<std::string> ids;
};
struct UnfollowParams {
FollowType type;
std::vector<std::string> ids;
};
struct CheckFollowingParams {
FollowType type;
std::vector<std::string> ids;
};
struct FollowPlaylistParams {
std::optional<bool> public_playlist = true;
};
} // namespace Spotify::Request