From 20710e3f3ea81e8c5548bbf053439e921431cfcd Mon Sep 17 00:00:00 2001 From: irlndts Date: Mon, 13 Feb 2017 19:40:27 +0300 Subject: [PATCH 1/2] 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) +} From d5292c30c909e6aad7e587aca0b0c3501cc5071a Mon Sep 17 00:00:00 2001 From: irlndts Date: Tue, 14 Feb 2017 22:01:50 +0300 Subject: [PATCH 2/2] DISCOGS-2 Search implementation --- README.md | 63 ++++++++++++++++++++++++++++++++----- artists_test.go | 4 +-- discogs.go | 11 +++---- discogs_test.go | 5 +++ examples/discogs_example.go | 8 ++--- labels.go | 32 +++++++++---------- labels_test.go | 4 +-- masters_test.go | 4 +-- releases_test.go | 2 +- search.go | 53 +++++++++++++++++++++++++------ 10 files changed, 135 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 56c9381..b0a289c 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,24 @@ # REST API 2.0 Discogs.com client -go-discogs is a Go client library for the [Discogs API](https://www.discogs.com/developers/). Check the usage section or try the examples to see how to access the Discogs API. +go-discogs is a Go client library for the [Discogs API](https://www.discogs.com/developers/). Check the usage section to see how to access the Discogs API. + +### Changelog +``` +14.02.2017 +- search is implemented +- minor improvements +``` ### Feauteres * Database - * Releases + * [Releases](#releases) * Master Releases * Release Versions * Artists * Artist Releases * Label * All Label Releases - -#### ToDo -- Search - + * [Search](#search) Install -------- @@ -28,7 +32,11 @@ First of all import library and init client variable. According to discogs api d import "github.com/irlndts/go-discogs" ``` ```go -client := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +example.com") +client := discogs.NewClient("TestDiscogsClient/0.0.1 +example.com", "") +``` +Some requests require authentication (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. Key-secret doesn't implemented yet, but token is yes. +```go +client := discogs.NewClient("TestDiscogsClient/0.0.1 +example.com", "sometoken") ``` #### Releases @@ -55,5 +63,46 @@ client := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +example.com") */ ``` +#### Search +Issue a search query to discogs database. This endpoint accepts pagination parameters +Authentication (as any user) is required. + +Use `SearchRequest` struc to create a request +```go +type SearchRequest struct { + Q string // search query (optional) + Type string // one of release, master, artist, label (optional) + Title string // search by combined “Artist Name - Release Title” title field (optional) + Release_title string // search release titles (optional) + Credit string // search release credits (optional) + Artist string // search artist names (optional) + Anv string // search artist ANV (optional) + Label string // search label names (optional) + Genre string // search genres (optional) + Style string // search styles (optional) + Country string // search release country (optional) + Year string // search release year (optional) + Format string // search formats (optional) + Catno string // search catalog number (optional) + Barcode string // search barcodes (optional) + Track string // search track titles (optional) + Submitter string // search submitter username (optional) + Contributer string // search contributor usernames (optional) + + Page int // optional + Per_page int // optional +} +``` + +Example +```go + request:= &discogs.SearchRequest{Artist: "reggaenauts", Release_title: "river rock", Page: 0, Per_page: 1} + search, _, err := client.Search(request) + + for _, r := range search.Results { + fmt.Println(r.Title) + } +``` + etc. More examples - soon diff --git a/artists_test.go b/artists_test.go index 85656ca..a31d616 100644 --- a/artists_test.go +++ b/artists_test.go @@ -8,7 +8,7 @@ import ( func TestArtistService_Artist(t *testing.T) { expectedId := 1000 - d := NewClient().UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs") + d := NewClient(testUserAgent, testToken) artist, _, err := d.Artist.Artist(&ArtistParams{Artist_id: "1000"}) check(t, err) @@ -18,7 +18,7 @@ func TestArtistService_Artist(t *testing.T) { func TestArtistService_Releases(t *testing.T) { expectedArtist := "Dave Clarke" - d := NewClient().UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs") + d := NewClient(testUserAgent, testToken) releases, _, err := d.Artist.Releases(&ArtistParams{Artist_id: "1000", Sort: "year", Sort_order: "desc"}) check(t, err) diff --git a/discogs.go b/discogs.go index 9084c87..7d40533 100644 --- a/discogs.go +++ b/discogs.go @@ -7,10 +7,7 @@ import ( ) const ( - libVeresion = "0.1" - discogsAPI = "https://api.discogs.com/" - useragent = "irlndts/go-discogs/0.1" - token = "oQTQKAprakIQfWOkAxTdYyDpgUqahHtdbHTuYkIy" + discogsAPI = "https://api.discogs.com/" ) // Client is a Discogs client for making Discogs API requests. @@ -24,8 +21,8 @@ type Client struct { } // NewClient returns a new Client. -func NewClient() *Client { - base := apirequest.New().Client(&http.Client{}).Base(discogsAPI).Add("User-Agent", useragent) //.Add("Authorization", "Discogs token="+token) +func NewClient(useragent, token string) *Client { + base := apirequest.New().Client(&http.Client{}).Base(discogsAPI).Add("User-Agent", useragent).Add("Authorization", "Discogs token="+token) return &Client{ api: base, @@ -33,7 +30,7 @@ func NewClient() *Client { Label: newLabelService(base.New()), Master: newMasterService(base.New()), Release: newReleaseService(base.New()), - Search: newSearchService(base), + Search: newSearchService(base.New()), } } diff --git a/discogs_test.go b/discogs_test.go index d7f779e..b58defb 100644 --- a/discogs_test.go +++ b/discogs_test.go @@ -4,6 +4,11 @@ import ( "testing" ) +const ( + testUserAgent = "UnitTestClient/0.0.2 +https://github.com/irlndts/go-discogs" + testToken = "" +) + func check(t *testing.T, e error) { if e != nil { t.Error(e) diff --git a/examples/discogs_example.go b/examples/discogs_example.go index c9d1825..e26fb5b 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -7,10 +7,10 @@ import ( ) func main() { - d := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +http://irlndts.moscow").Token("") + d := discogs.NewClient("TestDiscogsClient/0.0.1 +http://irlndts.moscow", "") - params := &discogs.SearchRequest{Release_title: "nevermind", Artist: "nirvana"} - search, _, err := d.Search.Search(params) + request := &discogs.SearchRequest{Q: "The Reggaenauts - River Rock / Thursday Kick-off", Page: 0, Per_page: 1} + search, _, err := d.Search.Search(request) if err != nil { fmt.Println(err) @@ -18,6 +18,6 @@ func main() { } for _, r := range search.Results { - fmt.Println(r) + fmt.Println(r.Id, r.Title) } } diff --git a/labels.go b/labels.go index 81dbf4c..e5e484c 100644 --- a/labels.go +++ b/labels.go @@ -11,23 +11,23 @@ type LabelService struct { } type LabelParams struct { - LabelID string - Page int - PerPage int + Label_id string + Page int + Per_Page int } type Label struct { - 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"` + 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"` } type LabelReleases struct { @@ -45,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.LabelID).Receive(label, apiError) + resp, err := self.api.New().Get(params.Label_id).Receive(label, apiError) return label, resp, relevantError(err, *apiError) } @@ -53,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.LabelID+"/releases").QueryStruct(params).Receive(releases, apiError) + resp, err := self.api.New().Get(params.Label_id+"/releases").QueryStruct(params).Receive(releases, apiError) return releases, resp, relevantError(err, *apiError) } diff --git a/labels_test.go b/labels_test.go index fd5b50f..4d2a466 100644 --- a/labels_test.go +++ b/labels_test.go @@ -8,7 +8,7 @@ import ( func TestLabelService_Label(t *testing.T) { expectedId := 1000 - d := NewClient().UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs") + d := NewClient(testUserAgent, testToken) label, _, err := d.Label.Label(&LabelParams{Label_id: "1000"}) check(t, err) @@ -18,7 +18,7 @@ func TestLabelService_Label(t *testing.T) { func TestLabelService_Releases(t *testing.T) { expectedId := "Cha Cha Twist" - d := NewClient().UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs") + d := NewClient(testUserAgent, testToken) label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"}) check(t, err) diff --git a/masters_test.go b/masters_test.go index e2452ff..5a5a792 100644 --- a/masters_test.go +++ b/masters_test.go @@ -8,7 +8,7 @@ import ( func TestMasterService_Master(t *testing.T) { expectedTitle := "Elephant Riddim" - d := NewClient().UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs") + d := NewClient(testUserAgent, testToken) master, _, err := d.Master.Master(&MasterParams{Master_id: "960657"}) check(t, err) @@ -18,7 +18,7 @@ func TestMasterService_Master(t *testing.T) { func TestMasterService_Versions(t *testing.T) { expectedTitle := "Stardiver" - d := NewClient().UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs") + d := NewClient(testUserAgent, testToken) versions, _, err := d.Master.Versions(&MasterVersionParams{Master_id: "1000", Page: 1, Per_page: 1}) check(t, err) diff --git a/releases_test.go b/releases_test.go index 12cbcbe..1d425ad 100644 --- a/releases_test.go +++ b/releases_test.go @@ -8,7 +8,7 @@ import ( func TestReleaseService_Release(t *testing.T) { expectedTitle := "Elephant Riddim" - d := NewClient().UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs") + d := NewClient(testUserAgent, testToken) release, _, err := d.Release.Release(&ReleaseParams{Release_id: "8138518"}) check(t, err) diff --git a/search.go b/search.go index 259dc71..d5a43eb 100644 --- a/search.go +++ b/search.go @@ -6,21 +6,54 @@ import ( "github.com/irlndts/go-apirequest" ) -type SearchRequest struct { - Release_title string - Artist string +type SearchService struct { + api *apirequest.API +} - Page int - PerPage int +type SearchRequest struct { + Q string // search query (optional) + Type string // one of release, master, artist, label (optional) + Title string // search by combined “Artist Name - Release Title” title field (optional) + Release_title string // search release titles (optional) + Credit string // search release credits (optional) + Artist string // search artist names (optional) + Anv string // search artist ANV (optional) + Label string // search label names (optional) + Genre string // search genres (optional) + Style string // search styles (optional) + Country string // search release country (optional) + Year string // search release year (optional) + Format string // search formats (optional) + Catno string // search catalog number (optional) + Barcode string // search barcodes (optional) + Track string // search track titles (optional) + Submitter string // search submitter username (optional) + Contributer string // search contributor usernames (optional) + + Page int // optional + Per_page int // optional } type Search struct { - Pagination Page `json:"pagination"` - Results []interface{} `json:"results"` + Pagination Page `json:"pagination"` + Results []Result `json:"results"` } -type SearchService struct { - api *apirequest.API +type Result struct { + Style []string `json:"style"` + Thumb string `json:"thumb"` + Title string `json:"title"` + Country string `json:"country"` + Format []string `json:"format"` + Uri string `json:"uri"` + Community Community `json:"community"` + Label []string `json:"label"` + Catno string `json:"catno"` + Year string `json:"year"` + Genre []string `json:"genre"` + Resource_url string `json:"resource_url"` + Type string `json:"type"` + Id int `json:"id"` } func newSearchService(api *apirequest.API) *SearchService { @@ -33,6 +66,6 @@ func (self *SearchService) Search(params *SearchRequest) (*Search, *http.Respons search := new(Search) apiError := new(APIError) - resp, err := self.api.QueryStruct(params).Receive(search, apiError) + resp, err := self.api.New().QueryStruct(params).Receive(search, apiError) return search, resp, relevantError(err, *apiError) }