From d0f617733ac85ba8d14c7b6b88e22195b238e975 Mon Sep 17 00:00:00 2001 From: irlndts Date: Tue, 20 Feb 2018 18:13:04 +0300 Subject: [PATCH 1/7] Some Improvements --- discogs.go | 63 +++++++++++++++++++++---------------- examples/discogs_example.go | 9 ++++-- releases.go | 21 +++++++++++-- 3 files changed, 60 insertions(+), 33 deletions(-) diff --git a/discogs.go b/discogs.go index fef0762..1559e57 100644 --- a/discogs.go +++ b/discogs.go @@ -11,28 +11,45 @@ const ( discogsAPI = "https://api.discogs.com/" ) +type Options struct { + Currency string + UserAgent string +} + // Client is a Discogs client for making Discogs API requests. type Client struct { api *apirequest.API currency string - Master *MasterService - Artist *ArtistService - Label *LabelService - Search *SearchService + + // services + Release *ReleaseService + Master *MasterService + Artist *ArtistService + Label *LabelService + Search *SearchService } // NewClient returns a new Client. -func NewClient() *Client { +func NewClient(o *Options) (*Client, error) { base := apirequest.New().Client(&http.Client{}).Base(discogsAPI) - return &Client{ - api: base, - currency: "USD", - - Artist: newArtistService(base.New()), - Label: newLabelService(base.New()), - Master: newMasterService(base.New()), - Search: newSearchService(base.New()), + if o.UserAgent != "" { + base.Set("User-Agent", o.UserAgent) } + + cur, err := currency(o.Currency) + if err != nil { + return nil, err + } + + return &Client{ + api: base, + + Release: newReleaseService(base.New(), cur), + Artist: newArtistService(base.New()), + Label: newLabelService(base.New()), + Master: newMasterService(base.New()), + Search: newSearchService(base.New()), + }, nil } // Token sets tokens, it's required for some queries like search @@ -41,23 +58,15 @@ func (c *Client) Token(token string) *Client { 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. +// currency validates 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 { +func currency(c string) (string, error) { + switch c { case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR": - c.currency = currency + return c, nil default: - return fmt.Errorf("%v\n", "Invalid currency abbreviation.") + return "", fmt.Errorf("%v\n", "Invalid currency abbreviation.") } - - return nil + return "USD", nil } diff --git a/examples/discogs_example.go b/examples/discogs_example.go index 1d334d1..4f87851 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -7,13 +7,16 @@ import ( ) func main() { - d := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +http://example.com") - if err := d.Currency("EUR"); err != nil { + d, err := discogs.NewClient(&discogs.Options{ + UserAgent: "TestDiscogsClient/0.0.1 +http://example.com", + Currency: "EUR", + }) + if err != nil { fmt.Println(err) return } - release, err := d.Release(9893847) + release, err := d.Release.Release(9893847) if err != nil { fmt.Println(err) return diff --git a/releases.go b/releases.go index fcb4be5..13ef585 100644 --- a/releases.go +++ b/releases.go @@ -2,6 +2,8 @@ package discogs import ( "strconv" + + apirequest "github.com/irlndts/go-apirequest" ) // Release serves relesase response from discogs @@ -45,12 +47,25 @@ type ReqRelease struct { CurrAbbr string } +// ReleaseService ... +type ReleaseService struct { + api *apirequest.API + currency string +} + +func newReleaseService(api *apirequest.API, currency string) *ReleaseService { + return &ReleaseService{ + api: api.Path("releases/"), + currency: currency, + } +} + // Release returns release by release's ID -func (c *Client) Release(releaseID int) (*Release, error) { +func (s *ReleaseService) Release(releaseID int) (*Release, error) { release := new(Release) apiError := new(APIError) - req := &ReqRelease{CurrAbbr: c.currency} - _, err := c.api.New().Get("releases/"+strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError) + req := &ReqRelease{CurrAbbr: s.currency} + _, err := s.api.New().Get(strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError) return release, relevantError(err, *apiError) } From 916e1f0bd6a36aa52f24b611364a7ea9efc964d2 Mon Sep 17 00:00:00 2001 From: irlndts Date: Tue, 20 Feb 2018 18:16:34 +0300 Subject: [PATCH 2/7] Improvement token --- discogs.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/discogs.go b/discogs.go index 1559e57..0564b14 100644 --- a/discogs.go +++ b/discogs.go @@ -14,14 +14,11 @@ const ( type Options struct { Currency string UserAgent string + Token string } // Client is a Discogs client for making Discogs API requests. type Client struct { - api *apirequest.API - currency string - - // services Release *ReleaseService Master *MasterService Artist *ArtistService @@ -41,9 +38,12 @@ func NewClient(o *Options) (*Client, error) { return nil, err } - return &Client{ - api: base, + // set token, it's required for some queries like search + if o.Token != "" { + base.Set("Authorization", "Discogs token="+o.Token) + } + return &Client{ Release: newReleaseService(base.New(), cur), Artist: newArtistService(base.New()), Label: newLabelService(base.New()), @@ -52,12 +52,6 @@ func NewClient(o *Options) (*Client, error) { }, nil } -// 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 -} - // currency validates 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 From 6af125badd9456fc81fffae7ed13a29d1fcae858 Mon Sep 17 00:00:00 2001 From: irlndts Date: Tue, 20 Feb 2018 19:26:00 +0300 Subject: [PATCH 3/7] Improvements --- discogs.go | 5 +++- examples/discogs_example.go | 3 ++- releases.go | 50 +++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/discogs.go b/discogs.go index 0564b14..d540082 100644 --- a/discogs.go +++ b/discogs.go @@ -28,9 +28,11 @@ type Client struct { // NewClient returns a new Client. func NewClient(o *Options) (*Client, error) { + header := &http.Header{} base := apirequest.New().Client(&http.Client{}).Base(discogsAPI) if o.UserAgent != "" { base.Set("User-Agent", o.UserAgent) + header.Add("User-Agent", o.UserAgent) } cur, err := currency(o.Currency) @@ -41,10 +43,11 @@ func NewClient(o *Options) (*Client, error) { // set token, it's required for some queries like search if o.Token != "" { base.Set("Authorization", "Discogs token="+o.Token) + header.Add("Authorization", "Discogs token="+o.Token) } return &Client{ - Release: newReleaseService(base.New(), cur), + Release: newReleaseService(header, cur), Artist: newArtistService(base.New()), Label: newLabelService(base.New()), Master: newMasterService(base.New()), diff --git a/examples/discogs_example.go b/examples/discogs_example.go index 4f87851..638ca67 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -10,6 +10,7 @@ func main() { d, err := discogs.NewClient(&discogs.Options{ UserAgent: "TestDiscogsClient/0.0.1 +http://example.com", Currency: "EUR", + Token: "", }) if err != nil { fmt.Println(err) @@ -21,5 +22,5 @@ func main() { fmt.Println(err) return } - fmt.Printf("%+v\n", release) + fmt.Printf("%+v\n", release.Title) } diff --git a/releases.go b/releases.go index 13ef585..01789d9 100644 --- a/releases.go +++ b/releases.go @@ -1,9 +1,11 @@ package discogs import ( + "encoding/json" + "io/ioutil" + "net/http" + "net/url" "strconv" - - apirequest "github.com/irlndts/go-apirequest" ) // Release serves relesase response from discogs @@ -49,23 +51,51 @@ type ReqRelease struct { // ReleaseService ... type ReleaseService struct { - api *apirequest.API + header *http.Header currency string } -func newReleaseService(api *apirequest.API, currency string) *ReleaseService { +func newReleaseService(header *http.Header, currency string) *ReleaseService { return &ReleaseService{ - api: api.Path("releases/"), + header: header, currency: currency, } } // Release returns release by release's ID func (s *ReleaseService) Release(releaseID int) (*Release, error) { - release := new(Release) - apiError := new(APIError) + params := url.Values{} + params.Set("CurrAbbr", s.currency) - req := &ReqRelease{CurrAbbr: s.currency} - _, err := s.api.New().Get(strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError) - return release, relevantError(err, *apiError) + var release *Release + if err := s.request(strconv.Itoa(releaseID), params, &release); err != nil { + return nil, err + } + + return release, nil +} + +func (s *ReleaseService) request(path string, params url.Values, resp interface{}) error { + r, err := http.NewRequest("GET", discogsAPI+"releases/"+path+"?"+params.Encode(), nil) + if err != nil { + return err + } + r.Header = *s.header + + client := &http.Client{} + response, err := client.Do(r) + if err != nil { + return err + } + + body, err := ioutil.ReadAll(response.Body) + if err != nil { + return err + } + + if err = json.Unmarshal(body, &resp); err != nil { + return err + } + + return nil } From 7d6af61b782bcd7f706c2cb4b1a7dbfe41ac4016 Mon Sep 17 00:00:00 2001 From: irlndts Date: Tue, 20 Feb 2018 20:00:24 +0300 Subject: [PATCH 4/7] TEST --- artists_test.go | 6 +++--- discogs.go | 6 ++++-- labels_test.go | 6 +++--- masters_test.go | 4 ++-- releases.go | 6 ++++-- releases_test.go | 18 ++++++++++++++++-- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/artists_test.go b/artists_test.go index a31d616..45000b0 100644 --- a/artists_test.go +++ b/artists_test.go @@ -8,17 +8,17 @@ import ( func TestArtistService_Artist(t *testing.T) { expectedId := 1000 - d := NewClient(testUserAgent, testToken) + d, _ := NewClient(&Options{}) artist, _, err := d.Artist.Artist(&ArtistParams{Artist_id: "1000"}) check(t, err) - assert(t, artist.Id == expectedId, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedId, artist.Id)) + assert(t, artist.Id == expectedId, fmt.Sprintf("Release.Title looked for %d, and received %d ", expectedId, artist.Id)) } func TestArtistService_Releases(t *testing.T) { expectedArtist := "Dave Clarke" - d := NewClient(testUserAgent, testToken) + d, _ := NewClient(&Options{}) releases, _, err := d.Artist.Releases(&ArtistParams{Artist_id: "1000", Sort: "year", Sort_order: "desc"}) check(t, err) diff --git a/discogs.go b/discogs.go index d540082..4c3e5bb 100644 --- a/discogs.go +++ b/discogs.go @@ -12,6 +12,7 @@ const ( ) type Options struct { + URL string Currency string UserAgent string Token string @@ -47,7 +48,7 @@ func NewClient(o *Options) (*Client, error) { } return &Client{ - Release: newReleaseService(header, cur), + Release: newReleaseService(o.URL+"/releases/", header, cur), Artist: newArtistService(base.New()), Label: newLabelService(base.New()), Master: newMasterService(base.New()), @@ -62,8 +63,9 @@ func currency(c string) (string, error) { switch c { case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR": return c, nil + case "": + return "USD", nil default: return "", fmt.Errorf("%v\n", "Invalid currency abbreviation.") } - return "USD", nil } diff --git a/labels_test.go b/labels_test.go index a50413c..8084584 100644 --- a/labels_test.go +++ b/labels_test.go @@ -8,17 +8,17 @@ import ( func TestLabelService_Label(t *testing.T) { expectedId := 1000 - d := NewClient(testUserAgent, testToken) + d, _ := NewClient(&Options{}) label, _, err := d.Label.Label(&LabelParams{Label_id: "1000"}) check(t, err) - assert(t, label.Id == expectedId, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedId, label.Id)) + assert(t, label.Id == expectedId, fmt.Sprintf("Release.Title looked for %d, and received %d ", expectedId, label.Id)) } func TestLabelService_Releases(t *testing.T) { expectedId := "Ghetto Sol" - d := NewClient(testUserAgent, testToken) + d, _ := NewClient(&Options{}) label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"}) check(t, err) diff --git a/masters_test.go b/masters_test.go index 5a5a792..37a1c68 100644 --- a/masters_test.go +++ b/masters_test.go @@ -8,7 +8,7 @@ import ( func TestMasterService_Master(t *testing.T) { expectedTitle := "Elephant Riddim" - d := NewClient(testUserAgent, testToken) + d, _ := NewClient(nil) master, _, err := d.Master.Master(&MasterParams{Master_id: "960657"}) check(t, err) @@ -18,7 +18,7 @@ func TestMasterService_Master(t *testing.T) { func TestMasterService_Versions(t *testing.T) { expectedTitle := "Stardiver" - d := NewClient(testUserAgent, testToken) + d, _ := NewClient(nil) versions, _, err := d.Master.Versions(&MasterVersionParams{Master_id: "1000", Page: 1, Per_page: 1}) check(t, err) diff --git a/releases.go b/releases.go index 01789d9..9949701 100644 --- a/releases.go +++ b/releases.go @@ -51,12 +51,14 @@ type ReqRelease struct { // ReleaseService ... type ReleaseService struct { + url string header *http.Header currency string } -func newReleaseService(header *http.Header, currency string) *ReleaseService { +func newReleaseService(url string, header *http.Header, currency string) *ReleaseService { return &ReleaseService{ + url: url, header: header, currency: currency, } @@ -76,7 +78,7 @@ func (s *ReleaseService) Release(releaseID int) (*Release, error) { } func (s *ReleaseService) request(path string, params url.Values, resp interface{}) error { - r, err := http.NewRequest("GET", discogsAPI+"releases/"+path+"?"+params.Encode(), nil) + r, err := http.NewRequest("GET", s.url+path+"?"+params.Encode(), nil) if err != nil { return err } diff --git a/releases_test.go b/releases_test.go index ae6ae65..c9313b6 100644 --- a/releases_test.go +++ b/releases_test.go @@ -2,13 +2,27 @@ package discogs import ( "fmt" + "io" + "net/http" + "net/http/httptest" "testing" ) -func TestReleaseService_Release(t *testing.T) { +func ReleaseServer(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + io.WriteString(w, `{"title":"Elephant Riddim"}`) +} + +func TestReleaseServiceRelease(t *testing.T) { expectedTitle := "Elephant Riddim" - d := NewClient(testUserAgent, testToken) + ts := httptest.NewServer(http.HandlerFunc(ReleaseServer)) + defer ts.Close() + + d, err := NewClient(&Options{URL: ts.URL}) + if err != nil { + t.Fatalf("failed to create client: %s", err) + } release, err := d.Release.Release(8138518) check(t, err) From d1095bc769c76eedaf1f6f1c45588ec153431f5e Mon Sep 17 00:00:00 2001 From: irlndts Date: Mon, 12 Mar 2018 19:30:58 +0300 Subject: [PATCH 5/7] Release improvement --- README.md | 14 +++++++------- artists_test.go | 26 -------------------------- discogs.go | 43 ++++++++++++++++++++++++++++++++++++++----- discogs_test.go | 20 ++++++++++++++++++++ labels_test.go | 26 -------------------------- masters_test.go | 26 -------------------------- releases.go | 34 ++-------------------------------- releases_test.go | 14 +++++++------- 8 files changed, 74 insertions(+), 129 deletions(-) delete mode 100644 artists_test.go delete mode 100644 labels_test.go delete mode 100644 masters_test.go diff --git a/README.md b/README.md index 3f99bfc..0bccd82 100644 --- a/README.md +++ b/README.md @@ -28,17 +28,17 @@ import "github.com/irlndts/go-discogs" Some requests require authentification (as any user). According to [Discogs](https://www.discogs.com/developers/#page:authentication,header:authentication-discogs-auth-flow), to send requests with Discogs Auth, you have two options: sending your credentials in the query string with key and secret parameters or a [token parameter](https://www.discogs.com/settings/developers). This is token way example: ```go -client := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +example.com").Token("Some Token") +client, err := discogs.NewClient(&discogs.Options{ + UserAgent: "Some Name", + Currency: "EUR", // optional, "USD" (default), "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR" are allowed + Token: "Some Token", // optional + URL: "https://api.discogs.com", + }) ``` -Don't forget to set required currency ("USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR" are allowed): -```go -err := client.Currency("EUR"); -``` - #### Releases ```go - release, err := client.Release(9893847) + release, err := client.Release.Release(9893847) fmt.Println(release.Artists[0].Name, " - ", release.Title) // St. Petersburg Ska-Jazz Review - Elephant Riddim ``` diff --git a/artists_test.go b/artists_test.go deleted file mode 100644 index 45000b0..0000000 --- a/artists_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package discogs - -import ( - "fmt" - "testing" -) - -func TestArtistService_Artist(t *testing.T) { - expectedId := 1000 - - d, _ := NewClient(&Options{}) - artist, _, err := d.Artist.Artist(&ArtistParams{Artist_id: "1000"}) - - check(t, err) - assert(t, artist.Id == expectedId, fmt.Sprintf("Release.Title looked for %d, and received %d ", expectedId, artist.Id)) -} - -func TestArtistService_Releases(t *testing.T) { - expectedArtist := "Dave Clarke" - - d, _ := NewClient(&Options{}) - releases, _, err := d.Artist.Releases(&ArtistParams{Artist_id: "1000", Sort: "year", Sort_order: "desc"}) - - check(t, err) - assert(t, releases.Releases[0].Artist == expectedArtist, fmt.Sprintf("Releses.Artist looked for %s, and received %s ", expectedArtist, releases.Releases[0].Artist)) -} diff --git a/discogs.go b/discogs.go index 4c3e5bb..58f1fa9 100644 --- a/discogs.go +++ b/discogs.go @@ -1,8 +1,11 @@ package discogs import ( + "encoding/json" "fmt" + "io/ioutil" "net/http" + "net/url" "github.com/irlndts/go-apirequest" ) @@ -27,15 +30,20 @@ type Client struct { Search *SearchService } +var header *http.Header + // NewClient returns a new Client. func NewClient(o *Options) (*Client, error) { - header := &http.Header{} + header = &http.Header{} base := apirequest.New().Client(&http.Client{}).Base(discogsAPI) - if o.UserAgent != "" { - base.Set("User-Agent", o.UserAgent) - header.Add("User-Agent", o.UserAgent) + + if o == nil || o.UserAgent == "" { + return nil, fmt.Errorf("failed to set user-agent") } + base.Set("User-Agent", o.UserAgent) + header.Add("User-Agent", o.UserAgent) + cur, err := currency(o.Currency) if err != nil { return nil, err @@ -47,8 +55,12 @@ func NewClient(o *Options) (*Client, error) { header.Add("Authorization", "Discogs token="+o.Token) } + if o.URL == "" { + o.URL = discogsAPI + } + return &Client{ - Release: newReleaseService(o.URL+"/releases/", header, cur), + Release: newReleaseService(o.URL+"/releases/", cur), Artist: newArtistService(base.New()), Label: newLabelService(base.New()), Master: newMasterService(base.New()), @@ -69,3 +81,24 @@ func currency(c string) (string, error) { return "", fmt.Errorf("%v\n", "Invalid currency abbreviation.") } } + +func request(path string, params url.Values, resp interface{}) error { + r, err := http.NewRequest("GET", path+"?"+params.Encode(), nil) + if err != nil { + return err + } + r.Header = *header + + client := &http.Client{} + response, err := client.Do(r) + if err != nil { + return err + } + + body, err := ioutil.ReadAll(response.Body) + if err != nil { + return err + } + + return json.Unmarshal(body, &resp) +} diff --git a/discogs_test.go b/discogs_test.go index b58defb..b08d0d0 100644 --- a/discogs_test.go +++ b/discogs_test.go @@ -20,3 +20,23 @@ func assert(t *testing.T, condition bool, assertion string) { t.Errorf("Assertion failed: %v", assertion) } } + +func initDiscogsClient(t *testing.T, options *Options) *Client { + if options == nil { + options = &Options{ + UserAgent: testUserAgent, + Currency: "USD", + } + } + + if options.UserAgent == "" { + options.UserAgent = testUserAgent + } + + client, err := NewClient(options) + if err != nil { + t.Fatalf("failed to create client: %s", err) + } + + return client +} diff --git a/labels_test.go b/labels_test.go deleted file mode 100644 index 8084584..0000000 --- a/labels_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package discogs - -import ( - "fmt" - "testing" -) - -func TestLabelService_Label(t *testing.T) { - expectedId := 1000 - - d, _ := NewClient(&Options{}) - label, _, err := d.Label.Label(&LabelParams{Label_id: "1000"}) - - check(t, err) - assert(t, label.Id == expectedId, fmt.Sprintf("Release.Title looked for %d, and received %d ", expectedId, label.Id)) -} - -func TestLabelService_Releases(t *testing.T) { - expectedId := "Ghetto Sol" - - d, _ := NewClient(&Options{}) - label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"}) - - check(t, err) - assert(t, label.Releases[0].Title == expectedId, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedId, label.Releases[0].Title)) -} diff --git a/masters_test.go b/masters_test.go deleted file mode 100644 index 37a1c68..0000000 --- a/masters_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package discogs - -import ( - "fmt" - "testing" -) - -func TestMasterService_Master(t *testing.T) { - expectedTitle := "Elephant Riddim" - - d, _ := NewClient(nil) - master, _, err := d.Master.Master(&MasterParams{Master_id: "960657"}) - - check(t, err) - assert(t, master.Title == expectedTitle, fmt.Sprintf("master.Title looked for %s, and received %s ", expectedTitle, master.Title)) -} - -func TestMasterService_Versions(t *testing.T) { - expectedTitle := "Stardiver" - - d, _ := NewClient(nil) - versions, _, err := d.Master.Versions(&MasterVersionParams{Master_id: "1000", Page: 1, Per_page: 1}) - - check(t, err) - assert(t, versions.Versions[0].Title == expectedTitle, fmt.Sprintf("master.Title looked for %s, and received %s ", expectedTitle, versions.Versions[0].Title)) -} diff --git a/releases.go b/releases.go index 9949701..70ac30b 100644 --- a/releases.go +++ b/releases.go @@ -1,9 +1,6 @@ package discogs import ( - "encoding/json" - "io/ioutil" - "net/http" "net/url" "strconv" ) @@ -52,14 +49,12 @@ type ReqRelease struct { // ReleaseService ... type ReleaseService struct { url string - header *http.Header currency string } -func newReleaseService(url string, header *http.Header, currency string) *ReleaseService { +func newReleaseService(url string, currency string) *ReleaseService { return &ReleaseService{ url: url, - header: header, currency: currency, } } @@ -70,34 +65,9 @@ func (s *ReleaseService) Release(releaseID int) (*Release, error) { params.Set("CurrAbbr", s.currency) var release *Release - if err := s.request(strconv.Itoa(releaseID), params, &release); err != nil { + if err := request(s.url+strconv.Itoa(releaseID), params, &release); err != nil { return nil, err } return release, nil } - -func (s *ReleaseService) request(path string, params url.Values, resp interface{}) error { - r, err := http.NewRequest("GET", s.url+path+"?"+params.Encode(), nil) - if err != nil { - return err - } - r.Header = *s.header - - client := &http.Client{} - response, err := client.Do(r) - if err != nil { - return err - } - - body, err := ioutil.ReadAll(response.Body) - if err != nil { - return err - } - - if err = json.Unmarshal(body, &resp); err != nil { - return err - } - - return nil -} diff --git a/releases_test.go b/releases_test.go index c9313b6..75fb4e9 100644 --- a/releases_test.go +++ b/releases_test.go @@ -1,7 +1,6 @@ package discogs import ( - "fmt" "io" "net/http" "net/http/httptest" @@ -19,12 +18,13 @@ func TestReleaseServiceRelease(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(ReleaseServer)) defer ts.Close() - d, err := NewClient(&Options{URL: ts.URL}) - if err != nil { - t.Fatalf("failed to create client: %s", err) - } + d := initDiscogsClient(t, &Options{URL: ts.URL}) release, err := d.Release.Release(8138518) + if err != nil { + t.Fatalf("failed to get release: %s", err) + } - check(t, err) - assert(t, release.Title == expectedTitle, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedTitle, release.Title)) + if release.Title != expectedTitle { + t.Fatalf("release title got=%s want=%s ", expectedTitle, release.Title) + } } From 69ef2a5d9a51e3b225ca2fa741a32ada3db6c0e7 Mon Sep 17 00:00:00 2001 From: irlndts Date: Mon, 12 Mar 2018 19:31:50 +0300 Subject: [PATCH 6/7] Release --- discogs_test.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/discogs_test.go b/discogs_test.go index b08d0d0..00a1624 100644 --- a/discogs_test.go +++ b/discogs_test.go @@ -9,23 +9,12 @@ const ( testToken = "" ) -func check(t *testing.T, e error) { - if e != nil { - t.Error(e) - } -} - -func assert(t *testing.T, condition bool, assertion string) { - if !condition { - t.Errorf("Assertion failed: %v", assertion) - } -} - func initDiscogsClient(t *testing.T, options *Options) *Client { if options == nil { options = &Options{ UserAgent: testUserAgent, Currency: "USD", + Token: testToken, } } From 4a80c3cc3953294f55859619e747b1a54f32b900 Mon Sep 17 00:00:00 2001 From: irlndts Date: Mon, 12 Mar 2018 19:51:37 +0300 Subject: [PATCH 7/7] Rating --- examples/discogs_example.go | 4 +-- releases.go | 51 ++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/examples/discogs_example.go b/examples/discogs_example.go index 638ca67..12997bc 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -17,10 +17,10 @@ func main() { return } - release, err := d.Release.Release(9893847) + release, err := d.Release.Rating(9893847) if err != nil { fmt.Println(err) return } - fmt.Printf("%+v\n", release.Title) + fmt.Printf("%+v\n", release) } diff --git a/releases.go b/releases.go index 70ac30b..b9ac9a8 100644 --- a/releases.go +++ b/releases.go @@ -5,6 +5,24 @@ import ( "strconv" ) +// ReleaseService ... +type ReleaseService struct { + url string + currency string +} + +func newReleaseService(url string, currency string) *ReleaseService { + return &ReleaseService{ + url: url, + currency: currency, + } +} + +// ReqRelease serves release request +type ReqRelease struct { + CurrAbbr string +} + // Release serves relesase response from discogs type Release struct { Title string `json:"title"` @@ -42,23 +60,6 @@ type Release struct { Year int `json:"year"` } -type ReqRelease struct { - CurrAbbr string -} - -// ReleaseService ... -type ReleaseService struct { - url string - currency string -} - -func newReleaseService(url string, currency string) *ReleaseService { - return &ReleaseService{ - url: url, - currency: currency, - } -} - // Release returns release by release's ID func (s *ReleaseService) Release(releaseID int) (*Release, error) { params := url.Values{} @@ -71,3 +72,19 @@ func (s *ReleaseService) Release(releaseID int) (*Release, error) { return release, nil } + +// ReleaseRating serves response for community release rating request +type ReleaseRating struct { + ID int `json:"release_id"` + Rating Rating `json:"rating"` +} + +// Ratings retruns community release rating +func (s *ReleaseService) Rating(releaseID int) (*ReleaseRating, error) { + var rating *ReleaseRating + if err := request(s.url+strconv.Itoa(releaseID)+"/rating", nil, &rating); err != nil { + return nil, err + } + + return rating, nil +}