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
import (
"fmt"
"net/http"
"github.com/irlndts/go-apirequest"
@ -14,7 +15,6 @@ const (
type Client struct {
api *apirequest.API
currency string
//Release *ReleaseService
Master *MasterService
Artist *ArtistService
Label *LabelService
@ -22,8 +22,8 @@ type Client struct {
}
// 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 {
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
}

View File

@ -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)

View File

@ -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)
release, err := d.Release(9893847)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", release)
}
}