diff --git a/discogs.go b/discogs.go index fef0762..1559e57 100644 --- a/discogs.go +++ b/discogs.go @@ -11,28 +11,45 @@ const ( discogsAPI = "https://api.discogs.com/" ) +type Options struct { + Currency string + UserAgent string +} + // Client is a Discogs client for making Discogs API requests. type Client struct { api *apirequest.API currency string - Master *MasterService - Artist *ArtistService - Label *LabelService - Search *SearchService + + // services + Release *ReleaseService + Master *MasterService + Artist *ArtistService + Label *LabelService + Search *SearchService } // NewClient returns a new Client. -func NewClient() *Client { +func NewClient(o *Options) (*Client, error) { base := apirequest.New().Client(&http.Client{}).Base(discogsAPI) - return &Client{ - api: base, - currency: "USD", - - Artist: newArtistService(base.New()), - Label: newLabelService(base.New()), - Master: newMasterService(base.New()), - Search: newSearchService(base.New()), + if o.UserAgent != "" { + base.Set("User-Agent", o.UserAgent) } + + 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 @@ -41,23 +58,15 @@ func (c *Client) Token(token string) *Client { 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. +// currency validates 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 { - switch currency { +func currency(c string) (string, error) { + switch c { case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR": - c.currency = currency + return c, nil default: - return fmt.Errorf("%v\n", "Invalid currency abbreviation.") + return "", fmt.Errorf("%v\n", "Invalid currency abbreviation.") } - - return nil + return "USD", nil } diff --git a/examples/discogs_example.go b/examples/discogs_example.go index 1d334d1..4f87851 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -7,13 +7,16 @@ import ( ) func main() { - d := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +http://example.com") - if err := d.Currency("EUR"); err != nil { + d, err := discogs.NewClient(&discogs.Options{ + UserAgent: "TestDiscogsClient/0.0.1 +http://example.com", + Currency: "EUR", + }) + if err != nil { fmt.Println(err) return } - release, err := d.Release(9893847) + release, err := d.Release.Release(9893847) if err != nil { fmt.Println(err) return diff --git a/releases.go b/releases.go index fcb4be5..13ef585 100644 --- a/releases.go +++ b/releases.go @@ -2,6 +2,8 @@ package discogs import ( "strconv" + + apirequest "github.com/irlndts/go-apirequest" ) // Release serves relesase response from discogs @@ -45,12 +47,25 @@ type ReqRelease struct { 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 -func (c *Client) Release(releaseID int) (*Release, error) { +func (s *ReleaseService) Release(releaseID int) (*Release, error) { release := new(Release) apiError := new(APIError) - req := &ReqRelease{CurrAbbr: c.currency} - _, err := c.api.New().Get("releases/"+strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError) + req := &ReqRelease{CurrAbbr: s.currency} + _, err := s.api.New().Get(strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError) return release, relevantError(err, *apiError) }