From d1095bc769c76eedaf1f6f1c45588ec153431f5e Mon Sep 17 00:00:00 2001 From: irlndts Date: Mon, 12 Mar 2018 19:30:58 +0300 Subject: [PATCH] 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) + } }