From 20710e3f3ea81e8c5548bbf053439e921431cfcd Mon Sep 17 00:00:00 2001 From: irlndts Date: Mon, 13 Feb 2017 19:40:27 +0300 Subject: [PATCH] DISCOGS-2 Fix for the day --- discogs.go | 26 +++++++++++++++++------- examples/discogs_example.go | 20 +++++++++--------- labels.go | 35 ++++++++++++++++---------------- commonstructs.go => models.go | 0 search.go | 38 +++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 35 deletions(-) rename commonstructs.go => models.go (100%) create mode 100644 search.go diff --git a/discogs.go b/discogs.go index c458563..9084c87 100644 --- a/discogs.go +++ b/discogs.go @@ -1,13 +1,16 @@ package discogs import ( - "github.com/irlndts/go-apirequest" "net/http" + + "github.com/irlndts/go-apirequest" ) const ( - discogsAPI = "https://api.discogs.com/" - useragent = "Test UserAgent" + libVeresion = "0.1" + discogsAPI = "https://api.discogs.com/" + 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 +} diff --git a/examples/discogs_example.go b/examples/discogs_example.go index f0f37f6..c9d1825 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -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) } } diff --git a/labels.go b/labels.go index 569184b..81dbf4c 100644 --- a/labels.go +++ b/labels.go @@ -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 - Page int - Per_page int + LabelID string + Page int + PerPage int } type Label struct { - Profile string `json:"profile"` - Releases_url string `json:"releases_url"` - Name string `json:"name"` - Contact_info 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"` - Id int `json:"id"` - Data_quality string `json:"data_quality"` + Profile string `json:"profile"` + ReleasesUrl string `json:"releases_url"` + Name string `json:"name"` + ContactInfo string `json:"contact_info"` + Uri string `json:"uri"` + Sublabels []Sublable `json:"sublabels"` + Urls []string `json:"urls"` + Images []Image `json:"images"` + ResourceUrl string `json:"resource_url"` + Id int `json:"id"` + 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) } diff --git a/commonstructs.go b/models.go similarity index 100% rename from commonstructs.go rename to models.go diff --git a/search.go b/search.go new file mode 100644 index 0000000..259dc71 --- /dev/null +++ b/search.go @@ -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) +}