DISCOGS-2 Fix for the day

This commit is contained in:
irlndts 2017-02-13 19:40:27 +03:00
parent cef1beeb81
commit 20710e3f3e
5 changed files with 84 additions and 35 deletions

View File

@ -1,13 +1,16 @@
package discogs
import (
"github.com/irlndts/go-apirequest"
"net/http"
"github.com/irlndts/go-apirequest"
)
const (
libVeresion = "0.1"
discogsAPI = "https://api.discogs.com/"
useragent = "Test UserAgent"
useragent = "irlndts/go-discogs/0.1"
token = "oQTQKAprakIQfWOkAxTdYyDpgUqahHtdbHTuYkIy"
)
// Client is a Discogs client for making Discogs API requests.
@ -17,23 +20,32 @@ type Client struct {
Master *MasterService
Artist *ArtistService
Label *LabelService
Search *SearchService
}
// NewClient returns a new Client.
func NewClient() *Client {
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI).Add("User-Agent", useragent)
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI).Add("User-Agent", useragent) //.Add("Authorization", "Discogs token="+token)
return &Client{
api: base,
Release: newReleaseService(base.New()),
Master: newMasterService(base.New()),
Artist: newArtistService(base.New()),
Label: newLabelService(base.New()),
Master: newMasterService(base.New()),
Release: newReleaseService(base.New()),
Search: newSearchService(base),
}
}
// discogs require specified user agent
// UserAgent sets specified user agent
// Discogs required it
func (c *Client) UserAgent(useragent string) *Client {
c.api.Set("User-Agent", useragent)
return c
}
// 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
}

View File

@ -2,24 +2,22 @@ package main
import (
"fmt"
"github.com/irlndts/go-discogs"
)
func main() {
d := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +http://irlndts.moscow")
d := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +http://irlndts.moscow").Token("")
/*
params := &discogs.ReleaseParams{Release_id: "8138518"}
release, _, err := d.Release.Release(params)
*/
params := &discogs.LabelParams{Label_id: "890477", Page: 2, Per_page: 3}
label, _, err := d.Label.Releases(params)
params := &discogs.SearchRequest{Release_title: "nevermind", Artist: "nirvana"}
search, _, err := d.Search.Search(params)
if err != nil {
fmt.Println(err)
} else {
for _, release := range label.Releases {
fmt.Println(release.Title)
}
return
}
for _, r := range search.Results {
fmt.Println(r)
}
}

View File

@ -1,8 +1,9 @@
package discogs
import (
"github.com/irlndts/go-apirequest"
"net/http"
"github.com/irlndts/go-apirequest"
)
type LabelService struct {
@ -10,23 +11,23 @@ type LabelService struct {
}
type LabelParams struct {
Label_id string
LabelID string
Page int
Per_page int
PerPage int
}
type Label struct {
Profile string `json:"profile"`
Releases_url string `json:"releases_url"`
ReleasesUrl string `json:"releases_url"`
Name string `json:"name"`
Contact_info string `json:"contact_info"`
ContactInfo string `json:"contact_info"`
Uri string `json:"uri"`
Sublabels []Sublable `json:"sublabels"`
Urls []string `json:"urls"`
Images []Image `json:"images"`
Resource_url string `json:"resource_url"`
ResourceUrl string `json:"resource_url"`
Id int `json:"id"`
Data_quality string `json:"data_quality"`
DataQuality string `json:"data_quality"`
}
type LabelReleases struct {
@ -44,7 +45,7 @@ func (self *LabelService) Label(params *LabelParams) (*Label, *http.Response, er
label := new(Label)
apiError := new(APIError)
resp, err := self.api.New().Get(params.Label_id).Receive(label, apiError)
resp, err := self.api.New().Get(params.LabelID).Receive(label, apiError)
return label, resp, relevantError(err, *apiError)
}
@ -52,6 +53,6 @@ func (self *LabelService) Releases(params *LabelParams) (*LabelReleases, *http.R
releases := new(LabelReleases)
apiError := new(APIError)
resp, err := self.api.New().Get(params.Label_id+"/releases").QueryStruct(params).Receive(releases, apiError)
resp, err := self.api.New().Get(params.LabelID+"/releases").QueryStruct(params).Receive(releases, apiError)
return releases, resp, relevantError(err, *apiError)
}

38
search.go Normal file
View File

@ -0,0 +1,38 @@
package discogs
import (
"net/http"
"github.com/irlndts/go-apirequest"
)
type SearchRequest struct {
Release_title string
Artist string
Page int
PerPage int
}
type Search struct {
Pagination Page `json:"pagination"`
Results []interface{} `json:"results"`
}
type SearchService struct {
api *apirequest.API
}
func newSearchService(api *apirequest.API) *SearchService {
return &SearchService{
api: api.Path("database/search"),
}
}
func (self *SearchService) Search(params *SearchRequest) (*Search, *http.Response, error) {
search := new(Search)
apiError := new(APIError)
resp, err := self.api.QueryStruct(params).Receive(search, apiError)
return search, resp, relevantError(err, *apiError)
}