Improvements
This commit is contained in:
parent
916e1f0bd6
commit
6af125badd
@ -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()),
|
||||
|
@ -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)
|
||||
}
|
||||
|
50
releases.go
50
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user