From dd4fa14f617c4a6b7c3d09c93c55117115dae51b Mon Sep 17 00:00:00 2001 From: irlndts Date: Tue, 25 Apr 2017 19:39:32 +0300 Subject: [PATCH] DSCGS-4 enough for today --- README.md | 5 +-- discogs.go | 47 +++++++++++++++----- examples/discogs_example.go | 10 ++++- labels_test.go | 2 +- releases.go | 89 +++++++++++++++++-------------------- releases_test.go | 2 +- search.go | 28 ++++++------ 7 files changed, 104 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index 24ac8e1..d6764b4 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,9 @@ client := discogs.NewClient("TestDiscogsClient/0.0.1 +example.com", "sometoken") #### Releases ```go - params := &discogs.ReleaseParams{Release_id: "8138518"} - release, _, err := client.Release.Release(params) + release, err := client.Release.Release(9893847) - 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 diff --git a/discogs.go b/discogs.go index 83e682c..080ce42 100644 --- a/discogs.go +++ b/discogs.go @@ -12,22 +12,47 @@ const ( // Client is a Discogs client for making Discogs API requests. type Client struct { - api *apirequest.API - Release *ReleaseService - Master *MasterService - Artist *ArtistService - Label *LabelService - Search *SearchService + api *apirequest.API + currency string + //Release *ReleaseService + 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) return &Client{ - Artist: newArtistService(base.New()), - Label: newLabelService(base.New()), - Master: newMasterService(base.New()), - Release: newReleaseService(base.New()), - Search: newSearchService(base.New()), + api: base, + currency: "USD", + + Artist: newArtistService(base.New()), + Label: newLabelService(base.New()), + Master: newMasterService(base.New()), + //Release: newReleaseService(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 +} diff --git a/examples/discogs_example.go b/examples/discogs_example.go index e26fb5b..94e079b 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -9,7 +9,7 @@ import ( func main() { 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) if err != nil { @@ -18,6 +18,12 @@ func main() { } 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) + } + } diff --git a/labels_test.go b/labels_test.go index 4d2a466..a50413c 100644 --- a/labels_test.go +++ b/labels_test.go @@ -16,7 +16,7 @@ func TestLabelService_Label(t *testing.T) { } func TestLabelService_Releases(t *testing.T) { - expectedId := "Cha Cha Twist" + expectedId := "Ghetto Sol" d := NewClient(testUserAgent, testToken) label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"}) diff --git a/releases.go b/releases.go index 75985f7..7af7eb1 100644 --- a/releases.go +++ b/releases.go @@ -1,61 +1,56 @@ package discogs import ( - "github.com/irlndts/go-apirequest" - "net/http" + "strconv" ) -type ReleaseService struct { - api *apirequest.API -} - -type ReleaseParams struct { - Release_id string -} - +// Release serves relesase response from discogs type Release struct { - Title string `json:"title"` - Id int `json:"id"` - Artists []ArtistSource `json:"artists"` - Data_quality string `json:"data_quality"` - Thumb string `json:"thumb"` - Community Community `json:"community"` - Companies []Company `json:"companies"` - Country string `json:"country"` - Date_added string `json:"date_added"` - Date_changed string `json:"date_changed"` - Estimated_weight int `json:"estimated_weight"` - Extraartists []ArtistSource `json:"extraartists"` - Format_quantity int `json:"format_quantity"` - Formats []Format `json:"formats"` - Genres []string `json:"genres"` - Identifiers []Identifier `json:"identifiers"` - Images []Image `json:"images"` - Labels []LabelSource `json:"labels"` - Master_id int `json:"master_id"` - Master_url string `json:"master_url"` - Notes string `josn:"notes"` - Released string `json:"released"` - Released_formatted string `json:"released_formatted"` - Resource_url string `json:"resource_url"` - Status string `json:"status"` - Styles []string `json:"styles"` - Tracklist []Track `json:"tracklist"` - Uri string `json:"uri"` - Videos []Video `json:"videos"` - Year int `json:"year"` + Title string `json:"title"` + ID int `json:"id"` + Artists []ArtistSource `json:"artists"` + DataQuality string `json:"data_quality"` + Thumb string `json:"thumb"` + Community Community `json:"community"` + Companies []Company `json:"companies"` + Country string `json:"country"` + DateAdded string `json:"date_added"` + DateChanged string `json:"date_changed"` + EstimatedWeight int `json:"estimated_weight"` + ExtraArtists []ArtistSource `json:"extraartists"` + FormatQuantity int `json:"format_quantity"` + Formats []Format `json:"formats"` + Genres []string `json:"genres"` + Identifiers []Identifier `json:"identifiers"` + Images []Image `json:"images"` + Labels []LabelSource `json:"labels"` + LowestPrice float64 `json:"lowest_price"` + MasterID int `json:"master_id"` + MasterURL string `json:"master_url"` + Notes string `json:"notes,omitempty"` + NumForSale int `json:"numfor_sale,omitempty"` + Released string `json:"released"` + ReleasedFormatted string `json:"released_formatted"` + ResourceURL string `json:"resource_url"` + // Series + Status string `json:"status"` + Styles []string `json:"styles"` + Tracklist []Track `json:"tracklist"` + URI string `json:"uri"` + Videos []Video `json:"videos"` + Year int `json:"year"` } -func newReleaseService(api *apirequest.API) *ReleaseService { - return &ReleaseService{ - api: api.Path("releases/"), - } +type RequestRelease struct { + Curr_abbr string } -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) apiError := new(APIError) - resp, err := self.api.New().Get(params.Release_id).Receive(release, apiError) - return release, resp, relevantError(err, *apiError) + req := &RequestRelease{Curr_abbr: c.currency} + _, err := c.api.New().Get("releases/"+strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError) + return release, relevantError(err, *apiError) } diff --git a/releases_test.go b/releases_test.go index 1d425ad..ae6ae65 100644 --- a/releases_test.go +++ b/releases_test.go @@ -9,7 +9,7 @@ func TestReleaseService_Release(t *testing.T) { expectedTitle := "Elephant Riddim" d := NewClient(testUserAgent, testToken) - release, _, err := d.Release.Release(&ReleaseParams{Release_id: "8138518"}) + release, err := d.Release.Release(8138518) check(t, err) assert(t, release.Title == expectedTitle, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedTitle, release.Title)) diff --git a/search.go b/search.go index 574be72..6485540 100644 --- a/search.go +++ b/search.go @@ -44,20 +44,20 @@ type Search struct { // Result describes a part of search result type Result struct { - Style []string `json:"style,omitempty"` - Thumb string `json:"thumb,omitempty"` - Title string `json:"title,omitempty"` - Country string `json:"country,omitempty"` - Format []string `json:"format,omitempty"` - Uri string `json:"uri,omitempty"` - Community Community `json:"community,omitempty"` - Label []string `json:"label,omitempty"` - Catno string `json:"catno,omitempty"` - Year string `json:"year,omitempty"` - Genre []string `json:"genre,omitempty"` - Resource_url string `json:"resource_url,omitempty"` - Type string `json:"type,omitempty"` - Id int `json:"id,omitempty"` + Style []string `json:"style,omitempty"` + Thumb string `json:"thumb,omitempty"` + Title string `json:"title,omitempty"` + Country string `json:"country,omitempty"` + Format []string `json:"format,omitempty"` + URI string `json:"uri,omitempty"` + Community Community `json:"community,omitempty"` + Label []string `json:"label,omitempty"` + Catno string `json:"catno,omitempty"` + Year string `json:"year,omitempty"` + Genre []string `json:"genre,omitempty"` + ResourceURL string `json:"resource_url,omitempty"` + Type string `json:"type,omitempty"` + ID int `json:"id,omitempty"` } func newSearchService(api *apirequest.API) *SearchService {