333 lines
12 KiB
C++
333 lines
12 KiB
C++
#pragma once
|
|
#include "Spotify/RequestParams.hpp"
|
|
#include "Spotify/Types/Album.hpp"
|
|
#include "Spotify/Types/Artist.hpp"
|
|
#include "Spotify/Types/Common.hpp"
|
|
#include "Spotify/Types/Other.hpp"
|
|
#include "Spotify/Types/Player.hpp"
|
|
#include "Spotify/Types/Playlist.hpp"
|
|
#include "Spotify/Types/Track.hpp"
|
|
#include "Spotify/Types/User.hpp"
|
|
#include "ts6bot/Proxy.hpp"
|
|
#include <curl/curl.h>
|
|
#include <expected>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
namespace Spotify {
|
|
|
|
// ============================================================================
|
|
// Error Handling
|
|
// ============================================================================
|
|
|
|
struct ApiError {
|
|
int status;
|
|
std::string message;
|
|
std::string reason;
|
|
|
|
auto GetStatus() const -> int { return status; }
|
|
auto GetMessage() const -> const std::string & { return message; }
|
|
};
|
|
|
|
template <typename T> using Result = std::expected<T, ApiError>;
|
|
|
|
// ============================================================================
|
|
// HTTP Client Implementation Details
|
|
// ============================================================================
|
|
|
|
struct CurlDeleter {
|
|
void operator()(CURL *ptr) const {
|
|
if (ptr)
|
|
curl_easy_cleanup(ptr);
|
|
}
|
|
};
|
|
|
|
enum class HttpMethod { GET, POST, PUT, DELETE };
|
|
|
|
// ============================================================================
|
|
// Main Spotify Client
|
|
// ============================================================================
|
|
|
|
class Client {
|
|
public:
|
|
Client(std::string accessToken);
|
|
~Client() = default;
|
|
|
|
// Configuration
|
|
void SetVerbose(bool verbose) { m_Verbose = verbose; }
|
|
void SetProxy(ProxyConfig config) { m_Proxy = std::move(config); }
|
|
void SetAccessToken(std::string token) { m_AccessToken = std::move(token); }
|
|
|
|
// ========================================================================
|
|
// Albums API
|
|
// ========================================================================
|
|
|
|
auto GetAlbum(const std::string &id, std::optional<std::string> market = {})
|
|
-> Result<Album>;
|
|
|
|
auto GetAlbums(const std::vector<std::string> &ids,
|
|
std::optional<std::string> market = {})
|
|
-> Result<std::vector<Album>>;
|
|
|
|
auto GetAlbumTracks(const std::string &id,
|
|
const Request::AlbumTracksParams ¶ms = {})
|
|
-> Result<PagingObject<SimplifiedTrack>>;
|
|
|
|
auto GetSavedAlbums(const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<SavedAlbum>>;
|
|
|
|
auto SaveAlbums(const std::vector<std::string> &ids) -> Result<void>;
|
|
|
|
auto RemoveSavedAlbums(const std::vector<std::string> &ids) -> Result<void>;
|
|
|
|
auto CheckSavedAlbums(const std::vector<std::string> &ids)
|
|
-> Result<std::vector<bool>>;
|
|
|
|
auto GetNewReleases(const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<SimplifiedAlbum>>;
|
|
|
|
// ========================================================================
|
|
// Artists API
|
|
// ========================================================================
|
|
|
|
auto GetArtist(const std::string &id) -> Result<Artist>;
|
|
|
|
auto GetArtists(const std::vector<std::string> &ids)
|
|
-> Result<std::vector<Artist>>;
|
|
|
|
auto GetArtistAlbums(const std::string &id,
|
|
const Request::ArtistAlbumsParams ¶ms = {})
|
|
-> Result<PagingObject<SimplifiedAlbum>>;
|
|
|
|
auto GetArtistTopTracks(const std::string &id, const std::string &market)
|
|
-> Result<std::vector<Track>>;
|
|
|
|
auto GetArtistRelatedArtists(const std::string &id)
|
|
-> Result<std::vector<Artist>>;
|
|
|
|
// ========================================================================
|
|
// Tracks API
|
|
// ========================================================================
|
|
|
|
auto GetTrack(const std::string &id, std::optional<std::string> market = {})
|
|
-> Result<Track>;
|
|
|
|
auto GetTracks(const std::vector<std::string> &ids,
|
|
std::optional<std::string> market = {})
|
|
-> Result<std::vector<Track>>;
|
|
|
|
auto GetSavedTracks(const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<SavedTrack>>;
|
|
|
|
auto SaveTracks(const std::vector<std::string> &ids) -> Result<void>;
|
|
|
|
auto RemoveSavedTracks(const std::vector<std::string> &ids) -> Result<void>;
|
|
|
|
auto CheckSavedTracks(const std::vector<std::string> &ids)
|
|
-> Result<std::vector<bool>>;
|
|
|
|
auto GetAudioFeatures(const std::string &id) -> Result<AudioFeatures>;
|
|
|
|
auto GetAudioFeaturesForTracks(const std::vector<std::string> &ids)
|
|
-> Result<std::vector<AudioFeatures>>;
|
|
|
|
auto GetAudioAnalysis(const std::string &id) -> Result<AudioAnalysis>;
|
|
|
|
auto GetRecommendations(const Request::RecommendationParams ¶ms)
|
|
-> Result<Recommendations>;
|
|
|
|
// ========================================================================
|
|
// Search API
|
|
// ========================================================================
|
|
|
|
auto Search(const Request::SearchParams ¶ms) -> Result<SearchResults>;
|
|
|
|
auto SearchTracks(const Request::TrackSearchFilters &filters,
|
|
const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<Track>>;
|
|
|
|
auto SearchAlbums(const Request::AlbumSearchFilters &filters,
|
|
const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<SimplifiedAlbum>>;
|
|
|
|
auto SearchArtists(const Request::ArtistSearchFilters &filters,
|
|
const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<Artist>>;
|
|
|
|
// ========================================================================
|
|
// Playlists API
|
|
// ========================================================================
|
|
|
|
auto GetPlaylist(const std::string &id,
|
|
std::optional<std::string> market = {}) -> Result<Playlist>;
|
|
|
|
auto GetPlaylistTracks(const std::string &id,
|
|
const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<PlaylistTrack>>;
|
|
|
|
auto CreatePlaylist(const std::string &user_id,
|
|
const Request::CreatePlaylistParams ¶ms)
|
|
-> Result<Playlist>;
|
|
|
|
auto UpdatePlaylist(const std::string &id,
|
|
const Request::UpdatePlaylistParams ¶ms)
|
|
-> Result<void>;
|
|
|
|
auto AddTracksToPlaylist(const std::string &id,
|
|
const Request::AddTracksToPlaylistParams ¶ms)
|
|
-> Result<std::string>;
|
|
|
|
auto RemoveTracksFromPlaylist(
|
|
const std::string &id,
|
|
const Request::RemoveTracksFromPlaylistParams ¶ms)
|
|
-> Result<std::string>;
|
|
|
|
auto ReorderPlaylistTracks(const std::string &id,
|
|
const Request::ReorderPlaylistParams ¶ms)
|
|
-> Result<std::string>;
|
|
|
|
auto ReplacePlaylistTracks(const std::string &id,
|
|
const Request::ReplacePlaylistTracksParams ¶ms)
|
|
-> Result<void>;
|
|
|
|
auto GetCurrentUserPlaylists(const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<SimplifiedPlaylist>>;
|
|
|
|
auto GetUserPlaylists(const std::string &user_id,
|
|
const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<SimplifiedPlaylist>>;
|
|
|
|
auto GetFeaturedPlaylists(const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<SimplifiedPlaylist>>;
|
|
|
|
auto GetCategoryPlaylists(const std::string &category_id,
|
|
const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<SimplifiedPlaylist>>;
|
|
|
|
// ========================================================================
|
|
// User API
|
|
// ========================================================================
|
|
|
|
auto GetCurrentUser() -> Result<User>;
|
|
|
|
auto GetUser(const std::string &user_id) -> Result<User>;
|
|
|
|
auto GetTopArtists(const Request::TopItemsParams ¶ms = {})
|
|
-> Result<PagingObject<Artist>>;
|
|
|
|
auto GetTopTracks(const Request::TopItemsParams ¶ms = {})
|
|
-> Result<PagingObject<Track>>;
|
|
|
|
// ========================================================================
|
|
// Follow API
|
|
// ========================================================================
|
|
|
|
auto FollowArtistsOrUsers(const Request::FollowParams ¶ms)
|
|
-> Result<void>;
|
|
|
|
auto UnfollowArtistsOrUsers(const Request::UnfollowParams ¶ms)
|
|
-> Result<void>;
|
|
|
|
auto CheckFollowingArtistsOrUsers(const Request::CheckFollowingParams ¶ms)
|
|
-> Result<std::vector<bool>>;
|
|
|
|
auto GetFollowedArtists(std::optional<int> limit = {},
|
|
std::optional<std::string> after = {})
|
|
-> Result<CursorPagingObject<Artist>>;
|
|
|
|
auto FollowPlaylist(const std::string &playlist_id,
|
|
const Request::FollowPlaylistParams ¶ms = {})
|
|
-> Result<void>;
|
|
|
|
auto UnfollowPlaylist(const std::string &playlist_id) -> Result<void>;
|
|
|
|
auto CheckFollowingPlaylist(const std::string &playlist_id,
|
|
const std::vector<std::string> &user_ids)
|
|
-> Result<std::vector<bool>>;
|
|
|
|
// ========================================================================
|
|
// Player API
|
|
// ========================================================================
|
|
|
|
auto GetPlaybackState(std::optional<std::string> market = {})
|
|
-> Result<PlaybackState>;
|
|
|
|
auto GetAvailableDevices() -> Result<std::vector<Device>>;
|
|
|
|
auto GetCurrentlyPlayingTrack(std::optional<std::string> market = {})
|
|
-> Result<PlaybackState>;
|
|
|
|
auto StartOrResumePlayback(const Request::PlayParams ¶ms,
|
|
std::optional<std::string> device_id = {})
|
|
-> Result<void>;
|
|
|
|
auto PausePlayback(std::optional<std::string> device_id = {}) -> Result<void>;
|
|
|
|
auto SkipToNext(std::optional<std::string> device_id = {}) -> Result<void>;
|
|
|
|
auto SkipToPrevious(std::optional<std::string> device_id = {})
|
|
-> Result<void>;
|
|
|
|
auto SeekToPosition(int position_ms,
|
|
std::optional<std::string> device_id = {})
|
|
-> Result<void>;
|
|
|
|
auto SetRepeatMode(RepeatMode mode, std::optional<std::string> device_id = {})
|
|
-> Result<void>;
|
|
|
|
auto SetPlaybackVolume(int volume_percent,
|
|
std::optional<std::string> device_id = {})
|
|
-> Result<void>;
|
|
|
|
auto TogglePlaybackShuffle(bool state,
|
|
std::optional<std::string> device_id = {})
|
|
-> Result<void>;
|
|
|
|
auto TransferPlayback(const Request::TransferPlaybackParams ¶ms)
|
|
-> Result<void>;
|
|
|
|
auto AddToQueue(const Request::QueueParams ¶ms) -> Result<void>;
|
|
|
|
auto GetRecentlyPlayedTracks(std::optional<int> limit = {},
|
|
std::optional<int64_t> after = {},
|
|
std::optional<int64_t> before = {})
|
|
-> Result<CursorPagingObject<PlaylistTrack>>;
|
|
|
|
// ========================================================================
|
|
// Browse API
|
|
// ========================================================================
|
|
|
|
auto GetCategories(const Request::PaginationParams ¶ms = {})
|
|
-> Result<PagingObject<Category>>;
|
|
|
|
auto GetCategory(const std::string &category_id,
|
|
std::optional<std::string> locale = {}) -> Result<Category>;
|
|
|
|
auto GetAvailableGenreSeeds() -> Result<std::vector<std::string>>;
|
|
|
|
auto GetAvailableMarkets() -> Result<std::vector<std::string>>;
|
|
|
|
private:
|
|
std::string m_AccessToken;
|
|
ProxyConfig m_Proxy;
|
|
bool m_Verbose = false;
|
|
|
|
auto PerformRequest(const std::string &url,
|
|
HttpMethod method = HttpMethod::GET,
|
|
const std::string &body = "",
|
|
const std::vector<std::string> &extra_headers = {})
|
|
-> Result<std::string>;
|
|
|
|
static auto WriteCallback(void *contents, size_t size, size_t nmemb,
|
|
std::string *out) -> size_t;
|
|
|
|
auto BuildUrl(const std::string &endpoint,
|
|
const std::map<std::string, std::string> ¶ms = {}) const
|
|
-> std::string;
|
|
auto BuildUrl(const std::string &endpoint,
|
|
const Request::RequestParams ¶ms) const -> std::string;
|
|
auto UrlEncode(const std::string &value) const -> std::string;
|
|
};
|
|
|
|
} // namespace Spotify
|