This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_trending_tracks.go
74 lines (63 loc) · 1.67 KB
/
get_trending_tracks.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
74
package audiusclient
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
type GetTrendingTracksResponse struct {
Data []APITrack
}
func (c *Client) GetTrendingTracks(genre string, time string) (GetTrendingTracksResponse, error) {
var getTrendingTracksResponse GetTrendingTracksResponse
// Select an audius host:
selectedHostURL, err := c.GetHost()
if err != nil {
return getTrendingTracksResponse, err
}
requestURL := *selectedHostURL
requestURL.Path = "/v1/tracks/trending"
// Build the query:
values := requestURL.Query()
if genre != "" {
values.Set("genre", genre)
}
if time != "" {
values.Set("time", time)
}
requestURL.RawQuery = values.Encode()
// Create the request:
urlString := requestURL.String()
req, err := http.NewRequest("GET", urlString, nil)
if err != nil {
return getTrendingTracksResponse, err
}
// Make the request:
res, err := httpClient.Do(req)
if err != nil {
return getTrendingTracksResponse, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
// Parse the error:
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return getTrendingTracksResponse, err
}
err = errors.New(string(body))
return getTrendingTracksResponse, err
}
// Decode the body:
err = json.NewDecoder(res.Body).Decode(&getTrendingTracksResponse)
if err != nil {
return getTrendingTracksResponse, err
}
// Set the stream urls on all of the tracks:
for index, track := range getTrendingTracksResponse.Data {
streamURL := *selectedHostURL
streamURL.Path = "/v1/tracks/" + track.ID + "/stream"
track.StreamURL = streamURL.String()
getTrendingTracksResponse.Data[index] = track
}
return getTrendingTracksResponse, nil
}