From ad267389f6dafb897adccea87f628d0d93d4d695 Mon Sep 17 00:00:00 2001 From: irlndts Date: Mon, 12 Mar 2018 20:51:06 +0300 Subject: [PATCH] DSCGS-7 Rewrite ArtistService like release service --- artists.go | 55 ++++++++++++++----------------------- discogs.go | 4 ++- examples/discogs_example.go | 2 +- models.go | 35 +++++++++++++++++++++++ 4 files changed, 60 insertions(+), 36 deletions(-) diff --git a/artists.go b/artists.go index 0323d55..e38092f 100644 --- a/artists.go +++ b/artists.go @@ -1,23 +1,16 @@ package discogs -import ( - "net/http" - - "github.com/irlndts/go-apirequest" -) +import "strconv" // ArtistService ... type ArtistService struct { - api *apirequest.API + url string } -// ArtistParams ... -type ArtistParams struct { - Artist_id string - Sort string // year, title, format - Sort_order string // asc, desc - Page int - Per_page int +func newArtistService(url string) *ArtistService { + return &ArtistService{ + url: url, + } } // Artist ... @@ -36,30 +29,24 @@ type Artist struct { // ArtistReleases ... type ArtistReleases struct { - Paginastion Page `json:"pagination"` - Releases []ReleaseSource `json:"releases"` + Pagination Page `json:"pagination"` + Releases []ReleaseSource `json:"releases"` } -func newArtistService(api *apirequest.API) *ArtistService { - return &ArtistService{ - api: api.Path("artists/"), +// Artist represents a person in the discogs database +func (s *ArtistService) Artist(artistID int) (*Artist, error) { + var artist *Artist + if err := request(s.url+strconv.Itoa(artistID), nil, &artist); err != nil { + return nil, err } + return artist, nil } -// Artist ... -func (self *ArtistService) Artist(params *ArtistParams) (*Artist, *http.Response, error) { - artist := new(Artist) - apiError := new(APIError) - - resp, err := self.api.New().Get(params.Artist_id).Receive(artist, apiError) - return artist, resp, relevantError(err, *apiError) -} - -// Releases ... -func (self *ArtistService) Releases(params *ArtistParams) (*ArtistReleases, *http.Response, error) { - releases := new(ArtistReleases) - apiError := new(APIError) - - resp, err := self.api.New().Get(params.Artist_id+"/releases").QueryStruct(params).Receive(releases, apiError) - return releases, resp, relevantError(err, *apiError) +// 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 + if err := request(s.url+strconv.Itoa(artistID)+"/releases", pagination.toParams(), &releases); err != nil { + return nil, err + } + return releases, nil } diff --git a/discogs.go b/discogs.go index 58f1fa9..5c5f62d 100644 --- a/discogs.go +++ b/discogs.go @@ -61,7 +61,7 @@ func NewClient(o *Options) (*Client, error) { return &Client{ Release: newReleaseService(o.URL+"/releases/", cur), - Artist: newArtistService(base.New()), + Artist: newArtistService(o.URL + "/artists/"), Label: newLabelService(base.New()), Master: newMasterService(base.New()), Search: newSearchService(base.New()), @@ -94,11 +94,13 @@ func request(path string, params url.Values, resp interface{}) error { if err != nil { return err } + defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) 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 12997bc..bc5b5db 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -17,7 +17,7 @@ func main() { return } - release, err := d.Release.Rating(9893847) + release, err := d.Artist.Releases(168101, nil) if err != nil { fmt.Println(err) return diff --git a/models.go b/models.go index e9df596..a2c50c4 100644 --- a/models.go +++ b/models.go @@ -1,5 +1,10 @@ package discogs +import ( + "net/url" + "strconv" +) + type Video struct { Description string `json:"description"` Duration int `json:"duration"` @@ -141,3 +146,33 @@ type ReleaseSource struct { Role string `json:"role"` Type string `json:"type"` } + +// Pagination ... +type Pagination struct { + Sort string // year, title, format + SortOrder string // asc, desc + Page int + PerPage int +} + +// toParams converts pagaination params to request values +func (p *Pagination) toParams() url.Values { + if p == nil { + return nil + } + + params := url.Values{} + if p.Sort != "" { + params.Set("sort", p.Sort) + } + if p.SortOrder != "" { + params.Set("sort_order", p.SortOrder) + } + if p.Page != 0 { + params.Set("page", strconv.Itoa(p.Page)) + } + if p.PerPage != 0 { + params.Set("per_page", strconv.Itoa(p.PerPage)) + } + return params +}