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_playlist.go
67 lines (56 loc) · 1.49 KB
/
get_playlist.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
package audiusclient
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
type GetPlaylistResponse struct {
Data APIPlaylist
}
type internalGetPlaylistResponse struct {
// TODO: Make this an individual track once Audius fixes the endpoint.
Data []APIPlaylist
}
func (c *Client) GetPlaylist(playlistID string) (GetPlaylistResponse, error) {
var getPlaylistResponse GetPlaylistResponse
// Select an audius host:
selectedHostURL, err := c.GetHost()
if err != nil {
return getPlaylistResponse, err
}
requestURL := *selectedHostURL
requestURL.Path = "/v1/playlists/" + playlistID
// Create the request:
urlString := requestURL.String()
req, err := http.NewRequest("GET", urlString, nil)
if err != nil {
return getPlaylistResponse, err
}
// Make the request:
res, err := httpClient.Do(req)
if err != nil {
return getPlaylistResponse, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
// Parse the error:
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return getPlaylistResponse, err
}
err = errors.New(string(body))
return getPlaylistResponse, err
}
// Decode the body:
var internalResponse internalGetPlaylistResponse
err = json.NewDecoder(res.Body).Decode(&internalResponse)
if err != nil {
return getPlaylistResponse, err
}
if len(internalResponse.Data) == 0 {
return getPlaylistResponse, errors.New("unable to find the playlist for the given ID")
}
getPlaylistResponse.Data = internalResponse.Data[0]
return getPlaylistResponse, nil
}