- Document all exported types, functions, methods, and constants across 52 files - Add package-level doc comments to client, models, utils, and ynison packages - Document HTTP method and endpoint path for each API client method - Document model struct fields and DeJSON/DeList/ToJSON/ToMap methods - Fix duplicate package doc comment in internal/models/base.go Co-Authored-By: Claude Code <noreply@anthropic.com>
80 lines
2.4 KiB
Go
80 lines
2.4 KiB
Go
// Package client provides methods for interacting with Yandex Music API endpoints.
|
|
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 their IDs (GET /tracks?artist-ids=...).
|
|
// Returns a slice of Artist pointers, or nil if no IDs are provided.
|
|
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 using GetArtists.
|
|
// Returns an error if no artist is found with the given 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 (GET /artists/{artist_id}/direct-albums).
|
|
// The page and pageSize parameters control pagination.
|
|
func (c *Client) GetArtistAlbums(ctx context.Context, artistID string, page, pageSize int) (*models.ArtistAlbums, error) {
|
|
params := map[string]string{
|
|
"page": fmt.Sprintf("%d", page),
|
|
"page-size": fmt.Sprintf("%d", pageSize),
|
|
}
|
|
|
|
var result models.ArtistAlbums
|
|
if err := c.request.Get(ctx, fmt.Sprintf("/artists/%s/direct-albums", artistID), params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
result.SetClient(c)
|
|
return &result, nil
|
|
}
|
|
|
|
// GetArtistTracks gets tracks by an artist (GET /artists/{artist_id}/tracks).
|
|
// The page and pageSize parameters control pagination.
|
|
func (c *Client) GetArtistTracks(ctx context.Context, artistID string, page, pageSize int) (*models.ArtistTracks, error) {
|
|
params := map[string]string{
|
|
"page": fmt.Sprintf("%d", page),
|
|
"page-size": fmt.Sprintf("%d", pageSize),
|
|
}
|
|
|
|
var result models.ArtistTracks
|
|
if err := c.request.Get(ctx, fmt.Sprintf("/artists/%s/tracks", artistID), params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
result.SetClient(c)
|
|
return &result, nil
|
|
}
|