DSCGS-4 Logic improved

This commit is contained in:
irlndts 2017-04-26 15:57:03 +03:00
parent dd4fa14f61
commit 44db789a6b
3 changed files with 22 additions and 22 deletions

View File

@ -1,6 +1,7 @@
package discogs package discogs
import ( import (
"fmt"
"net/http" "net/http"
"github.com/irlndts/go-apirequest" "github.com/irlndts/go-apirequest"
@ -14,16 +15,15 @@ const (
type Client struct { type Client struct {
api *apirequest.API api *apirequest.API
currency string currency string
//Release *ReleaseService Master *MasterService
Master *MasterService Artist *ArtistService
Artist *ArtistService Label *LabelService
Label *LabelService Search *SearchService
Search *SearchService
} }
// NewClient returns a new Client. // NewClient returns a new Client.
func NewClient(useragent, token string) *Client { func NewClient() *Client {
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI).Add("User-Agent", useragent).Add("Authorization", "Discogs token="+token) base := apirequest.New().Client(&http.Client{}).Base(discogsAPI)
return &Client{ return &Client{
api: base, api: base,
currency: "USD", currency: "USD",
@ -31,7 +31,6 @@ func NewClient(useragent, token string) *Client {
Artist: newArtistService(base.New()), Artist: newArtistService(base.New()),
Label: newLabelService(base.New()), Label: newLabelService(base.New()),
Master: newMasterService(base.New()), Master: newMasterService(base.New()),
//Release: newReleaseService(base.New()),
Search: newSearchService(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: // 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 (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 return nil
} }

View File

@ -9,6 +9,7 @@ type APIError struct {
Message string `json:"message"` Message string `json:"message"`
} }
// Error ...
func (e APIError) Error() string { func (e APIError) Error() string {
if e.Message != "" { if e.Message != "" {
return fmt.Sprintf("discogs: %v", e.Message) return fmt.Sprintf("discogs: %v", e.Message)

View File

@ -7,23 +7,17 @@ import (
) )
func main() { 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} if err := d.Currency("EUR"); err != nil {
search, _, err := d.Search.Search(request)
if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
for _, r := range search.Results { release, err := d.Release(9893847)
fmt.Printf("%+v\n", r) if err != nil {
//fmt.Println(r.Id, r.Title) fmt.Println(err)
return
release, _ := d.Release(r.ID)
fmt.Printf("%+v\n", release)
} }
fmt.Printf("%+v\n", release)
} }