Files
yandex-music-api-go/internal/client/supplements.go
T
MrixsandClaude Code 0456ca0665 Add comprehensive godoc documentation to all exported symbols
- 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>
2026-10-18 08:57:43 +03:00

142 lines
4.5 KiB
Go

// Package client provides methods for interacting with Yandex Music API endpoints.
package client
import (
"context"
"fmt"
"strconv"
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/models"
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/utils"
)
// GetTrackSupplement gets supplement for a track (GET /tracks/{track_id}/supplement).
func (c *Client) GetTrackSupplement(ctx context.Context, trackID string) (*models.Supplement, error) {
path := fmt.Sprintf("/tracks/%s/supplement", trackID)
var result models.Supplement
if err := c.request.Get(ctx, path, nil, &result); err != nil {
return nil, err
}
return &result, nil
}
// GetTrackLyrics gets lyrics for a track (GET /tracks/{track_id}/lyrics).
// The format parameter specifies the lyrics format (e.g., "TEXT" or "LRC").
// The request is signed using the default signing key.
func (c *Client) GetTrackLyrics(ctx context.Context, trackID string, format string) (*models.TrackLyrics, error) {
path := fmt.Sprintf("/tracks/%s/lyrics", trackID)
// Generate signature for the request
sign := utils.GetSignRequest(trackID, utils.DefaultSignKey)
params := map[string]string{
"format": format,
"timeStamp": strconv.FormatInt(sign.Timestamp, 10),
"sign": sign.Value,
}
var result models.TrackLyrics
if err := c.request.Get(ctx, path, params, &result); err != nil {
return nil, err
}
return &result, nil
}
// GetTrackLyricsText fetches the full lyrics text from the download URL provided in TrackLyrics.
func (c *Client) GetTrackLyricsText(ctx context.Context, trackLyrics *models.TrackLyrics) (string, error) {
data, err := c.request.Download(ctx, trackLyrics.DownloadURL)
if err != nil {
return "", err
}
return string(data), nil
}
// GetLyrics gets lyrics info for a track using the legacy endpoint (GET /tracks/lyrics).
func (c *Client) GetLyrics(ctx context.Context, trackID string) (*models.LyricsInfo, error) {
params := map[string]string{
"track_id": trackID,
}
var result models.LyricsInfo
if err := c.request.Get(ctx, "/tracks/lyrics", params, &result); err != nil {
return nil, err
}
return &result, nil
}
// GetVideoSupplement gets video supplement for a track using the legacy endpoint (GET /tracks/video-supplement).
func (c *Client) GetVideoSupplement(ctx context.Context, trackID string) (*models.VideoSupplement, error) {
params := map[string]string{
"track_id": trackID,
}
var result models.VideoSupplement
if err := c.request.Get(ctx, "/tracks/video-supplement", params, &result); err != nil {
return nil, err
}
return &result, nil
}
// GetLyricsByTrackID is an alias for GetTrackLyrics for backward compatibility (GET /tracks/{track_id}/lyrics).
func (c *Client) GetLyricsByTrackID(ctx context.Context, trackID string, format string) (*models.TrackLyrics, error) {
return c.GetTrackLyrics(ctx, trackID, format)
}
// LyricsFormat represents the lyrics format.
type LyricsFormat string
const (
// LyricsFormatText is plain text lyrics format.
LyricsFormatText LyricsFormat = "TEXT"
// LyricsFormatLRC is LRC format lyrics with timestamps.
LyricsFormatLRC LyricsFormat = "LRC"
)
// GetTrackLyricsWithOptions gets lyrics with options using a LyricsFormat value.
func (c *Client) GetTrackLyricsWithOptions(ctx context.Context, trackID string, format LyricsFormat) (*models.TrackLyrics, error) {
return c.GetTrackLyrics(ctx, trackID, string(format))
}
// SupplementOptions holds options for supplement requests.
type SupplementOptions struct {
TrackID string
}
// GetSupplement gets track supplement with options.
func (c *Client) GetSupplement(ctx context.Context, opts SupplementOptions) (*models.Supplement, error) {
return c.GetTrackSupplement(ctx, opts.TrackID)
}
// LyricsOptions holds options for lyrics requests.
type LyricsOptions struct {
TrackID string
Format LyricsFormat
}
// GetLyricsWithOptions gets track lyrics with options.
func (c *Client) GetLyricsWithOptions(ctx context.Context, opts LyricsOptions) (*models.TrackLyrics, error) {
format := string(opts.Format)
if format == "" {
format = string(LyricsFormatText)
}
return c.GetTrackLyrics(ctx, opts.TrackID, format)
}
// Sign represents a request signature for signed API requests.
type Sign struct {
Timestamp int64
Value string
}
// GetSignRequest creates a signature for a track request (exported for external use).
// It delegates to utils.GetSignRequest with the default signing key.
func GetSignRequest(trackID string) Sign {
h := utils.GetSignRequest(trackID, utils.DefaultSignKey)
return Sign{
Timestamp: h.Timestamp,
Value: h.Value,
}
}