This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
go-discogs/releases.go

102 lines
2.8 KiB
Go
Raw Normal View History

2016-03-04 16:08:02 +00:00
package discogs
import (
2018-02-20 16:26:00 +00:00
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
2017-04-25 16:39:32 +00:00
"strconv"
2016-03-04 16:08:02 +00:00
)
2017-04-25 16:39:32 +00:00
// Release serves relesase response from discogs
2016-03-04 16:48:26 +00:00
type Release struct {
2017-04-25 16:39:32 +00:00
Title string `json:"title"`
ID int `json:"id"`
Artists []ArtistSource `json:"artists"`
DataQuality string `json:"data_quality"`
Thumb string `json:"thumb"`
Community Community `json:"community"`
Companies []Company `json:"companies"`
Country string `json:"country"`
DateAdded string `json:"date_added"`
DateChanged string `json:"date_changed"`
EstimatedWeight int `json:"estimated_weight"`
ExtraArtists []ArtistSource `json:"extraartists"`
FormatQuantity int `json:"format_quantity"`
Formats []Format `json:"formats"`
Genres []string `json:"genres"`
Identifiers []Identifier `json:"identifiers"`
Images []Image `json:"images"`
Labels []LabelSource `json:"labels"`
LowestPrice float64 `json:"lowest_price"`
MasterID int `json:"master_id"`
MasterURL string `json:"master_url"`
Notes string `json:"notes,omitempty"`
2017-05-09 15:52:58 +00:00
NumForSale int `json:"num_for_sale,omitempty"`
2017-04-25 16:39:32 +00:00
Released string `json:"released"`
ReleasedFormatted string `json:"released_formatted"`
ResourceURL string `json:"resource_url"`
// Series
Status string `json:"status"`
Styles []string `json:"styles"`
Tracklist []Track `json:"tracklist"`
URI string `json:"uri"`
Videos []Video `json:"videos"`
Year int `json:"year"`
2016-03-09 14:54:25 +00:00
}
2018-01-22 18:15:06 +00:00
type ReqRelease struct {
CurrAbbr string
2016-03-04 16:08:02 +00:00
}
2016-03-04 16:48:26 +00:00
2018-02-20 15:13:04 +00:00
// ReleaseService ...
type ReleaseService struct {
2018-02-20 16:26:00 +00:00
header *http.Header
2018-02-20 15:13:04 +00:00
currency string
}
2018-02-20 16:26:00 +00:00
func newReleaseService(header *http.Header, currency string) *ReleaseService {
2018-02-20 15:13:04 +00:00
return &ReleaseService{
2018-02-20 16:26:00 +00:00
header: header,
2018-02-20 15:13:04 +00:00
currency: currency,
}
}
2017-04-25 16:39:32 +00:00
// Release returns release by release's ID
2018-02-20 15:13:04 +00:00
func (s *ReleaseService) Release(releaseID int) (*Release, error) {
2018-02-20 16:26:00 +00:00
params := url.Values{}
params.Set("CurrAbbr", s.currency)
var release *Release
if err := s.request(strconv.Itoa(releaseID), params, &release); err != nil {
return nil, err
}
return release, nil
}
func (s *ReleaseService) request(path string, params url.Values, resp interface{}) error {
r, err := http.NewRequest("GET", discogsAPI+"releases/"+path+"?"+params.Encode(), nil)
if err != nil {
return err
}
r.Header = *s.header
client := &http.Client{}
response, err := client.Do(r)
if err != nil {
return err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
if err = json.Unmarshal(body, &resp); err != nil {
return err
}
2016-03-10 14:25:55 +00:00
2018-02-20 16:26:00 +00:00
return nil
2016-03-04 16:48:26 +00:00
}