11 Commits
0.0.1 ... 0.0.3

Author SHA1 Message Date
Artem Piskun
4e7c6c065a Merge pull request #3 from irlndts/DISCOGS-2
Discogs-2 Search Implementation
2017-02-14 22:05:07 +03:00
irlndts
d5292c30c9 DISCOGS-2 Search implementation 2017-02-14 22:01:50 +03:00
irlndts
20710e3f3e DISCOGS-2 Fix for the day 2017-02-13 19:40:27 +03:00
irlndts
cef1beeb81 test is fixed 2017-02-10 19:54:21 +03:00
Artem Piskun
83d3334321 Update README.md 2016-03-14 14:19:41 +03:00
Artem Piskun
c131693a53 Update README.md 2016-03-11 17:54:33 +03:00
irlndts
db5bf6d2d3 Merge branch 'master' of https://github.com/irlndts/go-discogs 2016-03-11 17:54:58 +03:00
irlndts
7a047f6bd5 net/http moved inside the lib 2016-03-11 17:47:23 +03:00
Artem Piskun
fd09f103fc Update README.md 2016-03-11 17:46:37 +03:00
Artem Piskun
b8e2c017d7 Update README.md 2016-03-11 17:41:48 +03:00
irlndts
de1e81b89d Artist Releases block 2016-03-11 17:07:35 +03:00
12 changed files with 248 additions and 41 deletions

107
README.md
View File

