Compare commits
63 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d987189090 | ||
![]() |
77c264c993 | ||
![]() |
84a3ecd4c5 | ||
![]() |
f9bfcf8fba | ||
![]() |
664896b3dd | ||
![]() |
3c48a3e6d4 | ||
![]() |
271bbe8b83 | ||
![]() |
35dfbe02d1 | ||
![]() |
618b882634 | ||
![]() |
a4b6e4193a | ||
![]() |
e1e0446c29 | ||
![]() |
dd501a3f65 | ||
![]() |
110a37661a | ||
![]() |
3775bc39c7 | ||
![]() |
e9ad282056 | ||
![]() |
31a2e3b959 | ||
![]() |
8e69f9334b | ||
![]() |
788518e3e2 | ||
![]() |
0a3a17b69d | ||
![]() |
ebfd052f67 | ||
![]() |
c526ccd692 | ||
![]() |
91d056b186 | ||
![]() |
441de35813 | ||
![]() |
872ff94673 | ||
![]() |
afd778f11c | ||
![]() |
dd29f70850 | ||
![]() |
d5ddddc09d | ||
![]() |
ede8271b88 | ||
![]() |
b2674eb55e | ||
![]() |
2bc2347afc | ||
![]() |
62ba69198d | ||
![]() |
33699d6ad5 | ||
![]() |
4b824487ea | ||
![]() |
1091d9be02 | ||
![]() |
b8ec73e2a0 | ||
![]() |
7a214b4b15 | ||
![]() |
fcb075f0f4 | ||
![]() |
615c7561ad | ||
![]() |
93ddab1b21 | ||
![]() |
ad267389f6 | ||
![]() |
8d359bb193 | ||
![]() |
4a80c3cc39 | ||
![]() |
69ef2a5d9a | ||
![]() |
d1095bc769 | ||
![]() |
7d6af61b78 | ||
![]() |
6af125badd | ||
![]() |
916e1f0bd6 | ||
![]() |
d0f617733a | ||
![]() |
055007c459 | ||
![]() |
72a2e61894 | ||
![]() |
87747a846f | ||
![]() |
795cdb9f79 | ||
![]() |
6a5e9d0a6b | ||
![]() |
7536e26d28 | ||
![]() |
5f77b45281 | ||
![]() |
0f41f45197 | ||
![]() |
efff71f46e | ||
![]() |
86244f6665 | ||
![]() |
78b3d0b0ad | ||
![]() |
44db789a6b | ||
![]() |
dd4fa14f61 | ||
![]() |
26dbacfe44 | ||
![]() |
bce16b5ed1 |
3
.travis.yml
Normal file
3
.travis.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
language: go
|
||||||
|
go:
|
||||||
|
- "1.12.5"
|
65
README.md
65
README.md
@@ -1,28 +1,24 @@
|
|||||||
# REST API 2.0 Discogs.com client
|
# 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 to see how to access the Discogs API.
|
[](https://travis-ci.org/irlndts/go-discogs)[](https://goreportcard.com/report/github.com/irlndts/go-discogs)
|
||||||
|
|
||||||
### Changelog
|
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.
|
||||||
```
|
|
||||||
14.02.2017
|
|
||||||
- search is implemented
|
|
||||||
- minor improvements
|
|
||||||
```
|
|
||||||
|
|
||||||
### Feauteres
|
### Feauteres
|
||||||
* Database
|
* Database
|
||||||
* [Releases](#releases)
|
* [Releases](#releases)
|
||||||
|
* Release Rating
|
||||||
* Master Releases
|
* Master Releases
|
||||||
* Release Versions
|
* Master Versions
|
||||||
* Artists
|
* Artists
|
||||||
* Artist Releases
|
* Artist Releases
|
||||||
* Label
|
* Label
|
||||||
* All Label Releases
|
* All Label Releases
|
||||||
* [Search](#search)
|
* [Search](#search)
|
||||||
|
|
||||||
Install
|
Install
|
||||||
--------
|
--------
|
||||||
go get github.com/irlndts/go-discogs
|
go get -u github.com/irlndts/go-discogs
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
---------
|
---------
|
||||||
@@ -31,49 +27,35 @@ First of all import library and init client variable. According to discogs api d
|
|||||||
```go
|
```go
|
||||||
import "github.com/irlndts/go-discogs"
|
import "github.com/irlndts/go-discogs"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Some requests require authentification (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](https://www.discogs.com/settings/developers).
|
||||||
|
This is token way example:
|
||||||
```go
|
```go
|
||||||
client := discogs.NewClient("TestDiscogsClient/0.0.1 +example.com", "")
|
client, err := discogs.NewClient(&discogs.Options{
|
||||||
```
|
UserAgent: "Some Name",
|
||||||
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.
|
Currency: "EUR", // optional, "USD" (default), "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR" are allowed
|
||||||
```go
|
Token: "Some Token", // optional
|
||||||
client := discogs.NewClient("TestDiscogsClient/0.0.1 +example.com", "sometoken")
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Releases
|
#### Releases
|
||||||
```go
|
```go
|
||||||
params := &discogs.ReleaseParams{Release_id: "8138518"}
|
release, _ := client.Database.Release(9893847)
|
||||||
release, _, err := client.Release.Release(params)
|
fmt.Println(release.Artists[0].Name, " - ", release.Title)
|
||||||
|
// St. Petersburg Ska-Jazz Review - Elephant Riddim
|
||||||
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
|
#### Search
|
||||||
Issue a search query to discogs database. This endpoint accepts pagination parameters
|
Issue a search query to discogs database. This endpoint accepts pagination parameters.
|
||||||
Authentication (as any user) is required.
|
Authentication (as any user) is required.
|
||||||
|
|
||||||
Use `SearchRequest` struc to create a request
|
Use `SearchRequest` struct to create a request.
|
||||||
```go
|
```go
|
||||||
type SearchRequest struct {
|
type SearchRequest struct {
|
||||||
Q string // search query (optional)
|
Q string // search query (optional)
|
||||||
Type string // one of release, master, artist, label (optional)
|
Type string // one of release, master, artist, label (optional)
|
||||||
Title string // search by combined “Artist Name - Release Title” title field (optional)
|
Title string // search by combined “Artist Name - Release Title” title field (optional)
|
||||||
Release_title string // search release titles (optional)
|
ReleaseTitle string // search release titles (optional)
|
||||||
Credit string // search release credits (optional)
|
Credit string // search release credits (optional)
|
||||||
Artist string // search artist names (optional)
|
Artist string // search artist names (optional)
|
||||||
Anv string // search artist ANV (optional)
|
Anv string // search artist ANV (optional)
|
||||||
@@ -90,14 +72,14 @@ type SearchRequest struct {
|
|||||||
Contributer string // search contributor usernames (optional)
|
Contributer string // search contributor usernames (optional)
|
||||||
|
|
||||||
Page int // optional
|
Page int // optional
|
||||||
Per_page int // optional
|
PerPage int // optional
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Example
|
Example
|
||||||
```go
|
```go
|
||||||
request:= &discogs.SearchRequest{Artist: "reggaenauts", Release_title: "river rock", Page: 0, Per_page: 1}
|
request := discogs.SearchRequest{Artist: "reggaenauts", ReleaseTitle: "river rock", Page: 0, PerPage: 1}
|
||||||
search, _, err := client.Search(request)
|
search, _ := client.Search.Search(request)
|
||||||
|
|
||||||
for _, r := range search.Results {
|
for _, r := range search.Results {
|
||||||
fmt.Println(r.Title)
|
fmt.Println(r.Title)
|
||||||
@@ -105,4 +87,3 @@ Example
|
|||||||
```
|
```
|
||||||
|
|
||||||
etc.
|
etc.
|
||||||
More examples - soon
|
|
||||||
|
58
artists.go
58
artists.go
@@ -1,58 +0,0 @@
|
|||||||
package discogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/irlndts/go-apirequest"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ArtistService struct {
|
|
||||||
api *apirequest.API
|
|
||||||
}
|
|
||||||
|
|
||||||
type ArtistParams struct {
|
|
||||||
Artist_id string
|
|
||||||
Sort string // year, title, format
|
|
||||||
Sort_order string // asc, desc
|
|
||||||
Page int
|
|
||||||
Per_page int
|
|
||||||
}
|
|
||||||
|
|
||||||
type Artist struct {
|
|
||||||
Namevariations []string `json:"namevariations"`
|
|
||||||
Profile string `json:"profile"`
|
|
||||||
Releases_url string `json:"releases_url"`
|
|
||||||
Resource_url string `json:"resource_url"`
|
|
||||||
Uri string `json:"uri"`
|
|
||||||
Urls []string `json:"urls"`
|
|
||||||
Data_quality string `json:"data_quality"`
|
|
||||||
Id int `json:"id"`
|
|
||||||
Images []Image `json:"images"`
|
|
||||||
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/"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *ArtistService) Artist(params *ArtistParams) (*Artist, *http.Response, error) {
|
|
||||||
artist := new(Artist)
|
|
||||||
apiError := new(APIError)
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
@@ -1,26 +0,0 @@
|
|||||||
package discogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestArtistService_Artist(t *testing.T) {
|
|
||||||
expectedId := 1000
|
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
|
196
database.go
Normal file
196
database.go
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
package discogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
releasesURI = "/releases/"
|
||||||
|
artistsURI = "/artists/"
|
||||||
|
labelsURI = "/labels/"
|
||||||
|
mastersURI = "/masters/"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DatabaseService ...
|
||||||
|
type DatabaseService struct {
|
||||||
|
url string
|
||||||
|
currency string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDatabaseService(url string, currency string) *DatabaseService {
|
||||||
|
return &DatabaseService{
|
||||||
|
url: url,
|
||||||
|
currency: currency,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release serves relesase response from discogs
|
||||||
|
type Release struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
Artists []ArtistSource `json:"artists"`
|
||||||
|
DataQuality string `json:"data_quality"`
|
||||||
|
Thumb string `json:"thumb"`
|
||||||
|
Community Community `json:"community"`
|
||||||
|
Companies []Company `json:"companies"`
|
||||||
|
Country string `json:"country"`
|
||||||
|
DateAdded string `json:"date_added"`
|
||||||
|
DateChanged string `json:"date_changed"`
|
||||||
|
EstimatedWeight int `json:"estimated_weight"`
|
||||||
|
ExtraArtists []ArtistSource `json:"extraartists"`
|
||||||
|
FormatQuantity int `json:"format_quantity"`
|
||||||
|
Formats []Format `json:"formats"`
|
||||||
|
Genres []string `json:"genres"`
|
||||||
|
Identifiers []Identifier `json:"identifiers"`
|
||||||
|
Images []Image `json:"images"`
|
||||||
|
Labels []LabelSource `json:"labels"`
|
||||||
|
LowestPrice float64 `json:"lowest_price"`
|
||||||
|
MasterID int `json:"master_id"`
|
||||||
|
MasterURL string `json:"master_url"`
|
||||||
|
Notes string `json:"notes,omitempty"`
|
||||||
|
NumForSale int `json:"num_for_sale,omitempty"`
|
||||||
|
Released string `json:"released"`
|
||||||
|
ReleasedFormatted string `json:"released_formatted"`
|
||||||
|
ResourceURL string `json:"resource_url"`
|
||||||
|
// Series
|
||||||
|
Status string `json:"status"`
|
||||||
|
Styles []string `json:"styles"`
|
||||||
|
Tracklist []Track `json:"tracklist"`
|
||||||
|
URI string `json:"uri"`
|
||||||
|
Videos []Video `json:"videos"`
|
||||||
|
Year int `json:"year"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release returns release by release's ID
|
||||||
|
func (s *DatabaseService) Release(releaseID int) (*Release, error) {
|
||||||
|
params := url.Values{}
|
||||||
|
params.Set("curr_abbr", s.currency)
|
||||||
|
|
||||||
|
var release *Release
|
||||||
|
err := request(s.url+releasesURI+strconv.Itoa(releaseID), params, &release)
|
||||||
|
return release, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReleaseRating serves response for community release rating request
|
||||||
|
type ReleaseRating struct {
|
||||||
|
ID int `json:"release_id"`
|
||||||
|
Rating Rating `json:"rating"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReleaseRating retruns community release rating
|
||||||
|
func (s *DatabaseService) ReleaseRating(releaseID int) (*ReleaseRating, error) {
|
||||||
|
var rating *ReleaseRating
|
||||||
|
err := request(s.url+releasesURI+strconv.Itoa(releaseID)+"/rating", nil, &rating)
|
||||||
|
return rating, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Artist ...
|
||||||
|
type Artist struct {
|
||||||
|
Namevariations []string `json:"namevariations"`
|
||||||
|
Profile string `json:"profile"`
|
||||||
|
ReleasesURL string `json:"releases_url"`
|
||||||
|
ResourceURL string `json:"resource_url"`
|
||||||
|
URI string `json:"uri"`
|
||||||
|
URLs []string `json:"urls"`
|
||||||
|
DataQuality string `json:"data_quality"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
Images []Image `json:"images"`
|
||||||
|
Members []Member `json:"members"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Artist represents a person in the discogs database
|
||||||
|
func (s *DatabaseService) Artist(artistID int) (*Artist, error) {
|
||||||
|
var artist *Artist
|
||||||
|
err := request(s.url+artistsURI+strconv.Itoa(artistID), nil, &artist)
|
||||||
|
return artist, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArtistReleases ...
|
||||||
|
type ArtistReleases struct {
|
||||||
|
Pagination Page `json:"pagination"`
|
||||||
|
Releases []ReleaseSource `json:"releases"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArtistReleases returns a list of releases and masters associated with the artist.
|
||||||
|
func (s *DatabaseService) ArtistReleases(artistID int, pagination *Pagination) (*ArtistReleases, error) {
|
||||||
|
var releases *ArtistReleases
|
||||||
|
err := request(s.url+artistsURI+strconv.Itoa(artistID)+"/releases", pagination.params(), &releases)
|
||||||
|
return releases, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Label resource represents a label, company, recording studio, location,
|
||||||
|
// or other entity involved with artists and releases.
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Label returns a label.
|
||||||
|
func (s *DatabaseService) Label(labelID int) (*Label, error) {
|
||||||
|
var label *Label
|
||||||
|
err := request(s.url+labelsURI+strconv.Itoa(labelID), nil, &label)
|
||||||
|
return label, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// LabelReleases is a list of Releases associated with the label.
|
||||||
|
type LabelReleases struct {
|
||||||
|
Pagination Page `json:"pagination"`
|
||||||
|
Releases []ReleaseSource `json:"releases"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// LabelReleases returns a list of Releases associated with the label.
|
||||||
|
func (s *DatabaseService) LabelReleases(labelID int, pagination *Pagination) (*LabelReleases, error) {
|
||||||
|
var releases *LabelReleases
|
||||||
|
err := request(s.url+labelsURI+strconv.Itoa(labelID)+"/releases", pagination.params(), &releases)
|
||||||
|
return releases, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Master resource represents a set of similar releases.
|
||||||
|
// Masters (also known as `master releases`) have a `main release` which is often the chronologically earliest.
|
||||||
|
type Master struct {
|
||||||
|
Styles []string `json:"styles"`
|
||||||
|
Genres []string `json:"genres"`
|
||||||
|
Videos []Video `json:"videos"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
MainRelease int `json:"main_release"`
|
||||||
|
MainReleaseURL string `json:"main_release_url"`
|
||||||
|
URI string `json:"uri"`
|
||||||
|
Artists []Artist `json:"artists"`
|
||||||
|
VersionURL string `json:"version_url"`
|
||||||
|
Year int `json:"year"`
|
||||||
|
Images []Image `json:"images"`
|
||||||
|
ResourceURL string `json:"resource_url"`
|
||||||
|
Tracklist []Track `json:"tracklist"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
DataQuality string `json:"data_quality"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Master returns a master release
|
||||||
|
func (s *DatabaseService) Master(masterID int) (*Master, error) {
|
||||||
|
var master *Master
|
||||||
|
err := request(s.url+mastersURI+strconv.Itoa(masterID), nil, &master)
|
||||||
|
return master, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MasterVersions retrieves a list of all releases that are versions of this master.
|
||||||
|
type MasterVersions struct {
|
||||||
|
Pagination Page `json:"pagination"`
|
||||||
|
Versions []Version `json:"versions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MasterVersions retrieves a list of all Releases that are versions of this master
|
||||||
|
func (s *DatabaseService) MasterVersions(masterID int, pagination *Pagination) (*MasterVersions, error) {
|
||||||
|
var versions *MasterVersions
|
||||||
|
err := request(s.url+mastersURI+strconv.Itoa(masterID)+"/versions", pagination.params(), &versions)
|
||||||
|
return versions, err
|
||||||
|
}
|
31
database_test.go
Normal file
31
database_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package discogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ReleaseServer(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
if _, err := io.WriteString(w, `{"title":"Elephant Riddim"}`); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReleaseServiceRelease(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(ReleaseServer))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
expectedTitle := "Elephant Riddim"
|
||||||
|
d := initDiscogsClient(t, &Options{URL: ts.URL})
|
||||||
|
release, err := d.Database.Release(8138518)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get release: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if release.Title != expectedTitle {
|
||||||
|
t.Fatalf("release title got=%s want=%s ", expectedTitle, release.Title)
|
||||||
|
}
|
||||||
|
}
|
110
discogs.go
110
discogs.go
@@ -1,48 +1,104 @@
|
|||||||
package discogs
|
package discogs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"github.com/irlndts/go-apirequest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
discogsAPI = "https://api.discogs.com/"
|
discogsAPI = "https://api.discogs.com"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Options is a set of options to use discogs API client
|
||||||
|
type Options struct {
|
||||||
|
URL string
|
||||||
|
Currency string
|
||||||
|
UserAgent string
|
||||||
|
Token string
|
||||||
|
}
|
||||||
|
|
||||||
// Client is a Discogs client for making Discogs API requests.
|
// Client is a Discogs client for making Discogs API requests.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
api *apirequest.API
|
Database *DatabaseService
|
||||||
Release *ReleaseService
|
Search *SearchService
|
||||||
Master *MasterService
|
|
||||||
Artist *ArtistService
|
|
||||||
Label *LabelService
|
|
||||||
Search *SearchService
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var header *http.Header
|
||||||
|
|
||||||
// NewClient returns a new Client.
|
// NewClient returns a new Client.
|
||||||
func NewClient(useragent, token string) *Client {
|
func NewClient(o *Options) (*Client, error) {
|
||||||
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI).Add("User-Agent", useragent).Add("Authorization", "Discogs token="+token)
|
header = &http.Header{}
|
||||||
|
|
||||||
|
if o == nil || o.UserAgent == "" {
|
||||||
|
return nil, ErrUserAgentInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
header.Add("User-Agent", o.UserAgent)
|
||||||
|
|
||||||
|
cur, err := currency(o.Currency)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// set token, it's required for some queries like search
|
||||||
|
if o.Token != "" {
|
||||||
|
header.Add("Authorization", "Discogs token="+o.Token)
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.URL == "" {
|
||||||
|
o.URL = discogsAPI
|
||||||
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
api: base,
|
Database: newDatabaseService(o.URL, cur),
|
||||||
Artist: newArtistService(base.New()),
|
Search: newSearchService(o.URL + "/database/search"),
|
||||||
Label: newLabelService(base.New()),
|
}, nil
|
||||||
Master: newMasterService(base.New()),
|
}
|
||||||
Release: newReleaseService(base.New()),
|
|
||||||
Search: newSearchService(base.New()),
|
// currency validates currency for marketplace data.
|
||||||
|
// Defaults to the authenticated users currency. Must be one of the following:
|
||||||
|
// USD GBP EUR CAD AUD JPY CHF MXN BRL NZD SEK ZAR
|
||||||
|
func currency(c string) (string, error) {
|
||||||
|
switch c {
|
||||||
|
case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR":
|
||||||
|
return c, nil
|
||||||
|
case "":
|
||||||
|
return "USD", nil
|
||||||
|
default:
|
||||||
|
return "", ErrCurrencyNotSupported
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserAgent sets specified user agent
|
func request(path string, params url.Values, resp interface{}) error {
|
||||||
// Discogs required it
|
r, err := http.NewRequest("GET", path+"?"+params.Encode(), nil)
|
||||||
func (c *Client) UserAgent(useragent string) *Client {
|
if err != nil {
|
||||||
c.api.Set("User-Agent", useragent)
|
return err
|
||||||
return c
|
}
|
||||||
}
|
r.Header = *header
|
||||||
|
|
||||||
// Token sets tokens, it's required for some queries like search
|
client := &http.Client{}
|
||||||
func (c *Client) Token(token string) *Client {
|
response, err := client.Do(r)
|
||||||
c.api.Set("Authorization", "Discogs token="+token)
|
if err != nil {
|
||||||
return c
|
return err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
if response.StatusCode != http.StatusOK {
|
||||||
|
switch response.StatusCode {
|
||||||
|
case http.StatusUnauthorized:
|
||||||
|
return ErrUnauthorized
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown error: %s", response.Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Unmarshal(body, &resp)
|
||||||
}
|
}
|
||||||
|
@@ -9,14 +9,85 @@ const (
|
|||||||
testToken = ""
|
testToken = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
func check(t *testing.T, e error) {
|
func initDiscogsClient(t *testing.T, options *Options) *Client {
|
||||||
if e != nil {
|
if options == nil {
|
||||||
t.Error(e)
|
options = &Options{
|
||||||
|
UserAgent: testUserAgent,
|
||||||
|
Currency: "USD",
|
||||||
|
Token: testToken,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.UserAgent == "" {
|
||||||
|
options.UserAgent = testUserAgent
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := NewClient(options)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create client: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewClient(t *testing.T) {
|
||||||
|
tests := map[string]struct {
|
||||||
|
options *Options
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
"normal": {&Options{
|
||||||
|
UserAgent: testUserAgent,
|
||||||
|
Currency: "USD",
|
||||||
|
Token: "some token",
|
||||||
|
}, nil},
|
||||||
|
"incorrect user-agent": {&Options{
|
||||||
|
UserAgent: "",
|
||||||
|
Currency: "USD",
|
||||||
|
}, ErrUserAgentInvalid},
|
||||||
|
"incorrect currency": {&Options{
|
||||||
|
UserAgent: testUserAgent,
|
||||||
|
Currency: "RUR",
|
||||||
|
}, ErrCurrencyNotSupported},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name := range tests {
|
||||||
|
tt := tests[name]
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
if _, err := NewClient(tt.options); err != tt.err {
|
||||||
|
t.Errorf("err got=%s; want=%s", err, tt.err)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func assert(t *testing.T, condition bool, assertion string) {
|
func TestCurrency(t *testing.T) {
|
||||||
if !condition {
|
tests := []struct {
|
||||||
t.Errorf("Assertion failed: %v", assertion)
|
currency string
|
||||||
|
want string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{currency: "", want: "USD", err: nil},
|
||||||
|
{currency: "USD", want: "USD", err: nil},
|
||||||
|
{currency: "GBP", want: "GBP", err: nil},
|
||||||
|
{currency: "EUR", want: "EUR", err: nil},
|
||||||
|
{currency: "CAD", want: "CAD", err: nil},
|
||||||
|
{currency: "AUD", want: "AUD", err: nil},
|
||||||
|
{currency: "JPY", want: "JPY", err: nil},
|
||||||
|
{currency: "CHF", want: "CHF", err: nil},
|
||||||
|
{currency: "MXN", want: "MXN", err: nil},
|
||||||
|
{currency: "BRL", want: "BRL", err: nil},
|
||||||
|
{currency: "NZD", want: "NZD", err: nil},
|
||||||
|
{currency: "SEK", want: "SEK", err: nil},
|
||||||
|
{currency: "ZAR", want: "ZAR", err: nil},
|
||||||
|
{currency: "RUR", want: "", err: ErrCurrencyNotSupported},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
cur, err := currency(tt.currency)
|
||||||
|
if err != tt.err {
|
||||||
|
t.Errorf("#%d err got=%s; want=%s", i, err, tt.err)
|
||||||
|
}
|
||||||
|
if cur != tt.want {
|
||||||
|
t.Errorf("#%d currency got=%s; want=%s", i, cur, tt.want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
41
errors.go
41
errors.go
@@ -2,38 +2,21 @@ package discogs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// APIError represents a Discogs API Error response
|
// Error represents a Discogs API error
|
||||||
type APIError struct {
|
type Error struct {
|
||||||
Message string `json:"message"`
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e APIError) Error() string {
|
func (e *Error) Error() string {
|
||||||
if e.Message != "" {
|
return fmt.Sprintf("discogs error: %s", strings.ToLower(e.Message))
|
||||||
return fmt.Sprintf("discogs: %v", e.Message)
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty returns true if empty. Otherwise, at least 1 error message/code is
|
// APIErrors
|
||||||
// present and false is returned.
|
var (
|
||||||
func (e APIError) Empty() bool {
|
ErrUnauthorized = &Error{"authentication required"}
|
||||||
if e.Message == "" {
|
ErrCurrencyNotSupported = &Error{"currency does not supported"}
|
||||||
return true
|
ErrUserAgentInvalid = &Error{"invalid user-agent"}
|
||||||
}
|
)
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// relevantError returns any non-nil http-related error (creating the request,
|
|
||||||
// getting the response, decoding) if any. If the decoded apiError is non-zero
|
|
||||||
// the apiError is returned. Otherwise, no errors occurred, returns nil.
|
|
||||||
func relevantError(httpError error, apiError APIError) error {
|
|
||||||
if httpError != nil {
|
|
||||||
return httpError
|
|
||||||
}
|
|
||||||
if apiError.Empty() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return apiError
|
|
||||||
}
|
|
||||||
|
@@ -7,17 +7,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
d := discogs.NewClient("TestDiscogsClient/0.0.1 +http://irlndts.moscow", "")
|
d, err := discogs.NewClient(&discogs.Options{
|
||||||
|
UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
|
||||||
request := &discogs.SearchRequest{Q: "The Reggaenauts - River Rock / Thursday Kick-off", Page: 0, Per_page: 1}
|
Currency: "USD",
|
||||||
search, _, err := d.Search.Search(request)
|
Token: "",
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range search.Results {
|
release, err := d.Search.Search(discogs.SearchRequest{Q: "middle", PerPage: 3})
|
||||||
fmt.Println(r.Id, r.Title)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Printf("%+v\n", release)
|
||||||
}
|
}
|
||||||
|
58
labels.go
58
labels.go
@@ -1,58 +0,0 @@
|
|||||||
package discogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/irlndts/go-apirequest"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LabelService struct {
|
|
||||||
api *apirequest.API
|
|
||||||
}
|
|
||||||
|
|
||||||
type LabelParams struct {
|
|
||||||
Label_id string
|
|
||||||
Page int
|
|
||||||
Per_Page 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"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LabelReleases struct {
|
|
||||||
Pagination Page `json:"pagination"`
|
|
||||||
Releases []ReleaseSource `json:"releases"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func newLabelService(api *apirequest.API) *LabelService {
|
|
||||||
return &LabelService{
|
|
||||||
api: api.Path("labels/"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *LabelService) Label(params *LabelParams) (*Label, *http.Response, error) {
|
|
||||||
label := new(Label)
|
|
||||||
apiError := new(APIError)
|
|
||||||
|
|
||||||
resp, err := self.api.New().Get(params.Label_id).Receive(label, apiError)
|
|
||||||
return label, resp, relevantError(err, *apiError)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *LabelService) Releases(params *LabelParams) (*LabelReleases, *http.Response, error) {
|
|
||||||
releases := new(LabelReleases)
|
|
||||||
apiError := new(APIError)
|
|
||||||
|
|
||||||
resp, err := self.api.New().Get(params.Label_id+"/releases").QueryStruct(params).Receive(releases, apiError)
|
|
||||||
return releases, resp, relevantError(err, *apiError)
|
|
||||||
}
|
|
@@ -1,26 +0,0 @@
|
|||||||
package discogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestLabelService_Label(t *testing.T) {
|
|
||||||
expectedId := 1000
|
|
||||||
|
|
||||||
d := NewClient(testUserAgent, testToken)
|
|
||||||
label, _, err := d.Label.Label(&LabelParams{Label_id: "1000"})
|
|
||||||
|
|
||||||
check(t, err)
|
|
||||||
assert(t, label.Id == expectedId, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedId, label.Id))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLabelService_Releases(t *testing.T) {
|
|
||||||
expectedId := "Cha Cha Twist"
|
|
||||||
|
|
||||||
d := NewClient(testUserAgent, testToken)
|
|
||||||
label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"})
|
|
||||||
|
|
||||||
check(t, err)
|
|
||||||
assert(t, label.Releases[0].Title == expectedId, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedId, label.Releases[0].Title))
|
|
||||||
}
|
|
63
masters.go
63
masters.go
@@ -1,63 +0,0 @@
|
|||||||
package discogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/irlndts/go-apirequest"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MasterService struct {
|
|
||||||
api *apirequest.API
|
|
||||||
}
|
|
||||||
|
|
||||||
type MasterParams struct {
|
|
||||||
Master_id string
|
|
||||||
}
|
|
||||||
|
|
||||||
type MasterVersionParams struct {
|
|
||||||
Master_id string
|
|
||||||
Page int
|
|
||||||
Per_page int
|
|
||||||
}
|
|
||||||
|
|
||||||
type Master struct {
|
|
||||||
Styles []string `json:"styles"`
|
|
||||||
Genres []string `json:"genres"`
|
|
||||||
Videos []Video `json:"videos"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Main_release int `json:"main_release"`
|
|
||||||
Main_release_url string `json:"main_release_url"`
|
|
||||||
Uri string `json:"uri"`
|
|
||||||
Artists []Artist `json:"artists"`
|
|
||||||
Version_url string `json:"version_url"`
|
|
||||||
Year int `json:"year"`
|
|
||||||
Images []Image `json:"images"`
|
|
||||||
Resource_url string `json:"resource_url"`
|
|
||||||
Tracklist []Track `json:"tracklist"`
|
|
||||||
Id int `json:"id"`
|
|
||||||
Data_quality string `json:"data_quality"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type MasterVersions struct {
|
|
||||||
Pagination Page `json:"pagination"`
|
|
||||||
Versions []Version `json:"versions"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMasterService(api *apirequest.API) *MasterService {
|
|
||||||
return &MasterService{
|
|
||||||
api: api.Path("masters/"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *MasterService) Master(params *MasterParams) (*Master, *http.Response, error) {
|
|
||||||
master := new(Master)
|
|
||||||
apiError := new(APIError)
|
|
||||||
resp, err := self.api.New().Get(params.Master_id).Receive(master, apiError)
|
|
||||||
return master, resp, relevantError(err, *apiError)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *MasterService) Versions(params *MasterVersionParams) (*MasterVersions, *http.Response, error) {
|
|
||||||
versions := new(MasterVersions)
|
|
||||||
apiError := new(APIError)
|
|
||||||
resp, err := self.api.New().Get(params.Master_id+"/versions").QueryStruct(params).Receive(versions, apiError)
|
|
||||||
return versions, resp, relevantError(err, *apiError)
|
|
||||||
}
|
|
@@ -1,26 +0,0 @@
|
|||||||
package discogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMasterService_Master(t *testing.T) {
|
|
||||||
expectedTitle := "Elephant Riddim"
|
|
||||||
|
|
||||||
d := NewClient(testUserAgent, testToken)
|
|
||||||
master, _, err := d.Master.Master(&MasterParams{Master_id: "960657"})
|
|
||||||
|
|
||||||
check(t, err)
|
|
||||||
assert(t, master.Title == expectedTitle, fmt.Sprintf("master.Title looked for %s, and received %s ", expectedTitle, master.Title))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMasterService_Versions(t *testing.T) {
|
|
||||||
expectedTitle := "Stardiver"
|
|
||||||
|
|
||||||
d := NewClient(testUserAgent, testToken)
|
|
||||||
versions, _, err := d.Master.Versions(&MasterVersionParams{Master_id: "1000", Page: 1, Per_page: 1})
|
|
||||||
|
|
||||||
check(t, err)
|
|
||||||
assert(t, versions.Versions[0].Title == expectedTitle, fmt.Sprintf("master.Title looked for %s, and received %s ", expectedTitle, versions.Versions[0].Title))
|
|
||||||
}
|
|
176
models.go
176
models.go
@@ -1,71 +1,86 @@
|
|||||||
package discogs
|
package discogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Video ...
|
||||||
type Video struct {
|
type Video struct {
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Duration int `json:"duration"`
|
Duration int `json:"duration"`
|
||||||
Embed bool `json:"embed"`
|
Embed bool `json:"embed"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Uri string `json:"uri"`
|
URI string `json:"uri"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ArtistSource ...
|
||||||
type ArtistSource struct {
|
type ArtistSource struct {
|
||||||
Anv string `json:"anv"`
|
Anv string `json:"anv"`
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
Join string `json:"join"`
|
Join string `json:"join"`
|
||||||
Name string `json:"name:`
|
Name string `json:"name"`
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
Tracks string `json:"tracks"`
|
Tracks string `json:"tracks"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Image ...
|
||||||
type Image struct {
|
type Image struct {
|
||||||
Height int `json:"height"`
|
Height int `json:"height"`
|
||||||
Width int `json:"width"`
|
Width int `json:"width"`
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Uri string `json:"uri"`
|
URI string `json:"uri"`
|
||||||
Uri150 string `json:"uri150"`
|
URI150 string `json:"uri150"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Track ...
|
||||||
type Track struct {
|
type Track struct {
|
||||||
Duration string `json:"duration"`
|
Duration string `json:"duration"`
|
||||||
Position string `json:"position"`
|
Position string `json:"position"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Type string `json:"type_"`
|
Type string `json:"type_"`
|
||||||
Extraartists []ArtistSource `json:"extraartists"`
|
Extraartists []ArtistSource `json:"extraartists"`
|
||||||
|
Artists []ArtistSource `json:"artists"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LabelSource ...
|
||||||
type LabelSource struct {
|
type LabelSource struct {
|
||||||
Catno string `json:"catno"`
|
Catno string `json:"catno"`
|
||||||
Entity_type string `json:"entity_type"`
|
EntityType string `json:"entity_type"`
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Identifier ...
|
||||||
type Identifier struct {
|
type Identifier struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format ...
|
||||||
type Format struct {
|
type Format struct {
|
||||||
Descriptions []string `json:"descriptions"`
|
Descriptions []string `json:"descriptions"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Qty string `json:"qty"`
|
Qty string `json:"qty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Company ...
|
||||||
type Company struct {
|
type Company struct {
|
||||||
Catno string `json:"catno"`
|
Catno string `json:"catno"`
|
||||||
Entity_type string `json:"entity_type"`
|
EntityType string `json:"entity_type"`
|
||||||
Entity_type_name string `json:"entity_type_name"`
|
EntityTypeName string `json:"entity_type_name"`
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Community ...
|
||||||
type Community struct {
|
type Community struct {
|
||||||
Contributors []Contributor `json:"contributors"`
|
Contributors []Contributor `json:"contributors"`
|
||||||
Data_quality string `json:"string"`
|
DataQuality string `json:"string"`
|
||||||
Have int `json:"have"`
|
Have int `json:"have"`
|
||||||
Rating Rating `json:"rating"`
|
Rating Rating `json:"rating"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
@@ -73,71 +88,102 @@ type Community struct {
|
|||||||
Want int `json:"want"`
|
Want int `json:"want"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Submitter ...
|
||||||
type Submitter struct {
|
type Submitter struct {
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rating ...
|
||||||
type Rating struct {
|
type Rating struct {
|
||||||
Average float32 `json:"average"`
|
Average float32 `json:"average"`
|
||||||
Count int `json:"count"`
|
Count int `json:"count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Contributor ...
|
||||||
type Contributor struct {
|
type Contributor struct {
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Page ...
|
||||||
type Page struct {
|
type Page struct {
|
||||||
Per_page int `json:"per_page"`
|
PerPage int `json:"per_page"`
|
||||||
Items int `json:"items"`
|
Items int `json:"items"`
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
Urls URLS `json:"urls"`
|
URLs URLsList `json:"urls"`
|
||||||
Pages int `json:"pages"`
|
Pages int `json:"pages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type URLS struct {
|
// URLsList ...
|
||||||
|
type URLsList struct {
|
||||||
Last string `json:"last"`
|
Last string `json:"last"`
|
||||||
Next string `json:"next"`
|
Next string `json:"next"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Version ...
|
||||||
type Version struct {
|
type Version struct {
|
||||||
Catno string `json:"catno"`
|
Catno string `json:"catno"`
|
||||||
Country string `json:"country"`
|
Country string `json:"country"`
|
||||||
Format string `json:"format"`
|
Format string `json:"format"`
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
Released string `json:"released"`
|
Released string `json:"released"`
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Thumb string `json:"thumb"`
|
Thumb string `json:"thumb"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Member ...
|
||||||
type Member struct {
|
type Member struct {
|
||||||
Active bool `json:"active"`
|
Active bool `json:"active"`
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sublable ...
|
||||||
type Sublable struct {
|
type Sublable struct {
|
||||||
Resource_url string `json:"url"`
|
ResourceURL string `json:"url"`
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReleaseSource ...
|
||||||
type ReleaseSource struct {
|
type ReleaseSource struct {
|
||||||
Artist string `json:"artist"`
|
Artist string `json:"artist"`
|
||||||
Catno string `json:"catno"`
|
Catno string `json:"catno"`
|
||||||
Format string `json:"format"`
|
Format string `json:"format"`
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
Resource_url string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Thumb string `json:"thumb"`
|
Thumb string `json:"thumb"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Year int `json:"year"`
|
Year int `json:"year"`
|
||||||
Main_release int `json:"main_release"`
|
MainRelease int `json:"main_release"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pagination ...
|
||||||
|
type Pagination struct {
|
||||||
|
Sort string // year, title, format
|
||||||
|
SortOrder string // asc, desc
|
||||||
|
Page int
|
||||||
|
PerPage int
|
||||||
|
}
|
||||||
|
|
||||||
|
// toParams converts pagaination params to request values
|
||||||
|
func (p *Pagination) params() url.Values {
|
||||||
|
if p == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
params := url.Values{}
|
||||||
|
params.Set("sort", p.Sort)
|
||||||
|
params.Set("sort_order", p.SortOrder)
|
||||||
|
params.Set("page", strconv.Itoa(p.Page))
|
||||||
|
params.Set("per_page", strconv.Itoa(p.PerPage))
|
||||||
|
return params
|
||||||
}
|
}
|
||||||
|
61
releases.go
61
releases.go
@@ -1,61 +0,0 @@
|
|||||||
package discogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/irlndts/go-apirequest"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ReleaseService struct {
|
|
||||||
api *apirequest.API
|
|
||||||
}
|
|
||||||
|
|
||||||
type ReleaseParams struct {
|
|
||||||
Release_id string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Release struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
Id int `json:"id"`
|
|
||||||
Artists []ArtistSource `json:"artists"`
|
|
||||||
Data_quality string `json:"data_quality"`
|
|
||||||
Thumb string `json:"thumb"`
|
|
||||||
Community Community `json:"community"`
|
|
||||||
Companies []Company `json:"companies"`
|
|
||||||
Country string `json:"country"`
|
|
||||||
Date_added string `json:"date_added"`
|
|
||||||
Date_changed string `json:"date_changed"`
|
|
||||||
Estimated_weight int `json:"estimated_weight"`
|
|
||||||
Extraartists []ArtistSource `json:"extraartists"`
|
|
||||||
Format_quantity int `json:"format_quantity"`
|
|
||||||
Formats []Format `json:"formats"`
|
|
||||||
Genres []string `json:"genres"`
|
|
||||||
Identifiers []Identifier `json:"identifiers"`
|
|
||||||
Images []Image `json:"images"`
|
|
||||||
Labels []LabelSource `json:"labels"`
|
|
||||||
Master_id int `json:"master_id"`
|
|
||||||
Master_url string `json:"master_url"`
|
|
||||||
Notes string `josn:"notes"`
|
|
||||||
Released string `json:"released"`
|
|
||||||
Released_formatted string `json:"released_formatted"`
|
|
||||||
Resource_url string `json:"resource_url"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
Styles []string `json:"styles"`
|
|
||||||
Tracklist []Track `json:"tracklist"`
|
|
||||||
Uri string `json:"uri"`
|
|
||||||
Videos []Video `json:"videos"`
|
|
||||||
Year int `json:"year"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func newReleaseService(api *apirequest.API) *ReleaseService {
|
|
||||||
return &ReleaseService{
|
|
||||||
api: api.Path("releases/"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *ReleaseService) Release(params *ReleaseParams) (*Release, *http.Response, error) {
|
|
||||||
release := new(Release)
|
|
||||||
apiError := new(APIError)
|
|
||||||
|
|
||||||
resp, err := self.api.New().Get(params.Release_id).Receive(release, apiError)
|
|
||||||
return release, resp, relevantError(err, *apiError)
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
package discogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestReleaseService_Release(t *testing.T) {
|
|
||||||
expectedTitle := "Elephant Riddim"
|
|
||||||
|
|
||||||
d := NewClient(testUserAgent, testToken)
|
|
||||||
release, _, err := d.Release.Release(&ReleaseParams{Release_id: "8138518"})
|
|
||||||
|
|
||||||
check(t, err)
|
|
||||||
assert(t, release.Title == expectedTitle, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedTitle, release.Title))
|
|
||||||
}
|
|
190
search.go
190
search.go
@@ -1,71 +1,147 @@
|
|||||||
package discogs
|
package discogs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"github.com/irlndts/go-apirequest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SearchService ...
|
||||||
type SearchService struct {
|
type SearchService struct {
|
||||||
api *apirequest.API
|
url string
|
||||||
}
|
}
|
||||||
|
|
||||||
type SearchRequest struct {
|
func newSearchService(url string) *SearchService {
|
||||||
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{
|
return &SearchService{
|
||||||
api: api.Path("database/search"),
|
url: url,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *SearchService) Search(params *SearchRequest) (*Search, *http.Response, error) {
|
// SearchRequest describes search request
|
||||||
search := new(Search)
|
type SearchRequest struct {
|
||||||
apiError := new(APIError)
|
Q string // search query
|
||||||
|
Type string // one of release, master, artist, label
|
||||||
|
Title string // search by combined “Artist Name - Release Title” title field
|
||||||
|
ReleaseTitle string // search release titles
|
||||||
|
Credit string // search release credits
|
||||||
|
Artist string // search artist names
|
||||||
|
Anv string // search artist ANV
|
||||||
|
Label string // search label names
|
||||||
|
Genre string // search genres
|
||||||
|
Style string // search styles
|
||||||
|
Country string // search release country
|
||||||
|
Year string // search release year
|
||||||
|
Format string // search formats
|
||||||
|
Catno string // search catalog number
|
||||||
|
Barcode string // search barcodes
|
||||||
|
Track string // search track titles
|
||||||
|
Submitter string // search submitter username
|
||||||
|
Contributor string // search contributor usernames
|
||||||
|
|
||||||
resp, err := self.api.New().QueryStruct(params).Receive(search, apiError)
|
Page int
|
||||||
return search, resp, relevantError(err, *apiError)
|
PerPage int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SearchRequest) params() url.Values {
|
||||||
|
if r == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
params := url.Values{}
|
||||||
|
|
||||||
|
if r.Q != "" {
|
||||||
|
params.Set("q", r.Q)
|
||||||
|
}
|
||||||
|
if r.Type != "" {
|
||||||
|
params.Set("type", r.Type)
|
||||||
|
}
|
||||||
|
if r.Title != "" {
|
||||||
|
params.Set("title", r.Title)
|
||||||
|
}
|
||||||
|
if r.ReleaseTitle != "" {
|
||||||
|
params.Set("release_title", r.ReleaseTitle)
|
||||||
|
}
|
||||||
|
if r.Credit != "" {
|
||||||
|
params.Set("credit", r.Credit)
|
||||||
|
}
|
||||||
|
if r.Artist != "" {
|
||||||
|
params.Set("artist", r.Artist)
|
||||||
|
}
|
||||||
|
if r.Anv != "" {
|
||||||
|
params.Set("anv", r.Anv)
|
||||||
|
}
|
||||||
|
if r.Label != "" {
|
||||||
|
params.Set("label", r.Label)
|
||||||
|
}
|
||||||
|
if r.Genre != "" {
|
||||||
|
params.Set("genre", r.Genre)
|
||||||
|
}
|
||||||
|
if r.Style != "" {
|
||||||
|
params.Set("style", r.Style)
|
||||||
|
}
|
||||||
|
if r.Country != "" {
|
||||||
|
params.Set("country", r.Country)
|
||||||
|
}
|
||||||
|
if r.Year != "" {
|
||||||
|
params.Set("year", r.Year)
|
||||||
|
}
|
||||||
|
if r.Format != "" {
|
||||||
|
params.Set("format", r.Format)
|
||||||
|
}
|
||||||
|
if r.Catno != "" {
|
||||||
|
params.Set("catno", r.Catno)
|
||||||
|
}
|
||||||
|
if r.Barcode != "" {
|
||||||
|
params.Set("barcode", r.Barcode)
|
||||||
|
}
|
||||||
|
if r.Track != "" {
|
||||||
|
params.Set("track", r.Track)
|
||||||
|
}
|
||||||
|
if r.Submitter != "" {
|
||||||
|
params.Set("submitter", r.Submitter)
|
||||||
|
}
|
||||||
|
if r.Contributor != "" {
|
||||||
|
params.Set("contributor", r.Contributor)
|
||||||
|
}
|
||||||
|
|
||||||
|
params.Set("page", strconv.Itoa(r.Page))
|
||||||
|
if r.PerPage != 0 {
|
||||||
|
params.Set("per_page", strconv.Itoa(r.PerPage))
|
||||||
|
}
|
||||||
|
return params
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search describes search response
|
||||||
|
type Search struct {
|
||||||
|
Pagination Page `json:"pagination"`
|
||||||
|
Results []Result `json:"results,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result describes a part of search result
|
||||||
|
type Result struct {
|
||||||
|
Style []string `json:"style,omitempty"`
|
||||||
|
Thumb string `json:"thumb,omitempty"`
|
||||||
|
CoverImage string `json:"cover_image,omitempty"`
|
||||||
|
Title string `json:"title,omitempty"`
|
||||||
|
Country string `json:"country,omitempty"`
|
||||||
|
Format []string `json:"format,omitempty"`
|
||||||
|
URI string `json:"uri,omitempty"`
|
||||||
|
Community Community `json:"community,omitempty"`
|
||||||
|
Label []string `json:"label,omitempty"`
|
||||||
|
Catno string `json:"catno,omitempty"`
|
||||||
|
Year string `json:"year,omitempty"`
|
||||||
|
Genre []string `json:"genre,omitempty"`
|
||||||
|
ResourceURL string `json:"resource_url,omitempty"`
|
||||||
|
Type string `json:"type,omitempty"`
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
MasterID int `json:"master_id,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search makes search request to discogs.
|
||||||
|
// Issue a search query to our database. This endpoint accepts pagination parameters.
|
||||||
|
// Authentication (as any user) is required.
|
||||||
|
// https://www.discogs.com/developers/#page:database,header:database-search
|
||||||
|
func (s *SearchService) Search(req SearchRequest) (*Search, error) {
|
||||||
|
var search *Search
|
||||||
|
err := request(s.url, req.params(), &search)
|
||||||
|
return search, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user