DSCGS-7 Rewrite ArtistService like release service

This commit is contained in:
irlndts 2018-03-12 20:51:06 +03:00
parent 8d359bb193
commit ad267389f6
4 changed files with 60 additions and 36 deletions

View File

@ -1,23 +1,16 @@
package discogs package discogs
import ( import "strconv"
"net/http"
"github.com/irlndts/go-apirequest"
)
// ArtistService ... // ArtistService ...
type ArtistService struct { type ArtistService struct {
api *apirequest.API url string
} }
// ArtistParams ... func newArtistService(url string) *ArtistService {
type ArtistParams struct { return &ArtistService{
Artist_id string url: url,
Sort string // year, title, format }
Sort_order string // asc, desc
Page int
Per_page int
} }
// Artist ... // Artist ...
@ -36,30 +29,24 @@ type Artist struct {
// ArtistReleases ... // ArtistReleases ...
type ArtistReleases struct { type ArtistReleases struct {
Paginastion Page `json:"pagination"` Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"` Releases []ReleaseSource `json:"releases"`
} }
func newArtistService(api *apirequest.API) *ArtistService { // Artist represents a person in the discogs database
return &ArtistService{ func (s *ArtistService) Artist(artistID int) (*Artist, error) {
api: api.Path("artists/"), var artist *Artist
if err := request(s.url+strconv.Itoa(artistID), nil, &artist); err != nil {
return nil, err
} }
return artist, nil
} }
// Artist ... // Releases returns a list of releases and masters associated with the artist.
func (self *ArtistService) Artist(params *ArtistParams) (*Artist, *http.Response, error) { func (s *ArtistService) Releases(artistID int, pagination *Pagination) (*ArtistReleases, error) {
artist := new(Artist) var releases *ArtistReleases
apiError := new(APIError) if err := request(s.url+strconv.Itoa(artistID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
resp, err := self.api.New().Get(params.Artist_id).Receive(artist, apiError) }
return artist, resp, relevantError(err, *apiError) return releases, nil
}
// 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)
} }

View File

@ -61,7 +61,7 @@ func NewClient(o *Options) (*Client, error) {
return &Client{ return &Client{
Release: newReleaseService(o.URL+"/releases/", cur), Release: newReleaseService(o.URL+"/releases/", cur),
Artist: newArtistService(base.New()), Artist: newArtistService(o.URL + "/artists/"),
Label: newLabelService(base.New()), Label: newLabelService(base.New()),
Master: newMasterService(base.New()), Master: newMasterService(base.New()),
Search: newSearchService(base.New()), Search: newSearchService(base.New()),
@ -94,11 +94,13 @@ func request(path string, params url.Values, resp interface{}) error {
if err != nil { if err != nil {
return err return err
} }
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body) body, err := ioutil.ReadAll(response.Body)
if err != nil { if err != nil {
return err return err
} }
fmt.Println(string(body))
return json.Unmarshal(body, &resp) return json.Unmarshal(body, &resp)
} }

View File

@ -17,7 +17,7 @@ func main() {
return return
} }
release, err := d.Release.Rating(9893847) release, err := d.Artist.Releases(168101, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View File

@ -1,5 +1,10 @@
package discogs package discogs
import (
"net/url"
"strconv"
)
type Video struct { type Video struct {
Description string `json:"description"` Description string `json:"description"`
Duration int `json:"duration"` Duration int `json:"duration"`
@ -141,3 +146,33 @@ type ReleaseSource struct {
Role string `json:"role"` Role string `json:"role"`
Type string `json:"type"` 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
}