Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bcf5c89215 | ||
![]() |
417d6d51e6 | ||
![]() |
54c186c94e | ||
![]() |
b61a3c137a | ||
![]() |
e287c7d9b3 | ||
![]() |
f499b48b2f | ||
![]() |
d987189090 |
@@ -1,3 +1,3 @@
|
||||
language: go
|
||||
go:
|
||||
- "1.12.5"
|
||||
- "1.14"
|
||||
|
@@ -4,6 +4,8 @@
|
||||
|
||||
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.
|
||||
|
||||
The lib is under MIT but be sure you are familiar with [Discogs API Terms of Use](https://support.discogs.com/hc/en-us/articles/360009334593-API-Terms-of-Use).
|
||||
|
||||
### Feauteres
|
||||
* Database
|
||||
* [Releases](#releases)
|
||||
@@ -31,10 +33,11 @@ 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
|
||||
client, err := discogs.NewClient(&discogs.Options{
|
||||
client, err := discogs.New(&discogs.Options{
|
||||
UserAgent: "Some Name",
|
||||
Currency: "EUR", // optional, "USD" (default), "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR" are allowed
|
||||
Token: "Some Token", // optional
|
||||
URL: "https://api.discogs.com", // optional
|
||||
})
|
||||
```
|
||||
|
||||
@@ -85,5 +88,3 @@ Example
|
||||
fmt.Println(r.Title)
|
||||
}
|
||||
```
|
||||
|
||||
etc.
|
||||
|
64
database.go
64
database.go
@@ -30,6 +30,7 @@ type Release struct {
|
||||
Title string `json:"title"`
|
||||
ID int `json:"id"`
|
||||
Artists []ArtistSource `json:"artists"`
|
||||
ArtistsSort string `json:"artists_sort"`
|
||||
DataQuality string `json:"data_quality"`
|
||||
Thumb string `json:"thumb"`
|
||||
Community Community `json:"community"`
|
||||
@@ -53,13 +54,13 @@ type Release struct {
|
||||
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"`
|
||||
Series []Series `json:"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
|
||||
@@ -85,18 +86,23 @@ func (s *DatabaseService) ReleaseRating(releaseID int) (*ReleaseRating, error) {
|
||||
return rating, err
|
||||
}
|
||||
|
||||
// Artist ...
|
||||
// Artist resource represents a person in the Discogs database
|
||||
// who contributed to a Release in some capacity.
|
||||
// More information https://www.discogs.com/developers#page:database,header:database-artist
|
||||
type Artist struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Realname string `json:"realname"`
|
||||
Members []Member `json:"members,omitempty"`
|
||||
Aliases []Alias `json:"aliases,omitempty"`
|
||||
Namevariations []string `json:"namevariations"`
|
||||
Images []Image `json:"images"`
|
||||
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
|
||||
@@ -157,22 +163,28 @@ func (s *DatabaseService) LabelReleases(labelID int, pagination *Pagination) (*L
|
||||
|
||||
// Master resource represents a set of similar releases.
|
||||
// Masters (also known as `master releases`) have a `main release` which is often the chronologically earliest.
|
||||
// More information https://www.discogs.com/developers#page:database,header:database-master-release
|
||||
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"`
|
||||
ID int `json:"id"`
|
||||
Styles []string `json:"styles"`
|
||||
Genres []string `json:"genres"`
|
||||
Title string `json:"title"`
|
||||
Year int `json:"year"`
|
||||
Tracklist []Track `json:"tracklist"`
|
||||
Notes string `json:"notes"`
|
||||
Artists []ArtistSource `json:"artists"`
|
||||
Images []Image `json:"images"`
|
||||
Videos []Video `json:"videos"`
|
||||
NumForSale int `json:"num_for_sale"`
|
||||
LowestPrice float64 `json:"lowest_price"`
|
||||
URI string `json:"uri"`
|
||||
MainRelease int `json:"main_release"`
|
||||
MainReleaseURL string `json:"main_release_url"`
|
||||
MostRecentRelease int `json:"most_recent_release"`
|
||||
MostRecentReleaseURL string `json:"most_recent_release_url"`
|
||||
VersionsURL string `json:"versions_url"`
|
||||
ResourceURL string `json:"resource_url"`
|
||||
DataQuality string `json:"data_quality"`
|
||||
}
|
||||
|
||||
// Master returns a master release
|
||||
|
@@ -1,31 +1,108 @@
|
||||
package discogs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
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 DatabaseServer(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.URL.Path {
|
||||
case "/releases/8138518":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := io.WriteString(w, releaseJson); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
case "/masters/718441":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := io.WriteString(w, masterJson); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
case "/artists/38661":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := io.WriteString(w, artistJson); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReleaseServiceRelease(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(ReleaseServer))
|
||||
func compareJson(t *testing.T, got, want string) {
|
||||
var g, w interface{}
|
||||
if err := json.Unmarshal([]byte(got), &g); err != nil {
|
||||
log.Fatalf("failed to unmarshal json: %s", err)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(want), &w); err != nil {
|
||||
log.Fatalf("failed to unmarshal json: %s", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(g, w); diff != "" {
|
||||
t.Errorf("(-want +got)\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabaseServiceRelease(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(DatabaseServer))
|
||||
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)
|
||||
json, err := json.Marshal(release)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal release: %s", err)
|
||||
}
|
||||
|
||||
compareJson(t, string(json), releaseJson)
|
||||
}
|
||||
|
||||
func TestDatabaseServiceMaster(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(DatabaseServer))
|
||||
defer ts.Close()
|
||||
|
||||
d := initDiscogsClient(t, &Options{URL: ts.URL})
|
||||
master, err := d.Database.Master(718441)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get master: %s", err)
|
||||
}
|
||||
|
||||
json, err := json.Marshal(master)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal release: %s", err)
|
||||
}
|
||||
compareJson(t, string(json), masterJson)
|
||||
}
|
||||
|
||||
func TestDatabaseServiceArtist(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(DatabaseServer))
|
||||
defer ts.Close()
|
||||
|
||||
d := initDiscogsClient(t, &Options{URL: ts.URL})
|
||||
artist, err := d.Database.Artist(38661)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get master: %s", err)
|
||||
}
|
||||
|
||||
json, err := json.Marshal(artist)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal artist: %s", err)
|
||||
}
|
||||
compareJson(t, string(json), artistJson)
|
||||
}
|
||||
|
20
discogs.go
20
discogs.go
@@ -14,22 +14,26 @@ const (
|
||||
|
||||
// Options is a set of options to use discogs API client
|
||||
type Options struct {
|
||||
URL string
|
||||
Currency string
|
||||
// Discogs API endpoint (optional).
|
||||
URL string
|
||||
// Currency to use (optional, default is USD).
|
||||
Currency string
|
||||
// UserAgent to to call discogs api with.
|
||||
UserAgent string
|
||||
Token string
|
||||
// Token provided by discogs (optional).
|
||||
Token string
|
||||
}
|
||||
|
||||
// Client is a Discogs client for making Discogs API requests.
|
||||
type Client struct {
|
||||
// Discogs is a Discogs' client for making Discogs API requests.
|
||||
type Discogs struct {
|
||||
Database *DatabaseService
|
||||
Search *SearchService
|
||||
}
|
||||
|
||||
var header *http.Header
|
||||
|
||||
// NewClient returns a new Client.
|
||||
func NewClient(o *Options) (*Client, error) {
|
||||
// New returns a new discogs API client.
|
||||
func New(o *Options) (*Discogs, error) {
|
||||
header = &http.Header{}
|
||||
|
||||
if o == nil || o.UserAgent == "" {
|
||||
@@ -52,7 +56,7 @@ func NewClient(o *Options) (*Client, error) {
|
||||
o.URL = discogsAPI
|
||||
}
|
||||
|
||||
return &Client{
|
||||
return &Discogs{
|
||||
Database: newDatabaseService(o.URL, cur),
|
||||
Search: newSearchService(o.URL + "/database/search"),
|
||||
}, nil
|
||||
|
@@ -9,7 +9,7 @@ const (
|
||||
testToken = ""
|
||||
)
|
||||
|
||||
func initDiscogsClient(t *testing.T, options *Options) *Client {
|
||||
func initDiscogsClient(t *testing.T, options *Options) *Discogs {
|
||||
if options == nil {
|
||||
options = &Options{
|
||||
UserAgent: testUserAgent,
|
||||
@@ -22,7 +22,7 @@ func initDiscogsClient(t *testing.T, options *Options) *Client {
|
||||
options.UserAgent = testUserAgent
|
||||
}
|
||||
|
||||
client, err := NewClient(options)
|
||||
client, err := New(options)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create client: %s", err)
|
||||
}
|
||||
@@ -30,7 +30,7 @@ func initDiscogsClient(t *testing.T, options *Options) *Client {
|
||||
return client
|
||||
}
|
||||
|
||||
func TestNewClient(t *testing.T) {
|
||||
func TestNew(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
options *Options
|
||||
err error
|
||||
@@ -53,7 +53,7 @@ func TestNewClient(t *testing.T) {
|
||||
for name := range tests {
|
||||
tt := tests[name]
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if _, err := NewClient(tt.options); err != tt.err {
|
||||
if _, err := New(tt.options); err != tt.err {
|
||||
t.Errorf("err got=%s; want=%s", err, tt.err)
|
||||
}
|
||||
})
|
||||
|
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
d, err := discogs.NewClient(&discogs.Options{
|
||||
d, err := discogs.New(&discogs.Options{
|
||||
UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
|
||||
Currency: "USD",
|
||||
Token: "",
|
||||
@@ -17,10 +17,10 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
release, err := d.Search.Search(discogs.SearchRequest{Q: "middle", PerPage: 3})
|
||||
master, err := d.Database.Master(718441)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%+v\n", release)
|
||||
fmt.Printf("%+v\n", master)
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@@ -1,3 +1,5 @@
|
||||
module github.com/irlndts/go-discogs
|
||||
|
||||
go 1.12
|
||||
go 1.14
|
||||
|
||||
require github.com/google/go-cmp v0.4.0
|
||||
|
4
go.sum
4
go.sum
@@ -0,0 +1,4 @@
|
||||
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
40
models.go
40
models.go
@@ -14,6 +14,17 @@ type Video struct {
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
|
||||
// Series ...
|
||||
type Series struct {
|
||||
Catno string `json:"catno"`
|
||||
EntityType string `json:"entity_type"`
|
||||
EntityTypeName string `json:"entity_type_name"`
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ResourceURL string `json:"resource_url"`
|
||||
ThumbnailURL string `json:"thumbnail_url,omitempty"`
|
||||
}
|
||||
|
||||
// ArtistSource ...
|
||||
type ArtistSource struct {
|
||||
Anv string `json:"anv"`
|
||||
@@ -41,23 +52,25 @@ type Track struct {
|
||||
Position string `json:"position"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type_"`
|
||||
Extraartists []ArtistSource `json:"extraartists"`
|
||||
Artists []ArtistSource `json:"artists"`
|
||||
Extraartists []ArtistSource `json:"extraartists,omitempty"`
|
||||
Artists []ArtistSource `json:"artists,omitempty"`
|
||||
}
|
||||
|
||||
// LabelSource ...
|
||||
type LabelSource struct {
|
||||
Catno string `json:"catno"`
|
||||
EntityType string `json:"entity_type"`
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ResourceURL string `json:"resource_url"`
|
||||
Catno string `json:"catno"`
|
||||
EntityType string `json:"entity_type"`
|
||||
EntityTypeName string `json:"entity_type_name"`
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ResourceURL string `json:"resource_url"`
|
||||
}
|
||||
|
||||
// Identifier ...
|
||||
type Identifier struct {
|
||||
Type string `json:"type"`
|
||||
Value string `json:"value"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Type string `json:"type"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// Format ...
|
||||
@@ -80,7 +93,7 @@ type Company struct {
|
||||
// Community ...
|
||||
type Community struct {
|
||||
Contributors []Contributor `json:"contributors"`
|
||||
DataQuality string `json:"string"`
|
||||
DataQuality string `json:"data_quality"`
|
||||
Have int `json:"have"`
|
||||
Rating Rating `json:"rating"`
|
||||
Status string `json:"status"`
|
||||
@@ -143,6 +156,13 @@ type Member struct {
|
||||
ResourceURL string `json:"resource_url"`
|
||||
}
|
||||
|
||||
// Alias ...
|
||||
type Alias struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ResourceURL string `json:"resource_url"`
|
||||
}
|
||||
|
||||
// Sublable ...
|
||||
type Sublable struct {
|
||||
ResourceURL string `json:"url"`
|
||||
|
@@ -133,6 +133,7 @@ type Result struct {
|
||||
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.
|
||||
|
9
testing_data.go
Normal file
9
testing_data.go
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user