From 44db789a6bda0e38efc60642a57a31fc29adafa1 Mon Sep 17 00:00:00 2001 From: irlndts Date: Wed, 26 Apr 2017 15:57:03 +0300 Subject: [PATCH] DSCGS-4 Logic improved --- discogs.go | 23 ++++++++++++++--------- errors.go | 1 + examples/discogs_example.go | 20 +++++++------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/discogs.go b/discogs.go index 080ce42..fef0762 100644 --- a/discogs.go +++ b/discogs.go @@ -1,6 +1,7 @@ package discogs import ( + "fmt" "net/http" "github.com/irlndts/go-apirequest" @@ -14,16 +15,15 @@ const ( type Client struct { api *apirequest.API currency string - //Release *ReleaseService - Master *MasterService - Artist *ArtistService - Label *LabelService - Search *SearchService + Master *MasterService + Artist *ArtistService + Label *LabelService + Search *SearchService } // NewClient returns a new Client. -func NewClient(useragent, token string) *Client { - base := apirequest.New().Client(&http.Client{}).Base(discogsAPI).Add("User-Agent", useragent).Add("Authorization", "Discogs token="+token) +func NewClient() *Client { + base := apirequest.New().Client(&http.Client{}).Base(discogsAPI) return &Client{ api: base, currency: "USD", @@ -31,7 +31,6 @@ func NewClient(useragent, token string) *Client { Artist: newArtistService(base.New()), Label: newLabelService(base.New()), Master: newMasterService(base.New()), - //Release: newReleaseService(base.New()), Search: newSearchService(base.New()), } } @@ -53,6 +52,12 @@ func (c *Client) UserAgent(useragent string) *Client { // 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 { - c.currency = currency + 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.") + } + return nil } diff --git a/errors.go b/errors.go index 5c4ddd3..8ddbab4 100644 --- a/errors.go +++ b/errors.go @@ -9,6 +9,7 @@ type APIError struct { Message string `json:"message"` } +// Error ... func (e APIError) Error() string { if e.Message != "" { return fmt.Sprintf("discogs: %v", e.Message) diff --git a/examples/discogs_example.go b/examples/discogs_example.go index 94e079b..1e95e9b 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -7,23 +7,17 @@ import ( ) func main() { - d := discogs.NewClient("TestDiscogsClient/0.0.1 +http://irlndts.moscow", "") + d := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +http://irlndts.moscow").Token() - request := &discogs.SearchRequest{Q: "The Swindlers - Dig Out Alive!", Page: 0, Per_page: 1} - search, _, err := d.Search.Search(request) - - if err != nil { + if err := d.Currency("EUR"); err != nil { fmt.Println(err) return } - for _, r := range search.Results { - fmt.Printf("%+v\n", r) - //fmt.Println(r.Id, r.Title) - - release, _ := d.Release(r.ID) - fmt.Printf("%+v\n", release) - + release, err := d.Release(9893847) + if err != nil { + fmt.Println(err) + return } - + fmt.Printf("%+v\n", release) }