Improvements
This commit is contained in:
parent
916e1f0bd6
commit
6af125badd
@ -28,9 +28,11 @@ type Client struct {
|
|||||||
|
|
||||||
// NewClient returns a new Client.
|
// NewClient returns a new Client.
|
||||||
func NewClient(o *Options) (*Client, error) {
|
func NewClient(o *Options) (*Client, error) {
|
||||||
|
header := &http.Header{}
|
||||||
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI)
|
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI)
|
||||||
if o.UserAgent != "" {
|
if o.UserAgent != "" {
|
||||||
base.Set("User-Agent", o.UserAgent)
|
base.Set("User-Agent", o.UserAgent)
|
||||||
|
header.Add("User-Agent", o.UserAgent)
|
||||||
}
|
}
|
||||||
|
|
||||||
cur, err := currency(o.Currency)
|
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
|
// set token, it's required for some queries like search
|
||||||
if o.Token != "" {
|
if o.Token != "" {
|
||||||
base.Set("Authorization", "Discogs token="+o.Token)
|
base.Set("Authorization", "Discogs token="+o.Token)
|
||||||
|
header.Add("Authorization", "Discogs token="+o.Token)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
Release: newReleaseService(base.New(), cur),
|
Release: newReleaseService(header, cur),
|
||||||
Artist: newArtistService(base.New()),
|
Artist: newArtistService(base.New()),
|
||||||
Label: newLabelService(base.New()),
|
Label: newLabelService(base.New()),
|
||||||
Master: newMasterService(base.New()),
|
Master: newMasterService(base.New()),
|
||||||
|
@ -10,6 +10,7 @@ func main() {
|
|||||||
d, err := discogs.NewClient(&discogs.Options{
|
d, err := discogs.NewClient(&discogs.Options{
|
||||||
UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
|
UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
|
||||||
Currency: "EUR",
|
Currency: "EUR",
|
||||||
|
Token: "",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@ -21,5 +22,5 @@ func main() {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("%+v\n", release)
|
fmt.Printf("%+v\n", release.Title)
|
||||||
}
|
}
|
||||||
|
50
releases.go
50
releases.go
@ -1,9 +1,11 @@
|
|||||||
package discogs
|
package discogs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
apirequest "github.com/irlndts/go-apirequest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Release serves relesase response from discogs
|
// Release serves relesase response from discogs
|
||||||
@ -49,23 +51,51 @@ type ReqRelease struct {
|
|||||||
|
|
||||||
// ReleaseService ...
|
// ReleaseService ...
|
||||||
type ReleaseService struct {
|
type ReleaseService struct {
|
||||||
api *apirequest.API
|
header *http.Header
|
||||||
currency string
|
currency string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newReleaseService(api *apirequest.API, currency string) *ReleaseService {
|
func newReleaseService(header *http.Header, currency string) *ReleaseService {
|
||||||
return &ReleaseService{
|
return &ReleaseService{
|
||||||
api: api.Path("releases/"),
|
header: header,
|
||||||
currency: currency,
|
currency: currency,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release returns release by release's ID
|
// Release returns release by release's ID
|
||||||
func (s *ReleaseService) Release(releaseID int) (*Release, error) {
|
func (s *ReleaseService) Release(releaseID int) (*Release, error) {
|
||||||
release := new(Release)
|
params := url.Values{}
|
||||||
apiError := new(APIError)
|
params.Set("CurrAbbr", s.currency)
|
||||||
|
|
||||||
req := &ReqRelease{CurrAbbr: s.currency}
|
var release *Release
|
||||||
_, err := s.api.New().Get(strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError)
|
if err := s.request(strconv.Itoa(releaseID), params, &release); err != nil {
|
||||||
return release, relevantError(err, *apiError)
|
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