90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/models"
|
|
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/utils"
|
|
)
|
|
|
|
// GetArtists gets artists by IDs
|
|
func (c *Client) GetArtists(ctx context.Context, artistIDs []string) ([]*models.Artist, error) {
|
|
if len(artistIDs) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
params := map[string]string{
|
|
"artist-ids": joinIDs(artistIDs),
|
|
}
|
|
|
|
var result []models.Artist
|
|
if err := c.request.Get(ctx, "/artists", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
artists := make([]*models.Artist, len(result))
|
|
for i := range result {
|
|
result[i].SetClient(c)
|
|
artists[i] = &result[i]
|
|
}
|
|
return artists, nil
|
|
}
|
|
|
|
// GetArtist gets a single artist by ID
|
|
func (c *Client) GetArtist(ctx context.Context, artistID string) (*models.Artist, error) {
|
|
artists, err := c.GetArtists(ctx, []string{artistID})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(artists) == 0 {
|
|
return nil, utils.NewYandexMusicError("artist not found", utils.ErrNotFound)
|
|
}
|
|
return artists[0], nil
|
|
}
|
|
|
|
// GetArtistAlbums gets albums by an artist
|
|
func (c *Client) GetArtistAlbums(ctx context.Context, artistID string, page, pageSize int) ([]*models.Album, error) {
|
|
params := map[string]string{
|
|
"artist-id": artistID,
|
|
"page": fmt.Sprintf("%d", page),
|
|
"page-size": fmt.Sprintf("%d", pageSize),
|
|
}
|
|
|
|
var result []models.Album
|
|
if err := c.request.Get(ctx, "/artists/albums", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
albums := make([]*models.Album, len(result))
|
|
for i := range result {
|
|
result[i].SetClient(c)
|
|
albums[i] = &result[i]
|
|
}
|
|
return albums, nil
|
|
}
|
|
|
|
// GetArtistTracks gets tracks by an artist
|
|
func (c *Client) GetArtistTracks(ctx context.Context, artistID string, page, pageSize int) ([]*models.Track, error) {
|
|
params := map[string]string{
|
|
"artist-id": artistID,
|
|
"page": fmt.Sprintf("%d", page),
|
|
"page-size": fmt.Sprintf("%d", pageSize),
|
|
}
|
|
|
|
var result []models.Track
|
|
if err := c.request.Get(ctx, "/artists/tracks", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tracks := make([]*models.Track, len(result))
|
|
for i := range result {
|
|
result[i].SetClient(c)
|
|
tracks[i] = &result[i]
|
|
}
|
|
return tracks, nil
|
|
}
|
|
|
|
// Need to add fmt import
|
|
// "fmt"
|