- 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>
155 lines
4.5 KiB
Go
155 lines
4.5 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"
|
|
)
|
|
|
|
// Search performs a multi-type search (GET /search).
|
|
// The searchType parameter specifies what to search for (e.g., "track", "album", "artist").
|
|
func (c *Client) Search(ctx context.Context, query string, searchType string, page, pageSize int) (*models.SearchResult, error) {
|
|
params := map[string]string{
|
|
"text": query,
|
|
"type": searchType,
|
|
"page": fmt.Sprintf("%d", page),
|
|
"page_size": fmt.Sprintf("%d", pageSize),
|
|
}
|
|
|
|
var result models.SearchResult
|
|
if err := c.request.Get(ctx, "/search", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Set client on all results
|
|
if result.Tracks != nil {
|
|
for i := range result.Tracks.Results {
|
|
result.Tracks.Results[i].SetClient(c)
|
|
}
|
|
}
|
|
if result.Albums != nil {
|
|
for i := range result.Albums.Results {
|
|
result.Albums.Results[i].SetClient(c)
|
|
}
|
|
}
|
|
if result.Artists != nil {
|
|
for i := range result.Artists.Results {
|
|
result.Artists.Results[i].SetClient(c)
|
|
}
|
|
}
|
|
if result.Playlists != nil {
|
|
for i := range result.Playlists.Results {
|
|
result.Playlists.Results[i].SetClient(c)
|
|
}
|
|
}
|
|
|
|
return &result, nil
|
|
}
|
|
|
|
// SearchTracks searches for tracks using Search with type "track".
|
|
func (c *Client) SearchTracks(ctx context.Context, query string, page, pageSize int) (*models.SearchTracksResult, error) {
|
|
result, err := c.Search(ctx, query, "track", page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Tracks, nil
|
|
}
|
|
|
|
// SearchAlbums searches for albums using Search with type "album".
|
|
func (c *Client) SearchAlbums(ctx context.Context, query string, page, pageSize int) (*models.SearchAlbumsResult, error) {
|
|
result, err := c.Search(ctx, query, "album", page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Albums, nil
|
|
}
|
|
|
|
// SearchArtists searches for artists using Search with type "artist".
|
|
func (c *Client) SearchArtists(ctx context.Context, query string, page, pageSize int) (*models.SearchArtistsResult, error) {
|
|
result, err := c.Search(ctx, query, "artist", page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Artists, nil
|
|
}
|
|
|
|
// SearchPlaylists searches for playlists using Search with type "playlist".
|
|
func (c *Client) SearchPlaylists(ctx context.Context, query string, page, pageSize int) (*models.SearchPlaylistsResult, error) {
|
|
result, err := c.Search(ctx, query, "playlist", page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Playlists, nil
|
|
}
|
|
|
|
// SearchVideos searches for videos using Search with type "video".
|
|
func (c *Client) SearchVideos(ctx context.Context, query string, page, pageSize int) (*models.SearchVideosResult, error) {
|
|
result, err := c.Search(ctx, query, "video", page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Videos, nil
|
|
}
|
|
|
|
// SearchUsers searches for users using Search with type "user".
|
|
func (c *Client) SearchUsers(ctx context.Context, query string, page, pageSize int) (*models.SearchUsersResult, error) {
|
|
result, err := c.Search(ctx, query, "user", page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Users, nil
|
|
}
|
|
|
|
// SearchSuggestions gets search suggestions for a query (GET /search/suggest?type=suggest).
|
|
func (c *Client) SearchSuggestions(ctx context.Context, query string) ([]string, error) {
|
|
params := map[string]string{
|
|
"text": query,
|
|
"type": "suggest",
|
|
}
|
|
|
|
var result struct {
|
|
Suggestions []string `json:"suggestions"`
|
|
}
|
|
if err := c.request.Get(ctx, "/search/suggest", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Suggestions, nil
|
|
}
|
|
|
|
// SearchBanners gets search banners for a query (GET /search/banner?type=banner).
|
|
func (c *Client) SearchBanners(ctx context.Context, query string) ([]models.SearchBanner, error) {
|
|
params := map[string]string{
|
|
"text": query,
|
|
"type": "banner",
|
|
}
|
|
|
|
var result struct {
|
|
Banners []models.SearchBanner `json:"banners"`
|
|
}
|
|
if err := c.request.Get(ctx, "/search/banner", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Banners, nil
|
|
}
|
|
|
|
// SearchTags gets search tags for a query (GET /search/tag?type=tag).
|
|
func (c *Client) SearchTags(ctx context.Context, query string) ([]models.Tag, error) {
|
|
params := map[string]string{
|
|
"text": query,
|
|
"type": "tag",
|
|
}
|
|
|
|
var result struct {
|
|
Tags []models.Tag `json:"tags"`
|
|
}
|
|
if err := c.request.Get(ctx, "/search/tag", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Tags, nil
|
|
}
|
|
|
|
// SearchResult represents search results
|
|
// This is defined in models/search.go
|