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
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a Discogs client for making Discogs API requests.
|
|
|
|
type Client struct {
|
2017-04-25 16:39:32 +00:00
|
|
|
api *apirequest.API
|
|
|
|
currency string
|
2017-04-26 12:57:03 +00:00
|
|
|
Master *MasterService
|
|
|
|
Artist *ArtistService
|
|
|
|
Label *LabelService
|
|
|
|
Search *SearchService
|
2016-03-04 16:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new Client.
|
2017-04-26 12:57:03 +00:00
|
|
|
func NewClient() *Client {
|
|
|
|
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI)
|
2016-03-04 16:08:02 +00:00
|
|
|
return &Client{
|
2017-04-25 16:39:32 +00:00
|
|
|
api: base,
|
|
|
|
currency: "USD",
|
|
|
|
|
|
|
|
Artist: newArtistService(base.New()),
|
|
|
|
Label: newLabelService(base.New()),
|
|
|
|
Master: newMasterService(base.New()),
|
|
|
|
Search: newSearchService(base.New()),
|
2016-03-04 16:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 16:39:32 +00:00
|
|
|
|
|
|
|
// Token sets tokens, it's required for some queries like search
|
|
|
|
func (c *Client) Token(token string) *Client {
|
|
|
|
c.api.Set("Authorization", "Discogs token="+token)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserAgent sets specified user agent
|
|
|
|
// 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:
|
|
|
|
// USD GBP EUR CAD AUD JPY CHF MXN BRL NZD SEK ZAR
|
|
|
|
func (c *Client) Currency(currency string) error {
|
2017-04-26 12:57:03 +00:00
|
|
|
switch currency {
|
|
|
|
case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR":
|
|
|
|
c.currency = currency
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%v\n", "Invalid currency abbreviation.")
|
|
|
|
}
|
|
|
|
|
2017-04-25 16:39:32 +00:00
|
|
|
return nil
|
|
|
|
}
|