From 615c7561adceb023d3eecefb254b8a2409a86f03 Mon Sep 17 00:00:00 2001 From: irlndts Date: Mon, 12 Mar 2018 21:13:09 +0300 Subject: [PATCH] DSCGS-9 Rewrite MasterService --- artists.go | 12 ++++----- discogs.go | 3 +-- examples/discogs_example.go | 2 +- masters.go | 52 +++++++++++++++++-------------------- 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/artists.go b/artists.go index e38092f..19fa402 100644 --- a/artists.go +++ b/artists.go @@ -27,12 +27,6 @@ type Artist struct { Members []Member `json:"members"` } -// ArtistReleases ... -type ArtistReleases struct { - Pagination Page `json:"pagination"` - Releases []ReleaseSource `json:"releases"` -} - // Artist represents a person in the discogs database func (s *ArtistService) Artist(artistID int) (*Artist, error) { var artist *Artist @@ -42,6 +36,12 @@ func (s *ArtistService) Artist(artistID int) (*Artist, error) { return artist, nil } +// ArtistReleases ... +type ArtistReleases struct { + Pagination Page `json:"pagination"` + Releases []ReleaseSource `json:"releases"` +} + // Releases returns a list of releases and masters associated with the artist. func (s *ArtistService) Releases(artistID int, pagination *Pagination) (*ArtistReleases, error) { var releases *ArtistReleases diff --git a/discogs.go b/discogs.go index 5c5f62d..8c39d8c 100644 --- a/discogs.go +++ b/discogs.go @@ -63,7 +63,7 @@ func NewClient(o *Options) (*Client, error) { Release: newReleaseService(o.URL+"/releases/", cur), Artist: newArtistService(o.URL + "/artists/"), Label: newLabelService(base.New()), - Master: newMasterService(base.New()), + Master: newMasterService(o.URL + "/masters/"), Search: newSearchService(base.New()), }, nil } @@ -100,7 +100,6 @@ func request(path string, params url.Values, resp interface{}) error { if err != nil { return err } - fmt.Println(string(body)) return json.Unmarshal(body, &resp) } diff --git a/examples/discogs_example.go b/examples/discogs_example.go index bc5b5db..aef3fb0 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -17,7 +17,7 @@ func main() { return } - release, err := d.Artist.Releases(168101, nil) + release, err := d.Master.Versions(1147170, nil) if err != nil { fmt.Println(err) return diff --git a/masters.go b/masters.go index 246b6f8..5820a91 100644 --- a/masters.go +++ b/masters.go @@ -1,24 +1,21 @@ package discogs import ( - "github.com/irlndts/go-apirequest" - "net/http" + "strconv" ) type MasterService struct { - api *apirequest.API + url string } -type MasterParams struct { - Master_id string -} - -type MasterVersionParams struct { - Master_id string - Page int - Per_page int +func newMasterService(url string) *MasterService { + return &MasterService{ + url: url, + } } +// Master resource represents a set of similar releases. +// Masters (also known as `master releases`) have a `main release` which is often the chronologically earliest. type Master struct { Styles []string `json:"styles"` Genres []string `json:"genres"` @@ -37,27 +34,26 @@ type Master struct { Data_quality string `json:"data_quality"` } +// Master returns a master release +func (s *MasterService) Master(masterID int) (*Master, error) { + var master *Master + if err := request(s.url+strconv.Itoa(masterID), nil, &master); err != nil { + return nil, err + } + return master, nil +} + +// MasterVersions retrieves a list of all releases that are versions of this master. type MasterVersions struct { Pagination Page `json:"pagination"` Versions []Version `json:"versions"` } -func newMasterService(api *apirequest.API) *MasterService { - return &MasterService{ - api: api.Path("masters/"), +// Versions retrieves a list of all Releases that are versions of this master +func (s *MasterService) Versions(masterID int, pagination *Pagination) (*MasterVersions, error) { + var versions *MasterVersions + if err := request(s.url+strconv.Itoa(masterID)+"/versions", pagination.toParams(), &versions); err != nil { + return nil, err } -} - -func (self *MasterService) Master(params *MasterParams) (*Master, *http.Response, error) { - master := new(Master) - apiError := new(APIError) - resp, err := self.api.New().Get(params.Master_id).Receive(master, apiError) - return master, resp, relevantError(err, *apiError) -} - -func (self *MasterService) Versions(params *MasterVersionParams) (*MasterVersions, *http.Response, error) { - versions := new(MasterVersions) - apiError := new(APIError) - resp, err := self.api.New().Get(params.Master_id+"/versions").QueryStruct(params).Receive(versions, apiError) - return versions, resp, relevantError(err, *apiError) + return versions, nil }