@@ -1,3 +1,108 @@
# REST API 2.0 Discogs.com client
..work in progress..
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)
* Master Releases
* Release Versions
* Artists
* Artist Releases
* Label
* All Label Releases
* [Search](#search)
Install
--------
go get github.com/irlndts/go-discogs
Usage
---------
The discogs package provides a client for accessing the Discogs API.
First of all import library and init client variable. According to discogs api documentation you [must provide your user-agent](https://www.discogs.com/developers/#page:home,header:home-general-information).
```go
import "github.com/irlndts/go-discogs"
```
```go
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
```go
params := &discogs.ReleaseParams{Release_id: "8138518"}
release, _, err := client.Release.Release(params)
fmt.Println(fmt.Println(release.Artists[0].Name, " - ", release.Title)) // St. Petersburg Ska-Jazz Review - Elephant Riddim
```
#### Artists
```go
params := &discogs.LabelParams{Label_id: "890477", Page: 2, Per_page: 3}
label, _, err := client.Label.Releases(params)
for _, release := range label.Releases {
fmt.Println(release.Title)
}
/*
Someday / I Hate Everything About You
Spy Potion
Surf Attack From Russia
*/
```
#### 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

View File

@@ -10,7 +10,11 @@ type ArtistService struct {
}
type ArtistParams struct {
Artist_id string
Artist_id string
Sort string // year, title, format
Sort_order string // asc, desc
Page int
Per_page int
}
type Artist struct {
@@ -26,6 +30,11 @@ type Artist struct {
Members []Member `json:"members"`
}
type ArtistReleases struct {
Paginastion Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
func newArtistService(api *apirequest.API) *ArtistService {
return &ArtistService{
api: api.Path("artists/"),
@@ -39,3 +48,11 @@ func (self *ArtistService) Artist(params *ArtistParams) (*Artist, *http.Response
resp, err := self.api.New().Get(params.Artist_id).Receive(artist, apiError)
return artist, resp, relevantError(err, *apiError)
}
func (self *ArtistService) Releases(params *ArtistParams) (*ArtistReleases, *http.Response, error) {
releases := new(ArtistReleases)
apiError := new(APIError)
resp, err := self.api.New().Get(params.Artist_id+"/releases").QueryStruct(params).Receive(releases, apiError)
return releases, resp, relevantError(err, *apiError)
}

View File

@@ -2,17 +2,25 @@ package discogs
import (
"fmt"
"net/http"
"testing"
)
func TestArtistService_Artist(t *testing.T) {
expectedId := 1000
client := &http.Client{}
d := NewClient(client).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)
assert(t, artist.Id == expectedId, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedId, artist.Id))
}
func TestArtistService_Releases(t *testing.T) {
expectedArtist := "Dave Clarke"
d := NewClient(testUserAgent, testToken)
releases, _, err := d.Artist.Releases(&ArtistParams{Artist_id: "1000", Sort: "year", Sort_order: "desc"})
check(t, err)
assert(t, releases.Releases[0].Artist == expectedArtist, fmt.Sprintf("Releses.Artist looked for %s, and received %s ", expectedArtist, releases.Releases[0].Artist))
}

View File

@@ -1,13 +1,13 @@
package discogs
import (
"github.com/irlndts/go-apirequest"
"net/http"
"github.com/irlndts/go-apirequest"
)
const (
discogsAPI = "https://api.discogs.com/"
useragent = "Test UserAgent"
)
// Client is a Discogs client for making Discogs API requests.
@@ -17,23 +17,32 @@ type Client struct {
Master *MasterService
Artist *ArtistService
Label *LabelService
Search *SearchService
}
// NewClient returns a new Client.
func NewClient(httpClient *http.Client) *Client {
base := apirequest.New().Client(httpClient).Base(discogsAPI).Add("User-Agent", useragent)
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,
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.New()),
}
}
// 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

@@ -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)

View File

@@ -2,26 +2,22 @@ package main
import (
"fmt"
"github.com/irlndts/go-discogs"
"net/http"
)
func main() {
client := &http.Client{}
d := discogs.NewClient(client).UserAgent("TestDiscogsClient/0.0.1 +http://irlndts.moscow")
d := discogs.NewClient("TestDiscogsClient/0.0.1 +http://irlndts.moscow", "")
/*
params := &discogs.ReleaseParams{Release_id: "8138518"}
release, _, err := d.Release.Release(params)
*/
params := &discogs.LabelParams{Label_id: "1000"}
label, _, err := d.Label.Releases(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)
} else {
//fmt.Println(release.Title)
fmt.Println(label.Releases[0].Title)
return
}
for _, r := range search.Results {
fmt.Println(r.Id, r.Title)
}
//fmt.Println(resp)
}

View File

@@ -1,8 +1,9 @@
package discogs
import (
"github.com/irlndts/go-apirequest"
"net/http"
"github.com/irlndts/go-apirequest"
)
type LabelService struct {
@@ -12,7 +13,7 @@ type LabelService struct {
type LabelParams struct {
Label_id string
Page int
Per_page int
Per_Page int
}
type Label struct {

View File

@@ -2,15 +2,13 @@ package discogs
import (
"fmt"
"net/http"
"testing"
)
func TestLabelService_Label(t *testing.T) {
expectedId := 1000
client := &http.Client{}
d := NewClient(client).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,10 +16,9 @@ func TestLabelService_Label(t *testing.T) {
}
func TestLabelService_Releases(t *testing.T) {
expectedId := "Good Time"
expectedId := "Cha Cha Twist"
client := &http.Client{}
d := NewClient(client).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)

View File

@@ -2,15 +2,13 @@ package discogs
import (
"fmt"
"net/http"
"testing"
)
func TestMasterService_Master(t *testing.T) {
expectedTitle := "Elephant Riddim"
client := &http.Client{}
d := NewClient(client).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)
@@ -20,8 +18,7 @@ func TestMasterService_Master(t *testing.T) {
func TestMasterService_Versions(t *testing.T) {
expectedTitle := "Stardiver"
client := &http.Client{}
d := NewClient(client).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)

View File

@@ -137,4 +137,7 @@ type ReleaseSource struct {
Thumb string `json:"thumb"`
Title string `json:"title"`
Year int `json:"year"`
Main_release int `json:"main_release"`
Role string `json:"role"`
Type string `json:"type"`
}

View File

@@ -2,15 +2,13 @@ package discogs
import (
"fmt"
"net/http"
"testing"
)
func TestReleaseService_Release(t *testing.T) {
expectedTitle := "Elephant Riddim"
client := &http.Client{}
d := NewClient(client).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)

71
search.go Normal file
View File

@@ -0,0 +1,71 @@
package discogs
import (
"net/http"
"github.com/irlndts/go-apirequest"
)
type SearchService struct {
api *apirequest.API
}
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 []Result `json:"results"`
}
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 {
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.New().QueryStruct(params).Receive(search, apiError)
return search, resp, relevantError(err, *apiError)
}