Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bcf5c89215 | ||
![]() |
417d6d51e6 | ||
![]() |
54c186c94e | ||
![]() |
b61a3c137a | ||
![]() |
e287c7d9b3 | ||
![]() |
f499b48b2f | ||
![]() |
d987189090 | ||
![]() |
77c264c993 | ||
![]() |
84a3ecd4c5 | ||
![]() |
f9bfcf8fba | ||
![]() |
664896b3dd | ||
![]() |
3c48a3e6d4 | ||
![]() |
271bbe8b83 | ||
![]() |
35dfbe02d1 |
@@ -1,3 +1,3 @@
|
|||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- "1.10"
|
- "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.
|
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
|
### Feauteres
|
||||||
* Database
|
* Database
|
||||||
* [Releases](#releases)
|
* [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).
|
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:
|
This is token way example:
|
||||||
```go
|
```go
|
||||||
client, err := discogs.NewClient(&discogs.Options{
|
client, err := discogs.New(&discogs.Options{
|
||||||
UserAgent: "Some Name",
|
UserAgent: "Some Name",
|
||||||
Currency: "EUR", // optional, "USD" (default), "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR" are allowed
|
Currency: "EUR", // optional, "USD" (default), "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR" are allowed
|
||||||
Token: "Some Token", // optional
|
Token: "Some Token", // optional
|
||||||
|
URL: "https://api.discogs.com", // optional
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -79,11 +82,9 @@ type SearchRequest struct {
|
|||||||
Example
|
Example
|
||||||
```go
|
```go
|
||||||
request := discogs.SearchRequest{Artist: "reggaenauts", ReleaseTitle: "river rock", Page: 0, PerPage: 1}
|
request := discogs.SearchRequest{Artist: "reggaenauts", ReleaseTitle: "river rock", Page: 0, PerPage: 1}
|
||||||
search, _ := 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)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
etc.
|
|
||||||
|
88
database.go
88
database.go
@@ -30,6 +30,7 @@ type Release struct {
|
|||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Artists []ArtistSource `json:"artists"`
|
Artists []ArtistSource `json:"artists"`
|
||||||
|
ArtistsSort string `json:"artists_sort"`
|
||||||
DataQuality string `json:"data_quality"`
|
DataQuality string `json:"data_quality"`
|
||||||
Thumb string `json:"thumb"`
|
Thumb string `json:"thumb"`
|
||||||
Community Community `json:"community"`
|
Community Community `json:"community"`
|
||||||
@@ -53,7 +54,7 @@ type Release struct {
|
|||||||
Released string `json:"released"`
|
Released string `json:"released"`
|
||||||
ReleasedFormatted string `json:"released_formatted"`
|
ReleasedFormatted string `json:"released_formatted"`
|
||||||
ResourceURL string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
// Series
|
Series []Series `json:"series"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Styles []string `json:"styles"`
|
Styles []string `json:"styles"`
|
||||||
Tracklist []Track `json:"tracklist"`
|
Tracklist []Track `json:"tracklist"`
|
||||||
@@ -68,11 +69,8 @@ func (s *DatabaseService) Release(releaseID int) (*Release, error) {
|
|||||||
params.Set("curr_abbr", s.currency)
|
params.Set("curr_abbr", s.currency)
|
||||||
|
|
||||||
var release *Release
|
var release *Release
|
||||||
if err := request(s.url+releasesURI+strconv.Itoa(releaseID), params, &release); err != nil {
|
err := request(s.url+releasesURI+strconv.Itoa(releaseID), params, &release)
|
||||||
return nil, err
|
return release, err
|
||||||
}
|
|
||||||
|
|
||||||
return release, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReleaseRating serves response for community release rating request
|
// ReleaseRating serves response for community release rating request
|
||||||
@@ -84,34 +82,34 @@ type ReleaseRating struct {
|
|||||||
// ReleaseRating retruns community release rating
|
// ReleaseRating retruns community release rating
|
||||||
func (s *DatabaseService) ReleaseRating(releaseID int) (*ReleaseRating, error) {
|
func (s *DatabaseService) ReleaseRating(releaseID int) (*ReleaseRating, error) {
|
||||||
var rating *ReleaseRating
|
var rating *ReleaseRating
|
||||||
if err := request(s.url+releasesURI+strconv.Itoa(releaseID)+"/rating", nil, &rating); err != nil {
|
err := request(s.url+releasesURI+strconv.Itoa(releaseID)+"/rating", nil, &rating)
|
||||||
return nil, err
|
return rating, err
|
||||||
}
|
|
||||||
|
|
||||||
return rating, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
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"`
|
Namevariations []string `json:"namevariations"`
|
||||||
|
Images []Image `json:"images"`
|
||||||
Profile string `json:"profile"`
|
Profile string `json:"profile"`
|
||||||
ReleasesURL string `json:"releases_url"`
|
ReleasesURL string `json:"releases_url"`
|
||||||
ResourceURL string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
URI string `json:"uri"`
|
URI string `json:"uri"`
|
||||||
URLs []string `json:"urls"`
|
URLs []string `json:"urls"`
|
||||||
DataQuality string `json:"data_quality"`
|
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
|
// Artist represents a person in the discogs database
|
||||||
func (s *DatabaseService) Artist(artistID int) (*Artist, error) {
|
func (s *DatabaseService) Artist(artistID int) (*Artist, error) {
|
||||||
var artist *Artist
|
var artist *Artist
|
||||||
if err := request(s.url+artistsURI+strconv.Itoa(artistID), nil, &artist); err != nil {
|
err := request(s.url+artistsURI+strconv.Itoa(artistID), nil, &artist)
|
||||||
return nil, err
|
return artist, err
|
||||||
}
|
|
||||||
return artist, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ArtistReleases ...
|
// ArtistReleases ...
|
||||||
@@ -123,10 +121,8 @@ type ArtistReleases struct {
|
|||||||
// ArtistReleases returns a list of releases and masters associated with the artist.
|
// ArtistReleases returns a list of releases and masters associated with the artist.
|
||||||
func (s *DatabaseService) ArtistReleases(artistID int, pagination *Pagination) (*ArtistReleases, error) {
|
func (s *DatabaseService) ArtistReleases(artistID int, pagination *Pagination) (*ArtistReleases, error) {
|
||||||
var releases *ArtistReleases
|
var releases *ArtistReleases
|
||||||
if err := request(s.url+artistsURI+strconv.Itoa(artistID)+"/releases", pagination.toParams(), &releases); err != nil {
|
err := request(s.url+artistsURI+strconv.Itoa(artistID)+"/releases", pagination.params(), &releases)
|
||||||
return nil, err
|
return releases, err
|
||||||
}
|
|
||||||
return releases, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Label resource represents a label, company, recording studio, location,
|
// Label resource represents a label, company, recording studio, location,
|
||||||
@@ -148,10 +144,8 @@ type Label struct {
|
|||||||
// Label returns a label.
|
// Label returns a label.
|
||||||
func (s *DatabaseService) Label(labelID int) (*Label, error) {
|
func (s *DatabaseService) Label(labelID int) (*Label, error) {
|
||||||
var label *Label
|
var label *Label
|
||||||
if err := request(s.url+labelsURI+strconv.Itoa(labelID), nil, &label); err != nil {
|
err := request(s.url+labelsURI+strconv.Itoa(labelID), nil, &label)
|
||||||
return nil, err
|
return label, err
|
||||||
}
|
|
||||||
return label, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LabelReleases is a list of Releases associated with the label.
|
// LabelReleases is a list of Releases associated with the label.
|
||||||
@@ -163,39 +157,41 @@ type LabelReleases struct {
|
|||||||
// LabelReleases returns a list of Releases associated with the label.
|
// LabelReleases returns a list of Releases associated with the label.
|
||||||
func (s *DatabaseService) LabelReleases(labelID int, pagination *Pagination) (*LabelReleases, error) {
|
func (s *DatabaseService) LabelReleases(labelID int, pagination *Pagination) (*LabelReleases, error) {
|
||||||
var releases *LabelReleases
|
var releases *LabelReleases
|
||||||
if err := request(s.url+labelsURI+strconv.Itoa(labelID)+"/releases", pagination.toParams(), &releases); err != nil {
|
err := request(s.url+labelsURI+strconv.Itoa(labelID)+"/releases", pagination.params(), &releases)
|
||||||
return nil, err
|
return releases, err
|
||||||
}
|
|
||||||
return releases, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Master resource represents a set of similar releases.
|
// Master resource represents a set of similar releases.
|
||||||
// Masters (also known as `master releases`) have a `main release` which is often the chronologically earliest.
|
// 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 {
|
type Master struct {
|
||||||
|
ID int `json:"id"`
|
||||||
Styles []string `json:"styles"`
|
Styles []string `json:"styles"`
|
||||||
Genres []string `json:"genres"`
|
Genres []string `json:"genres"`
|
||||||
Videos []Video `json:"videos"`
|
|
||||||
Title string `json:"title"`
|
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"`
|
MainRelease int `json:"main_release"`
|
||||||
MainReleaseURL string `json:"main_release_url"`
|
MainReleaseURL string `json:"main_release_url"`
|
||||||
URI string `json:"uri"`
|
MostRecentRelease int `json:"most_recent_release"`
|
||||||
Artists []Artist `json:"artists"`
|
MostRecentReleaseURL string `json:"most_recent_release_url"`
|
||||||
VersionURL string `json:"version_url"`
|
VersionsURL string `json:"versions_url"`
|
||||||
Year int `json:"year"`
|
|
||||||
Images []Image `json:"images"`
|
|
||||||
ResourceURL string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
Tracklist []Track `json:"tracklist"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
DataQuality string `json:"data_quality"`
|
DataQuality string `json:"data_quality"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Master returns a master release
|
// Master returns a master release
|
||||||
func (s *DatabaseService) Master(masterID int) (*Master, error) {
|
func (s *DatabaseService) Master(masterID int) (*Master, error) {
|
||||||
var master *Master
|
var master *Master
|
||||||
if err := request(s.url+mastersURI+strconv.Itoa(masterID), nil, &master); err != nil {
|
err := request(s.url+mastersURI+strconv.Itoa(masterID), nil, &master)
|
||||||
return nil, err
|
return master, err
|
||||||
}
|
|
||||||
return master, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MasterVersions retrieves a list of all releases that are versions of this master.
|
// MasterVersions retrieves a list of all releases that are versions of this master.
|
||||||
@@ -207,8 +203,6 @@ type MasterVersions struct {
|
|||||||
// MasterVersions retrieves a list of all Releases that are versions of this master
|
// MasterVersions retrieves a list of all Releases that are versions of this master
|
||||||
func (s *DatabaseService) MasterVersions(masterID int, pagination *Pagination) (*MasterVersions, error) {
|
func (s *DatabaseService) MasterVersions(masterID int, pagination *Pagination) (*MasterVersions, error) {
|
||||||
var versions *MasterVersions
|
var versions *MasterVersions
|
||||||
if err := request(s.url+mastersURI+strconv.Itoa(masterID)+"/versions", pagination.toParams(), &versions); err != nil {
|
err := request(s.url+mastersURI+strconv.Itoa(masterID)+"/versions", pagination.params(), &versions)
|
||||||
return nil, err
|
return versions, err
|
||||||
}
|
|
||||||
return versions, nil
|
|
||||||
}
|
}
|
||||||
|
@@ -1,21 +1,62 @@
|
|||||||
package discogs
|
package discogs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ReleaseServer(w http.ResponseWriter, r *http.Request) {
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
io.WriteString(w, `{"title":"Elephant Riddim"}`)
|
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) {
|
func compareJson(t *testing.T, got, want string) {
|
||||||
expectedTitle := "Elephant Riddim"
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
ts := httptest.NewServer(http.HandlerFunc(ReleaseServer))
|
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()
|
defer ts.Close()
|
||||||
|
|
||||||
d := initDiscogsClient(t, &Options{URL: ts.URL})
|
d := initDiscogsClient(t, &Options{URL: ts.URL})
|
||||||
@@ -24,7 +65,44 @@ func TestReleaseServiceRelease(t *testing.T) {
|
|||||||
t.Fatalf("failed to get release: %s", err)
|
t.Fatalf("failed to get release: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if release.Title != expectedTitle {
|
json, err := json.Marshal(release)
|
||||||
t.Fatalf("release title got=%s want=%s ", expectedTitle, release.Title)
|
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)
|
||||||
}
|
}
|
||||||
|
24
discogs.go
24
discogs.go
@@ -2,6 +2,7 @@ package discogs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -13,22 +14,26 @@ const (
|
|||||||
|
|
||||||
// Options is a set of options to use discogs API client
|
// Options is a set of options to use discogs API client
|
||||||
type Options struct {
|
type Options struct {
|
||||||
|
// Discogs API endpoint (optional).
|
||||||
URL string
|
URL string
|
||||||
|
// Currency to use (optional, default is USD).
|
||||||
Currency string
|
Currency string
|
||||||
|
// UserAgent to to call discogs api with.
|
||||||
UserAgent string
|
UserAgent string
|
||||||
|
// Token provided by discogs (optional).
|
||||||
Token string
|
Token string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client is a Discogs client for making Discogs API requests.
|
// Discogs is a Discogs' client for making Discogs API requests.
|
||||||
type Client struct {
|
type Discogs struct {
|
||||||
Database *DatabaseService
|
Database *DatabaseService
|
||||||
Search *SearchService
|
Search *SearchService
|
||||||
}
|
}
|
||||||
|
|
||||||
var header *http.Header
|
var header *http.Header
|
||||||
|
|
||||||
// NewClient returns a new Client.
|
// New returns a new discogs API client.
|
||||||
func NewClient(o *Options) (*Client, error) {
|
func New(o *Options) (*Discogs, error) {
|
||||||
header = &http.Header{}
|
header = &http.Header{}
|
||||||
|
|
||||||
if o == nil || o.UserAgent == "" {
|
if o == nil || o.UserAgent == "" {
|
||||||
@@ -51,7 +56,7 @@ func NewClient(o *Options) (*Client, error) {
|
|||||||
o.URL = discogsAPI
|
o.URL = discogsAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Discogs{
|
||||||
Database: newDatabaseService(o.URL, cur),
|
Database: newDatabaseService(o.URL, cur),
|
||||||
Search: newSearchService(o.URL + "/database/search"),
|
Search: newSearchService(o.URL + "/database/search"),
|
||||||
}, nil
|
}, nil
|
||||||
@@ -85,6 +90,15 @@ func request(path string, params url.Values, resp interface{}) error {
|
|||||||
}
|
}
|
||||||
defer response.Body.Close()
|
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)
|
body, err := ioutil.ReadAll(response.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@@ -9,7 +9,7 @@ const (
|
|||||||
testToken = ""
|
testToken = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
func initDiscogsClient(t *testing.T, options *Options) *Client {
|
func initDiscogsClient(t *testing.T, options *Options) *Discogs {
|
||||||
if options == nil {
|
if options == nil {
|
||||||
options = &Options{
|
options = &Options{
|
||||||
UserAgent: testUserAgent,
|
UserAgent: testUserAgent,
|
||||||
@@ -22,7 +22,7 @@ func initDiscogsClient(t *testing.T, options *Options) *Client {
|
|||||||
options.UserAgent = testUserAgent
|
options.UserAgent = testUserAgent
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := NewClient(options)
|
client, err := New(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create client: %s", err)
|
t.Fatalf("failed to create client: %s", err)
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ func initDiscogsClient(t *testing.T, options *Options) *Client {
|
|||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewClient(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
options *Options
|
options *Options
|
||||||
err error
|
err error
|
||||||
@@ -50,9 +50,10 @@ func TestNewClient(t *testing.T) {
|
|||||||
}, ErrCurrencyNotSupported},
|
}, ErrCurrencyNotSupported},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, tt := range tests {
|
for name := range tests {
|
||||||
|
tt := tests[name]
|
||||||
t.Run(name, func(t *testing.T) {
|
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)
|
t.Errorf("err got=%s; want=%s", err, tt.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@@ -16,6 +16,7 @@ func (e *Error) Error() string {
|
|||||||
|
|
||||||
// APIErrors
|
// APIErrors
|
||||||
var (
|
var (
|
||||||
|
ErrUnauthorized = &Error{"authentication required"}
|
||||||
ErrCurrencyNotSupported = &Error{"currency does not supported"}
|
ErrCurrencyNotSupported = &Error{"currency does not supported"}
|
||||||
ErrUserAgentInvalid = &Error{"invalid user-agent"}
|
ErrUserAgentInvalid = &Error{"invalid user-agent"}
|
||||||
)
|
)
|
||||||
|
@@ -7,9 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
d, err := discogs.NewClient(&discogs.Options{
|
d, err := discogs.New(&discogs.Options{
|
||||||
UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
|
UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
|
||||||
Currency: "AAA",
|
Currency: "USD",
|
||||||
Token: "",
|
Token: "",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -17,10 +17,10 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
release, err := d.Database.Release(12345)
|
master, err := d.Database.Master(718441)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
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
|
module github.com/irlndts/go-discogs
|
||||||
|
|
||||||
require github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135
|
go 1.14
|
||||||
|
|
||||||
|
require github.com/google/go-cmp v0.4.0
|
||||||
|
6
go.sum
6
go.sum
@@ -1,2 +1,4 @@
|
|||||||
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 h1:zLTLjkaOFEFIOxY5BWLFLwh+cL8vOBW4XJ2aqLE/Tf0=
|
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
|
||||||
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
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=
|
||||||
|
36
models.go
36
models.go
@@ -14,6 +14,17 @@ type Video struct {
|
|||||||
URI string `json:"uri"`
|
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 ...
|
// ArtistSource ...
|
||||||
type ArtistSource struct {
|
type ArtistSource struct {
|
||||||
Anv string `json:"anv"`
|
Anv string `json:"anv"`
|
||||||
@@ -41,14 +52,15 @@ type Track struct {
|
|||||||
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,omitempty"`
|
||||||
Artists []ArtistSource `json:"artists"`
|
Artists []ArtistSource `json:"artists,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LabelSource ...
|
// LabelSource ...
|
||||||
type LabelSource struct {
|
type LabelSource struct {
|
||||||
Catno string `json:"catno"`
|
Catno string `json:"catno"`
|
||||||
EntityType string `json:"entity_type"`
|
EntityType string `json:"entity_type"`
|
||||||
|
EntityTypeName string `json:"entity_type_name"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ResourceURL string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
@@ -56,6 +68,7 @@ type LabelSource struct {
|
|||||||
|
|
||||||
// Identifier ...
|
// Identifier ...
|
||||||
type Identifier struct {
|
type Identifier struct {
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
@@ -80,7 +93,7 @@ type Company struct {
|
|||||||
// Community ...
|
// Community ...
|
||||||
type Community struct {
|
type Community struct {
|
||||||
Contributors []Contributor `json:"contributors"`
|
Contributors []Contributor `json:"contributors"`
|
||||||
DataQuality string `json:"string"`
|
DataQuality string `json:"data_quality"`
|
||||||
Have int `json:"have"`
|
Have int `json:"have"`
|
||||||
Rating Rating `json:"rating"`
|
Rating Rating `json:"rating"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
@@ -143,6 +156,13 @@ type Member struct {
|
|||||||
ResourceURL string `json:"resource_url"`
|
ResourceURL string `json:"resource_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Alias ...
|
||||||
|
type Alias struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
ResourceURL string `json:"resource_url"`
|
||||||
|
}
|
||||||
|
|
||||||
// Sublable ...
|
// Sublable ...
|
||||||
type Sublable struct {
|
type Sublable struct {
|
||||||
ResourceURL string `json:"url"`
|
ResourceURL string `json:"url"`
|
||||||
@@ -175,23 +195,15 @@ type Pagination struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// toParams converts pagaination params to request values
|
// toParams converts pagaination params to request values
|
||||||
func (p *Pagination) toParams() url.Values {
|
func (p *Pagination) params() url.Values {
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
if p.Sort != "" {
|
|
||||||
params.Set("sort", p.Sort)
|
params.Set("sort", p.Sort)
|
||||||
}
|
|
||||||
if p.SortOrder != "" {
|
|
||||||
params.Set("sort_order", p.SortOrder)
|
params.Set("sort_order", p.SortOrder)
|
||||||
}
|
|
||||||
if p.Page != 0 {
|
|
||||||
params.Set("page", strconv.Itoa(p.Page))
|
params.Set("page", strconv.Itoa(p.Page))
|
||||||
}
|
|
||||||
if p.PerPage != 0 {
|
|
||||||
params.Set("per_page", strconv.Itoa(p.PerPage))
|
params.Set("per_page", strconv.Itoa(p.PerPage))
|
||||||
}
|
|
||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
|
126
search.go
126
search.go
@@ -1,7 +1,8 @@
|
|||||||
package discogs
|
package discogs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/google/go-querystring/query"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SearchService ...
|
// SearchService ...
|
||||||
@@ -17,27 +18,96 @@ func newSearchService(url string) *SearchService {
|
|||||||
|
|
||||||
// SearchRequest describes search request
|
// SearchRequest describes search request
|
||||||
type SearchRequest struct {
|
type SearchRequest struct {
|
||||||
Q string `url:"q,omitempty"` // search query
|
Q string // search query
|
||||||
Type string `url:"type,omitempty"` // one of release, master, artist, label
|
Type string // one of release, master, artist, label
|
||||||
Title string `url:"title,omitempty"` // search by combined “Artist Name - Release Title” title field
|
Title string // search by combined “Artist Name - Release Title” title field
|
||||||
ReleaseTitle string `url:"release_title,omitempty"` // search release titles
|
ReleaseTitle string // search release titles
|
||||||
Credit string `url:"credit,omitempty"` // search release credits
|
Credit string // search release credits
|
||||||
Artist string `url:"artist,omitempty"` // search artist names
|
Artist string // search artist names
|
||||||
Anv string `url:"anv,omitempty"` // search artist ANV
|
Anv string // search artist ANV
|
||||||
Label string `url:"label,omitempty"` // search label names
|
Label string // search label names
|
||||||
Genre string `url:"genre,omitempty"` // search genres
|
Genre string // search genres
|
||||||
Style string `url:"style,omitempty"` // search styles
|
Style string // search styles
|
||||||
Country string `url:"country,omitempty"` // search release country
|
Country string // search release country
|
||||||
Year string `url:"year,omitempty"` // search release year
|
Year string // search release year
|
||||||
Format string `url:"format,omitempty"` // search formats
|
Format string // search formats
|
||||||
Catno string `url:"catno,omitempty"` // search catalog number
|
Catno string // search catalog number
|
||||||
Barcode string `url:"barcode,omitempty"` // search barcodes
|
Barcode string // search barcodes
|
||||||
Track string `url:"track,omitempty"` // search track titles
|
Track string // search track titles
|
||||||
Submitter string `url:"submitter,omitempty"` // search submitter username
|
Submitter string // search submitter username
|
||||||
Contributor string `url:"contributor,omitempty"` // search contributor usernames
|
Contributor string // search contributor usernames
|
||||||
|
|
||||||
Page int `url:"page,omitempty"`
|
Page int
|
||||||
PerPage int `url:"per_page,omitempty"`
|
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
|
// Search describes search response
|
||||||
@@ -50,6 +120,7 @@ type Search struct {
|
|||||||
type Result struct {
|
type Result struct {
|
||||||
Style []string `json:"style,omitempty"`
|
Style []string `json:"style,omitempty"`
|
||||||
Thumb string `json:"thumb,omitempty"`
|
Thumb string `json:"thumb,omitempty"`
|
||||||
|
CoverImage string `json:"cover_image,omitempty"`
|
||||||
Title string `json:"title,omitempty"`
|
Title string `json:"title,omitempty"`
|
||||||
Country string `json:"country,omitempty"`
|
Country string `json:"country,omitempty"`
|
||||||
Format []string `json:"format,omitempty"`
|
Format []string `json:"format,omitempty"`
|
||||||
@@ -62,22 +133,15 @@ type Result struct {
|
|||||||
ResourceURL string `json:"resource_url,omitempty"`
|
ResourceURL string `json:"resource_url,omitempty"`
|
||||||
Type string `json:"type,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
ID int `json:"id,omitempty"`
|
ID int `json:"id,omitempty"`
|
||||||
|
MasterID int `json:"master_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search makes search request to discogs.
|
// Search makes search request to discogs.
|
||||||
// Issue a search query to our database. This endpoint accepts pagination parameters.
|
// Issue a search query to our database. This endpoint accepts pagination parameters.
|
||||||
// Authentication (as any user) is required.
|
// Authentication (as any user) is required.
|
||||||
// https://www.discogs.com/developers/#page:database,header:database-search
|
// https://www.discogs.com/developers/#page:database,header:database-search
|
||||||
// TODO(irlndts): improve params to pass
|
|
||||||
func (s *SearchService) Search(req SearchRequest) (*Search, error) {
|
func (s *SearchService) Search(req SearchRequest) (*Search, error) {
|
||||||
params, err := query.Values(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var search *Search
|
var search *Search
|
||||||
if err := request(s.url, params, &search); err != nil {
|
err := request(s.url, req.params(), &search)
|
||||||
return nil, err
|
return search, err
|
||||||
}
|
|
||||||
return search, nil
|
|
||||||
}
|
}
|
||||||
|
9
testing_data.go
Normal file
9
testing_data.go
Normal file
File diff suppressed because one or more lines are too long
27
vendor/github.com/google/go-querystring/LICENSE
generated
vendored
27
vendor/github.com/google/go-querystring/LICENSE
generated
vendored
@@ -1,27 +0,0 @@
|
|||||||
Copyright (c) 2013 Google. All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are
|
|
||||||
met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
* Redistributions in binary form must reproduce the above
|
|
||||||
copyright notice, this list of conditions and the following disclaimer
|
|
||||||
in the documentation and/or other materials provided with the
|
|
||||||
distribution.
|
|
||||||
* Neither the name of Google Inc. nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
320
vendor/github.com/google/go-querystring/query/encode.go
generated
vendored
320
vendor/github.com/google/go-querystring/query/encode.go
generated
vendored
@@ -1,320 +0,0 @@
|
|||||||
// Copyright 2013 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// Package query implements encoding of structs into URL query parameters.
|
|
||||||
//
|
|
||||||
// As a simple example:
|
|
||||||
//
|
|
||||||
// type Options struct {
|
|
||||||
// Query string `url:"q"`
|
|
||||||
// ShowAll bool `url:"all"`
|
|
||||||
// Page int `url:"page"`
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// opt := Options{ "foo", true, 2 }
|
|
||||||
// v, _ := query.Values(opt)
|
|
||||||
// fmt.Print(v.Encode()) // will output: "q=foo&all=true&page=2"
|
|
||||||
//
|
|
||||||
// The exact mapping between Go values and url.Values is described in the
|
|
||||||
// documentation for the Values() function.
|
|
||||||
package query
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"reflect"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var timeType = reflect.TypeOf(time.Time{})
|
|
||||||
|
|
||||||
var encoderType = reflect.TypeOf(new(Encoder)).Elem()
|
|
||||||
|
|
||||||
// Encoder is an interface implemented by any type that wishes to encode
|
|
||||||
// itself into URL values in a non-standard way.
|
|
||||||
type Encoder interface {
|
|
||||||
EncodeValues(key string, v *url.Values) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Values returns the url.Values encoding of v.
|
|
||||||
//
|
|
||||||
// Values expects to be passed a struct, and traverses it recursively using the
|
|
||||||
// following encoding rules.
|
|
||||||
//
|
|
||||||
// Each exported struct field is encoded as a URL parameter unless
|
|
||||||
//
|
|
||||||
// - the field's tag is "-", or
|
|
||||||
// - the field is empty and its tag specifies the "omitempty" option
|
|
||||||
//
|
|
||||||
// The empty values are false, 0, any nil pointer or interface value, any array
|
|
||||||
// slice, map, or string of length zero, and any time.Time that returns true
|
|
||||||
// for IsZero().
|
|
||||||
//
|
|
||||||
// The URL parameter name defaults to the struct field name but can be
|
|
||||||
// specified in the struct field's tag value. The "url" key in the struct
|
|
||||||
// field's tag value is the key name, followed by an optional comma and
|
|
||||||
// options. For example:
|
|
||||||
//
|
|
||||||
// // Field is ignored by this package.
|
|
||||||
// Field int `url:"-"`
|
|
||||||
//
|
|
||||||
// // Field appears as URL parameter "myName".
|
|
||||||
// Field int `url:"myName"`
|
|
||||||
//
|
|
||||||
// // Field appears as URL parameter "myName" and the field is omitted if
|
|
||||||
// // its value is empty
|
|
||||||
// Field int `url:"myName,omitempty"`
|
|
||||||
//
|
|
||||||
// // Field appears as URL parameter "Field" (the default), but the field
|
|
||||||
// // is skipped if empty. Note the leading comma.
|
|
||||||
// Field int `url:",omitempty"`
|
|
||||||
//
|
|
||||||
// For encoding individual field values, the following type-dependent rules
|
|
||||||
// apply:
|
|
||||||
//
|
|
||||||
// Boolean values default to encoding as the strings "true" or "false".
|
|
||||||
// Including the "int" option signals that the field should be encoded as the
|
|
||||||
// strings "1" or "0".
|
|
||||||
//
|
|
||||||
// time.Time values default to encoding as RFC3339 timestamps. Including the
|
|
||||||
// "unix" option signals that the field should be encoded as a Unix time (see
|
|
||||||
// time.Unix())
|
|
||||||
//
|
|
||||||
// Slice and Array values default to encoding as multiple URL values of the
|
|
||||||
// same name. Including the "comma" option signals that the field should be
|
|
||||||
// encoded as a single comma-delimited value. Including the "space" option
|
|
||||||
// similarly encodes the value as a single space-delimited string. Including
|
|
||||||
// the "semicolon" option will encode the value as a semicolon-delimited string.
|
|
||||||
// Including the "brackets" option signals that the multiple URL values should
|
|
||||||
// have "[]" appended to the value name. "numbered" will append a number to
|
|
||||||
// the end of each incidence of the value name, example:
|
|
||||||
// name0=value0&name1=value1, etc.
|
|
||||||
//
|
|
||||||
// Anonymous struct fields are usually encoded as if their inner exported
|
|
||||||
// fields were fields in the outer struct, subject to the standard Go
|
|
||||||
// visibility rules. An anonymous struct field with a name given in its URL
|
|
||||||
// tag is treated as having that name, rather than being anonymous.
|
|
||||||
//
|
|
||||||
// Non-nil pointer values are encoded as the value pointed to.
|
|
||||||
//
|
|
||||||
// Nested structs are encoded including parent fields in value names for
|
|
||||||
// scoping. e.g:
|
|
||||||
//
|
|
||||||
// "user[name]=acme&user[addr][postcode]=1234&user[addr][city]=SFO"
|
|
||||||
//
|
|
||||||
// All other values are encoded using their default string representation.
|
|
||||||
//
|
|
||||||
// Multiple fields that encode to the same URL parameter name will be included
|
|
||||||
// as multiple URL values of the same name.
|
|
||||||
func Values(v interface{}) (url.Values, error) {
|
|
||||||
values := make(url.Values)
|
|
||||||
val := reflect.ValueOf(v)
|
|
||||||
for val.Kind() == reflect.Ptr {
|
|
||||||
if val.IsNil() {
|
|
||||||
return values, nil
|
|
||||||
}
|
|
||||||
val = val.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
if v == nil {
|
|
||||||
return values, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if val.Kind() != reflect.Struct {
|
|
||||||
return nil, fmt.Errorf("query: Values() expects struct input. Got %v", val.Kind())
|
|
||||||
}
|
|
||||||
|
|
||||||
err := reflectValue(values, val, "")
|
|
||||||
return values, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// reflectValue populates the values parameter from the struct fields in val.
|
|
||||||
// Embedded structs are followed recursively (using the rules defined in the
|
|
||||||
// Values function documentation) breadth-first.
|
|
||||||
func reflectValue(values url.Values, val reflect.Value, scope string) error {
|
|
||||||
var embedded []reflect.Value
|
|
||||||
|
|
||||||
typ := val.Type()
|
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
|
||||||
sf := typ.Field(i)
|
|
||||||
if sf.PkgPath != "" && !sf.Anonymous { // unexported
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
sv := val.Field(i)
|
|
||||||
tag := sf.Tag.Get("url")
|
|
||||||
if tag == "-" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
name, opts := parseTag(tag)
|
|
||||||
if name == "" {
|
|
||||||
if sf.Anonymous && sv.Kind() == reflect.Struct {
|
|
||||||
// save embedded struct for later processing
|
|
||||||
embedded = append(embedded, sv)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
name = sf.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
if scope != "" {
|
|
||||||
name = scope + "[" + name + "]"
|
|
||||||
}
|
|
||||||
|
|
||||||
if opts.Contains("omitempty") && isEmptyValue(sv) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if sv.Type().Implements(encoderType) {
|
|
||||||
if !reflect.Indirect(sv).IsValid() {
|
|
||||||
sv = reflect.New(sv.Type().Elem())
|
|
||||||
}
|
|
||||||
|
|
||||||
m := sv.Interface().(Encoder)
|
|
||||||
if err := m.EncodeValues(name, &values); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if sv.Kind() == reflect.Slice || sv.Kind() == reflect.Array {
|
|
||||||
var del byte
|
|
||||||
if opts.Contains("comma") {
|
|
||||||
del = ','
|
|
||||||
} else if opts.Contains("space") {
|
|
||||||
del = ' '
|
|
||||||
} else if opts.Contains("semicolon") {
|
|
||||||
del = ';'
|
|
||||||
} else if opts.Contains("brackets") {
|
|
||||||
name = name + "[]"
|
|
||||||
}
|
|
||||||
|
|
||||||
if del != 0 {
|
|
||||||
s := new(bytes.Buffer)
|
|
||||||
first := true
|
|
||||||
for i := 0; i < sv.Len(); i++ {
|
|
||||||
if first {
|
|
||||||
first = false
|
|
||||||
} else {
|
|
||||||
s.WriteByte(del)
|
|
||||||
}
|
|
||||||
s.WriteString(valueString(sv.Index(i), opts))
|
|
||||||
}
|
|
||||||
values.Add(name, s.String())
|
|
||||||
} else {
|
|
||||||
for i := 0; i < sv.Len(); i++ {
|
|
||||||
k := name
|
|
||||||
if opts.Contains("numbered") {
|
|
||||||
k = fmt.Sprintf("%s%d", name, i)
|
|
||||||
}
|
|
||||||
values.Add(k, valueString(sv.Index(i), opts))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for sv.Kind() == reflect.Ptr {
|
|
||||||
if sv.IsNil() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
sv = sv.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
if sv.Type() == timeType {
|
|
||||||
values.Add(name, valueString(sv, opts))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if sv.Kind() == reflect.Struct {
|
|
||||||
reflectValue(values, sv, name)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
values.Add(name, valueString(sv, opts))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, f := range embedded {
|
|
||||||
if err := reflectValue(values, f, scope); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// valueString returns the string representation of a value.
|
|
||||||
func valueString(v reflect.Value, opts tagOptions) string {
|
|
||||||
for v.Kind() == reflect.Ptr {
|
|
||||||
if v.IsNil() {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
v = v.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
if v.Kind() == reflect.Bool && opts.Contains("int") {
|
|
||||||
if v.Bool() {
|
|
||||||
return "1"
|
|
||||||
}
|
|
||||||
return "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
if v.Type() == timeType {
|
|
||||||
t := v.Interface().(time.Time)
|
|
||||||
if opts.Contains("unix") {
|
|
||||||
return strconv.FormatInt(t.Unix(), 10)
|
|
||||||
}
|
|
||||||
return t.Format(time.RFC3339)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(v.Interface())
|
|
||||||
}
|
|
||||||
|
|
||||||
// isEmptyValue checks if a value should be considered empty for the purposes
|
|
||||||
// of omitting fields with the "omitempty" option.
|
|
||||||
func isEmptyValue(v reflect.Value) bool {
|
|
||||||
switch v.Kind() {
|
|
||||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
|
||||||
return v.Len() == 0
|
|
||||||
case reflect.Bool:
|
|
||||||
return !v.Bool()
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
||||||
return v.Int() == 0
|
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
||||||
return v.Uint() == 0
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
return v.Float() == 0
|
|
||||||
case reflect.Interface, reflect.Ptr:
|
|
||||||
return v.IsNil()
|
|
||||||
}
|
|
||||||
|
|
||||||
if v.Type() == timeType {
|
|
||||||
return v.Interface().(time.Time).IsZero()
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// tagOptions is the string following a comma in a struct field's "url" tag, or
|
|
||||||
// the empty string. It does not include the leading comma.
|
|
||||||
type tagOptions []string
|
|
||||||
|
|
||||||
// parseTag splits a struct field's url tag into its name and comma-separated
|
|
||||||
// options.
|
|
||||||
func parseTag(tag string) (string, tagOptions) {
|
|
||||||
s := strings.Split(tag, ",")
|
|
||||||
return s[0], s[1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Contains checks whether the tagOptions contains the specified option.
|
|
||||||
func (o tagOptions) Contains(option string) bool {
|
|
||||||
for _, s := range o {
|
|
||||||
if s == option {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
2
vendor/modules.txt
vendored
2
vendor/modules.txt
vendored
@@ -1,2 +0,0 @@
|
|||||||
# github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135
|
|
||||||
github.com/google/go-querystring/query
|
|
Reference in New Issue
Block a user