DSCGS-4 enough for today

This commit is contained in:
irlndts 2017-04-25 19:39:32 +03:00
parent 26dbacfe44
commit dd4fa14f61
7 changed files with 104 additions and 79 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.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

@ -13,7 +13,8 @@ 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
//Release *ReleaseService
Master *MasterService Master *MasterService
Artist *ArtistService Artist *ArtistService
Label *LabelService Label *LabelService
@ -24,10 +25,34 @@ type Client struct {
func NewClient(useragent, token string) *Client { func NewClient(useragent, token string) *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).Add("User-Agent", useragent).Add("Authorization", "Discogs token="+token)
return &Client{ return &Client{
api: base,
currency: "USD",
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()), //Release: newReleaseService(base.New()),
Search: newSearchService(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 {
c.currency = currency
return nil
}

View File

@ -9,7 +9,7 @@ import (
func main() { func main() {
d := discogs.NewClient("TestDiscogsClient/0.0.1 +http://irlndts.moscow", "") d := discogs.NewClient("TestDiscogsClient/0.0.1 +http://irlndts.moscow", "")
request := &discogs.SearchRequest{Q: "The Reggaenauts - River Rock / Thursday Kick-off", Page: 0, Per_page: 1} request := &discogs.SearchRequest{Q: "The Swindlers - Dig Out Alive!", Page: 0, Per_page: 1}
search, _, err := d.Search.Search(request) search, _, err := d.Search.Search(request)
if err != nil { if err != nil {
@ -18,6 +18,12 @@ func main() {
} }
for _, r := range search.Results { for _, r := range search.Results {
fmt.Println(r.Id, r.Title) fmt.Printf("%+v\n", r)
//fmt.Println(r.Id, r.Title)
release, _ := d.Release(r.ID)
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"`
Notes string `json:"notes,omitempty"`
NumForSale int `json:"numfor_sale,omitempty"`
Released string `json:"released"` Released string `json:"released"`
Released_formatted string `json:"released_formatted"` ReleasedFormatted string `json:"released_formatted"`
Resource_url string `json:"resource_url"` ResourceURL string `json:"resource_url"`
// Series
Status string `json:"status"` Status string `json:"status"`
Styles []string `json:"styles"` Styles []string `json:"styles"`
Tracklist []Track `json:"tracklist"` Tracklist []Track `json:"tracklist"`
Uri string `json:"uri"` URI string `json:"uri"`
Videos []Video `json:"videos"` Videos []Video `json:"videos"`
Year int `json:"year"` 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

@ -49,15 +49,15 @@ type Result struct {
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 {