Merge pull request #12 from irlndts/DSCGS-7

This commit is contained in:
Artem Piskun 2018-03-12 20:54:54 +03:00 committed by GitHub
commit 93ddab1b21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 36 deletions

View File

@ -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"`
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 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
}
// 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)
return releases, nil
}

View File

@ -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)
}

View File

@ -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

View File

@ -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
}