Release improvement
This commit is contained in:
parent
7d6af61b78
commit
d1095bc769
14
README.md
14
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")
|
||||
```
|
||||
|
||||
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");
|
||||
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",
|
||||
})
|
||||
```
|
||||
|
||||
#### 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
|
||||
```
|
||||
|
@ -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))
|
||||
}
|
41
discogs.go
41
discogs.go
@ -1,8 +1,11 @@
|
||||
package discogs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/irlndts/go-apirequest"
|
||||
)
|
||||
@ -27,14 +30,19 @@ 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 != "" {
|
||||
|
||||
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 {
|
||||
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
@ -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))
|
||||
}
|
34
releases.go
34
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
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
check(t, err)
|
||||
assert(t, release.Title == expectedTitle, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedTitle, release.Title))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get release: %s", err)
|
||||
}
|
||||
|
||||
if release.Title != expectedTitle {
|
||||
t.Fatalf("release title got=%s want=%s ", expectedTitle, release.Title)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user