-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
73 lines (59 loc) · 1.93 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gotidal
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
const (
searchURL = "/search"
SearchTypeAlbums = "ALBUMS"
SearchTypeArtists = "ARTISTS"
SearchTypeTracks = "TRACKS"
SearchTypeVideos = "VIDEOS"
SearchPopularityWorldwide = "WORLDWIDE"
SearchPopularityCountry = "COUNTRY"
)
// SearchParams defines the request parameters used by the TIDAL search API endpoint.
// See: https://developer.tidal.com/apiref?spec=search&ref=search
type SearchParams struct {
// Search query in plain text.
// Example: Beyoncé
Query string `json:"query"`
// Target search type. Optional. Searches for all types if not specified.
// Example: ARTISTS, ALBUMS, TRACKS, VIDEOS
Type string `json:"type"`
// Pagination offset (in number of items). Required if 'query' is provided.
// Example: 0
Offset int `json:"offset"`
// Page size. Required if 'query' is provided.
// Example: 10
Limit int `json:"limit"`
// Specify which popularity type to apply for query result: either worldwide or country popularity.
// Worldwide popularity is used by default if nothing is specified.
// Example: WORLDWIDE, COUNTRY
Popularity string `json:"popularity"`
}
var ErrMissingRequiredParameters = errors.New("both the Query and the CountryCode parameters are required")
type SearchResults struct {
Albums []Album
Artists []Artist
Tracks []Track
Videos []Video
}
func (c *Client) Search(ctx context.Context, params SearchParams) (*SearchResults, error) {
if params.Query == "" {
return nil, ErrMissingRequiredParameters
}
response, err := c.request(ctx, http.MethodGet, searchURL, params)
if err != nil {
return nil, fmt.Errorf("failed to connect to the search endpoint: %w", err)
}
var results SearchResults
err = json.Unmarshal(response, &results)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal the search response body: %w", err)
}
return &results, nil
}