Some Improvements
This commit is contained in:
parent
055007c459
commit
d0f617733a
63
discogs.go
63
discogs.go
@ -11,28 +11,45 @@ const (
|
|||||||
discogsAPI = "https://api.discogs.com/"
|
discogsAPI = "https://api.discogs.com/"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
Currency string
|
||||||
|
UserAgent string
|
||||||
|
}
|
||||||
|
|
||||||
// Client is a Discogs client for making Discogs API requests.
|
// Client is a Discogs client for making Discogs API requests.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
api *apirequest.API
|
api *apirequest.API
|
||||||
currency string
|
currency string
|
||||||
Master *MasterService
|
|
||||||
Artist *ArtistService
|
// services
|
||||||
Label *LabelService
|
Release *ReleaseService
|
||||||
Search *SearchService
|
Master *MasterService
|
||||||
|
Artist *ArtistService
|
||||||
|
Label *LabelService
|
||||||
|
Search *SearchService
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a new Client.
|
// NewClient returns a new Client.
|
||||||
func NewClient() *Client {
|
func NewClient(o *Options) (*Client, error) {
|
||||||
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI)
|
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI)
|
||||||
return &Client{
|
if o.UserAgent != "" {
|
||||||
api: base,
|
base.Set("User-Agent", o.UserAgent)
|
||||||
currency: "USD",
|
|
||||||
|
|
||||||
Artist: newArtistService(base.New()),
|
|
||||||
Label: newLabelService(base.New()),
|
|
||||||
Master: newMasterService(base.New()),
|
|
||||||
Search: newSearchService(base.New()),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cur, err := currency(o.Currency)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Client{
|
||||||
|
api: base,
|
||||||
|
|
||||||
|
Release: newReleaseService(base.New(), cur),
|
||||||
|
Artist: newArtistService(base.New()),
|
||||||
|
Label: newLabelService(base.New()),
|
||||||
|
Master: newMasterService(base.New()),
|
||||||
|
Search: newSearchService(base.New()),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token sets tokens, it's required for some queries like search
|
// Token sets tokens, it's required for some queries like search
|
||||||
@ -41,23 +58,15 @@ func (c *Client) Token(token string) *Client {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserAgent sets specified user agent
|
// currency validates currency for marketplace data.
|
||||||
// Discogs requires it
|
|
||||||
func (c *Client) UserAgent(useragent string) *Client {
|
|
||||||
c.api.Set("User-Agent", useragent)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCurrency determines currency for marketplace data.
|
|
||||||
// Defaults to the authenticated users currency. Must be one of the following:
|
// Defaults to the authenticated users currency. Must be one of the following:
|
||||||
// USD GBP EUR CAD AUD JPY CHF MXN BRL NZD SEK ZAR
|
// USD GBP EUR CAD AUD JPY CHF MXN BRL NZD SEK ZAR
|
||||||
func (c *Client) Currency(currency string) error {
|
func currency(c string) (string, error) {
|
||||||
switch currency {
|
switch c {
|
||||||
case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR":
|
case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR":
|
||||||
c.currency = currency
|
return c, nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("%v\n", "Invalid currency abbreviation.")
|
return "", fmt.Errorf("%v\n", "Invalid currency abbreviation.")
|
||||||
}
|
}
|
||||||
|
return "USD", nil
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
d := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +http://example.com")
|
d, err := discogs.NewClient(&discogs.Options{
|
||||||
if err := d.Currency("EUR"); err != nil {
|
UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
|
||||||
|
Currency: "EUR",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
release, err := d.Release(9893847)
|
release, err := d.Release.Release(9893847)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
|
21
releases.go
21
releases.go
@ -2,6 +2,8 @@ package discogs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
apirequest "github.com/irlndts/go-apirequest"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Release serves relesase response from discogs
|
// Release serves relesase response from discogs
|
||||||
@ -45,12 +47,25 @@ type ReqRelease struct {
|
|||||||
CurrAbbr string
|
CurrAbbr string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReleaseService ...
|
||||||
|
type ReleaseService struct {
|
||||||
|
api *apirequest.API
|
||||||
|
currency string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newReleaseService(api *apirequest.API, currency string) *ReleaseService {
|
||||||
|
return &ReleaseService{
|
||||||
|
api: api.Path("releases/"),
|
||||||
|
currency: currency,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Release returns release by release's ID
|
// Release returns release by release's ID
|
||||||
func (c *Client) Release(releaseID int) (*Release, error) {
|
func (s *ReleaseService) Release(releaseID int) (*Release, error) {
|
||||||
release := new(Release)
|
release := new(Release)
|
||||||
apiError := new(APIError)
|
apiError := new(APIError)
|
||||||
|
|
||||||
req := &ReqRelease{CurrAbbr: c.currency}
|
req := &ReqRelease{CurrAbbr: s.currency}
|
||||||
_, err := c.api.New().Get("releases/"+strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError)
|
_, err := s.api.New().Get(strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError)
|
||||||
return release, relevantError(err, *apiError)
|
return release, relevantError(err, *apiError)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user