Merge pull request #5 from irlndts/DSCG-4

DSCGS-4
This commit is contained in:
Artem Piskun 2017-04-26 16:02:07 +03:00 committed by GitHub
commit 86244f6665
8 changed files with 111 additions and 86 deletions

View File

@ -41,10 +41,9 @@ client := discogs.NewClient("TestDiscogsClient/0.0.1 +example.com", "sometoken")
#### Releases #### Releases
```go ```go
params := &discogs.ReleaseParams{Release_id: "8138518"} release, err := client..Release(9893847)
release, _, err := client.Release.Release(params)
fmt.Println(fmt.Println(release.Artists[0].Name, " - ", release.Title)) // St. Petersburg Ska-Jazz Review - Elephant Riddim fmt.Println(release.Artists[0].Name, " - ", release.Title) // St. Petersburg Ska-Jazz Review - Elephant Riddim
``` ```
#### Artists #### Artists

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"
@ -12,22 +13,51 @@ const (
// Client is a Discogs client for making Discogs API requests. // Client is a Discogs client for making Discogs API requests.
type Client struct { type Client struct {
api *apirequest.API api *apirequest.API
Release *ReleaseService currency string
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{
Artist: newArtistService(base.New()), api: base,
Label: newLabelService(base.New()), currency: "USD",
Master: newMasterService(base.New()),
Release: newReleaseService(base.New()), Artist: newArtistService(base.New()),
Search: newSearchService(base.New()), Label: newLabelService(base.New()),
Master: newMasterService(base.New()),
Search: newSearchService(base.New()),
} }
} }
// 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 {
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"` 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,17 +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 Reggaenauts - River Rock / Thursday Kick-off", 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.Println(r.Id, r.Title) if err != nil {
fmt.Println(err)
return
} }
fmt.Printf("%+v\n", release)
} }

View File

@ -16,7 +16,7 @@ func TestLabelService_Label(t *testing.T) {
} }
func TestLabelService_Releases(t *testing.T) { func TestLabelService_Releases(t *testing.T) {
expectedId := "Cha Cha Twist" expectedId := "Ghetto Sol"
d := NewClient(testUserAgent, testToken) d := NewClient(testUserAgent, testToken)
label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"}) label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"})

View File

@ -1,61 +1,56 @@
package discogs package discogs
import ( import (
"github.com/irlndts/go-apirequest" "strconv"
"net/http"
) )
type ReleaseService struct { // Release serves relesase response from discogs
api *apirequest.API
}
type ReleaseParams struct {
Release_id string
}
type Release struct { type Release struct {
Title string `json:"title"` Title string `json:"title"`
Id int `json:"id"` ID int `json:"id"`
Artists []ArtistSource `json:"artists"` Artists []ArtistSource `json:"artists"`
Data_quality string `json:"data_quality"` DataQuality string `json:"data_quality"`
Thumb string `json:"thumb"` Thumb string `json:"thumb"`
Community Community `json:"community"` Community Community `json:"community"`
Companies []Company `json:"companies"` Companies []Company `json:"companies"`
Country string `json:"country"` Country string `json:"country"`
Date_added string `json:"date_added"` DateAdded string `json:"date_added"`
Date_changed string `json:"date_changed"` DateChanged string `json:"date_changed"`
Estimated_weight int `json:"estimated_weight"` EstimatedWeight int `json:"estimated_weight"`
Extraartists []ArtistSource `json:"extraartists"` ExtraArtists []ArtistSource `json:"extraartists"`
Format_quantity int `json:"format_quantity"` FormatQuantity int `json:"format_quantity"`
Formats []Format `json:"formats"` Formats []Format `json:"formats"`
Genres []string `json:"genres"` Genres []string `json:"genres"`
Identifiers []Identifier `json:"identifiers"` Identifiers []Identifier `json:"identifiers"`
Images []Image `json:"images"` Images []Image `json:"images"`
Labels []LabelSource `json:"labels"` Labels []LabelSource `json:"labels"`
Master_id int `json:"master_id"` LowestPrice float64 `json:"lowest_price"`
Master_url string `json:"master_url"` MasterID int `json:"master_id"`
Notes string `josn:"notes"` MasterURL string `json:"master_url"`
Released string `json:"released"` Notes string `json:"notes,omitempty"`
Released_formatted string `json:"released_formatted"` NumForSale int `json:"numfor_sale,omitempty"`
Resource_url string `json:"resource_url"` Released string `json:"released"`
Status string `json:"status"` ReleasedFormatted string `json:"released_formatted"`
Styles []string `json:"styles"` ResourceURL string `json:"resource_url"`
Tracklist []Track `json:"tracklist"` // Series
Uri string `json:"uri"` Status string `json:"status"`
Videos []Video `json:"videos"` Styles []string `json:"styles"`
Year int `json:"year"` Tracklist []Track `json:"tracklist"`
URI string `json:"uri"`
Videos []Video `json:"videos"`
Year int `json:"year"`
} }
func newReleaseService(api *apirequest.API) *ReleaseService { type RequestRelease struct {
return &ReleaseService{ Curr_abbr string
api: api.Path("releases/"),
}
} }
func (self *ReleaseService) Release(params *ReleaseParams) (*Release, *http.Response, error) { // Release returns release by release's ID
func (c *Client) Release(releaseID int) (*Release, error) {
release := new(Release) release := new(Release)
apiError := new(APIError) apiError := new(APIError)
resp, err := self.api.New().Get(params.Release_id).Receive(release, apiError) req := &RequestRelease{Curr_abbr: c.currency}
return release, resp, relevantError(err, *apiError) _, err := c.api.New().Get("releases/"+strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError)
return release, relevantError(err, *apiError)
} }

View File

@ -9,7 +9,7 @@ func TestReleaseService_Release(t *testing.T) {
expectedTitle := "Elephant Riddim" expectedTitle := "Elephant Riddim"
d := NewClient(testUserAgent, testToken) d := NewClient(testUserAgent, testToken)
release, _, err := d.Release.Release(&ReleaseParams{Release_id: "8138518"}) release, err := d.Release.Release(8138518)
check(t, err) check(t, err)
assert(t, release.Title == expectedTitle, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedTitle, release.Title)) assert(t, release.Title == expectedTitle, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedTitle, release.Title))

View File

@ -44,20 +44,20 @@ type Search struct {
// Result describes a part of search result // Result describes a part of search result
type Result struct { type Result struct {
Style []string `json:"style,omitempty"` Style []string `json:"style,omitempty"`
Thumb string `json:"thumb,omitempty"` Thumb string `json:"thumb,omitempty"`
Title string `json:"title,omitempty"` Title string `json:"title,omitempty"`
Country string `json:"country,omitempty"` Country string `json:"country,omitempty"`
Format []string `json:"format,omitempty"` Format []string `json:"format,omitempty"`
Uri string `json:"uri,omitempty"` URI string `json:"uri,omitempty"`
Community Community `json:"community,omitempty"` Community Community `json:"community,omitempty"`
Label []string `json:"label,omitempty"` Label []string `json:"label,omitempty"`
Catno string `json:"catno,omitempty"` Catno string `json:"catno,omitempty"`
Year string `json:"year,omitempty"` Year string `json:"year,omitempty"`
Genre []string `json:"genre,omitempty"` Genre []string `json:"genre,omitempty"`
Resource_url string `json:"resource_url,omitempty"` ResourceURL string `json:"resource_url,omitempty"`
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Id int `json:"id,omitempty"` ID int `json:"id,omitempty"`
} }
func newSearchService(api *apirequest.API) *SearchService { func newSearchService(api *apirequest.API) *SearchService {