2016-03-04 16:08:02 +00:00
|
|
|
package discogs
|
|
|
|
|
|
|
|
import (
|
2017-04-26 12:57:03 +00:00
|
|
|
"fmt"
|
2016-03-04 16:08:02 +00:00
|
|
|
"net/http"
|
2017-02-13 16:40:27 +00:00
|
|
|
|
|
|
|
"github.com/irlndts/go-apirequest"
|
2016-03-04 16:08:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-14 19:01:50 +00:00
|
|
|
discogsAPI = "https://api.discogs.com/"
|
2016-03-04 16:08:02 +00:00
|
|
|
)
|
|
|
|
|
2018-02-20 15:13:04 +00:00
|
|
|
type Options struct {
|
2018-02-20 17:00:24 +00:00
|
|
|
URL string
|
2018-02-20 15:13:04 +00:00
|
|
|
Currency string
|
|
|
|
UserAgent string
|
2018-02-20 15:16:34 +00:00
|
|
|
Token string
|
2018-02-20 15:13:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 16:08:02 +00:00
|
|
|
// Client is a Discogs client for making Discogs API requests.
|
|
|
|
type Client struct {
|
2018-02-20 15:13:04 +00:00
|
|
|
Release *ReleaseService
|
|
|
|
Master *MasterService
|
|
|
|
Artist *ArtistService
|
|
|
|
Label *LabelService
|
|
|
|
Search *SearchService
|
2016-03-04 16:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new Client.
|
2018-02-20 15:13:04 +00:00
|
|
|
func NewClient(o *Options) (*Client, error) {
|
2018-02-20 16:26:00 +00:00
|
|
|
header := &http.Header{}
|
2017-04-26 12:57:03 +00:00
|
|
|
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI)
|
2018-02-20 15:13:04 +00:00
|
|
|
if o.UserAgent != "" {
|
|
|
|
base.Set("User-Agent", o.UserAgent)
|
2018-02-20 16:26:00 +00:00
|
|
|
header.Add("User-Agent", o.UserAgent)
|
2018-02-20 15:13:04 +00:00
|
|
|
}
|
2017-04-25 16:39:32 +00:00
|
|
|
|
2018-02-20 15:13:04 +00:00
|
|
|
cur, err := currency(o.Currency)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-03-04 16:08:02 +00:00
|
|
|
}
|
2018-02-20 15:13:04 +00:00
|
|
|
|
2018-02-20 15:16:34 +00:00
|
|
|
// set token, it's required for some queries like search
|
|
|
|
if o.Token != "" {
|
|
|
|
base.Set("Authorization", "Discogs token="+o.Token)
|
2018-02-20 16:26:00 +00:00
|
|
|
header.Add("Authorization", "Discogs token="+o.Token)
|
2018-02-20 15:16:34 +00:00
|
|
|
}
|
2018-02-20 15:13:04 +00:00
|
|
|
|
2018-02-20 15:16:34 +00:00
|
|
|
return &Client{
|
2018-02-20 17:00:24 +00:00
|
|
|
Release: newReleaseService(o.URL+"/releases/", header, cur),
|
2018-02-20 15:13:04 +00:00
|
|
|
Artist: newArtistService(base.New()),
|
|
|
|
Label: newLabelService(base.New()),
|
|
|
|
Master: newMasterService(base.New()),
|
|
|
|
Search: newSearchService(base.New()),
|
|
|
|
}, nil
|
2016-03-04 16:08:02 +00:00
|
|
|
}
|
2017-04-25 16:39:32 +00:00
|
|
|
|
2018-02-20 15:13:04 +00:00
|
|
|
// currency validates currency for marketplace data.
|
2017-04-25 16:39:32 +00:00
|
|
|
// Defaults to the authenticated users currency. Must be one of the following:
|
|
|
|
// USD GBP EUR CAD AUD JPY CHF MXN BRL NZD SEK ZAR
|
2018-02-20 15:13:04 +00:00
|
|
|
func currency(c string) (string, error) {
|
|
|
|
switch c {
|
2017-04-26 12:57:03 +00:00
|
|
|
case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR":
|
2018-02-20 15:13:04 +00:00
|
|
|
return c, nil
|
2018-02-20 17:00:24 +00:00
|
|
|
case "":
|
|
|
|
return "USD", nil
|
2017-04-26 12:57:03 +00:00
|
|
|
default:
|
2018-02-20 15:13:04 +00:00
|
|
|
return "", fmt.Errorf("%v\n", "Invalid currency abbreviation.")
|
2017-04-26 12:57:03 +00:00
|
|
|
}
|
2017-04-25 16:39:32 +00:00
|
|
|
}
|