Merge pull request #24 from irlndts/DSCGS-23

Move services to database
This commit is contained in:
Artem Piskun 2018-03-21 21:18:54 +03:00 committed by GitHub
commit ebfd052f67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 230 additions and 277 deletions

View File

@ -5,13 +5,14 @@ go-discogs is a Go client library for the [Discogs API](https://www.discogs.com/
### Feauteres
* Database
* [Releases](#releases)
* Release Rating
* Master Releases
* Release Versions
* Master Versions
* Artists
* Artist Releases
* Label
* All Label Releases
* [Search](#search)
* [Search](#search)
Install
--------
@ -37,7 +38,7 @@ client, err := discogs.NewClient(&discogs.Options{
#### Releases
```go
release, _ := client.Release.Release(9893847)
release, _ := client.Database.Release(9893847)
fmt.Println(release.Artists[0].Name, " - ", release.Title)
// St. Petersburg Ska-Jazz Review - Elephant Riddim
```

View File

@ -1,52 +0,0 @@
package discogs
import "strconv"
// ArtistService ...
type ArtistService struct {
url string
}
func newArtistService(url string) *ArtistService {
return &ArtistService{
url: url,
}
}
// 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 *ArtistService) Artist(artistID int) (*Artist, error) {
var artist *Artist
if err := request(s.url+strconv.Itoa(artistID), nil, &artist); err != nil {
return nil, err
}
return artist, nil
}
// ArtistReleases ...
type ArtistReleases struct {
Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
// Releases returns a list of releases and masters associated with the artist.
func (s *ArtistService) Releases(artistID int, pagination *Pagination) (*ArtistReleases, error) {
var releases *ArtistReleases
if err := request(s.url+strconv.Itoa(artistID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
}
return releases, nil
}

219
database.go Normal file
View File

@ -0,0 +1,219 @@
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,
}
}
// ReqRelease serves release request
type ReqRelease struct {
CurrAbbr string
}
// 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("CurrAbbr", s.currency)
var release *Release
if err := request(s.url+releasesURI+strconv.Itoa(releaseID), params, &release); err != nil {
return nil, err
}
return release, nil
}
// ReleaseRating serves response for community release rating request
type ReleaseRating struct {
ID int `json:"release_id"`
Rating Rating `json:"rating"`
}
// Rating retruns community release rating
func (s *DatabaseService) ReleaseRating(releaseID int) (*ReleaseRating, error) {
var rating *ReleaseRating
if err := request(s.url+releasesURI+strconv.Itoa(releaseID)+"/rating", nil, &rating); err != nil {
return nil, err
}
return rating, nil
}
// 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
if err := request(s.url+artistsURI+strconv.Itoa(artistID), nil, &artist); err != nil {
return nil, err
}
return artist, nil
}
// ArtistReleases ...
type ArtistReleases struct {
Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
// Releases returns a list of releases and masters associated with the artist.
func (s *DatabaseService) ArtistReleases(artistID int, pagination *Pagination) (*ArtistReleases, error) {
var releases *ArtistReleases
if err := request(s.url+artistsURI+strconv.Itoa(artistID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
}
return releases, nil
}
// 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
if err := request(s.url+labelsURI+strconv.Itoa(labelID), nil, &label); err != nil {
return nil, err
}
return label, nil
}
// LabelReleases is a list of Releases associated with the label.
type LabelReleases struct {
Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
// Releases returns a list of Releases associated with the label.
func (s *DatabaseService) LabelReleases(labelID int, pagination *Pagination) (*LabelReleases, error) {
var releases *LabelReleases
if err := request(s.url+labelsURI+strconv.Itoa(labelID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
}
return releases, nil
}
// 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
if err := request(s.url+mastersURI+strconv.Itoa(masterID), nil, &master); err != nil {
return nil, err
}
return master, nil
}
// MasterVersions retrieves a list of all releases that are versions of this master.
type MasterVersions struct {
Pagination Page `json:"pagination"`
Versions []Version `json:"versions"`
}
// Versions 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
if err := request(s.url+mastersURI+strconv.Itoa(masterID)+"/versions", pagination.toParams(), &versions); err != nil {
return nil, err
}
return versions, nil
}

View File

@ -19,7 +19,7 @@ func TestReleaseServiceRelease(t *testing.T) {
defer ts.Close()
d := initDiscogsClient(t, &Options{URL: ts.URL})
release, err := d.Release.Release(8138518)
release, err := d.Database.Release(8138518)
if err != nil {
t.Fatalf("failed to get release: %s", err)
}

View File

@ -9,7 +9,7 @@ import (
)
const (
discogsAPI = "https://api.discogs.com/"
discogsAPI = "https://api.discogs.com"
)
// Options is a set of options to use discogs API client
@ -22,11 +22,8 @@ type Options struct {
// Client is a Discogs client for making Discogs API requests.
type Client struct {
Release *ReleaseService
Master *MasterService
Artist *ArtistService
Label *LabelService
Search *SearchService
Database *DatabaseService
Search *SearchService
}
var header *http.Header
@ -56,11 +53,8 @@ func NewClient(o *Options) (*Client, error) {
}
return &Client{
Release: newReleaseService(o.URL+"releases/", cur),
Artist: newArtistService(o.URL + "artists/"),
Label: newLabelService(o.URL + "labels/"),
Master: newMasterService(o.URL + "masters/"),
Search: newSearchService(o.URL + "database/search"),
Database: newDatabaseService(o.URL, cur),
Search: newSearchService(o.URL + "/database/search"),
}, nil
}
@ -79,7 +73,6 @@ func currency(c string) (string, error) {
}
func request(path string, params url.Values, resp interface{}) error {
fmt.Println(path + "?" + params.Encode())
r, err := http.NewRequest("GET", path+"?"+params.Encode(), nil)
if err != nil {
return err

View File

@ -17,9 +17,7 @@ func main() {
return
}
release, err := d.Search.Search(discogs.SearchRequest{
Q: "Ska-Jazz Review",
})
release, err := d.Database.Release(12345)
if err != nil {
fmt.Println(err)
return

View File

@ -1,56 +0,0 @@
package discogs
import (
"strconv"
)
// LabelService ...
type LabelService struct {
url string
}
func newLabelService(url string) *LabelService {
return &LabelService{
url: url,
}
}
// 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 *LabelService) Label(labelID int) (*Label, error) {
var label *Label
if err := request(s.url+strconv.Itoa(labelID), nil, &label); err != nil {
return nil, err
}
return label, nil
}
// LabelReleases is a list of Releases associated with the label.
type LabelReleases struct {
Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
// Releases returns a list of Releases associated with the label.
func (s *LabelService) Releases(labelID int, pagination *Pagination) (*LabelReleases, error) {
var releases *LabelReleases
if err := request(s.url+strconv.Itoa(labelID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
}
return releases, nil
}

View File

@ -1,60 +0,0 @@
package discogs
import (
"strconv"
)
// MasterService ...
type MasterService struct {
url string
}
func newMasterService(url string) *MasterService {
return &MasterService{
url: url,
}
}
// 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 *MasterService) Master(masterID int) (*Master, error) {
var master *Master
if err := request(s.url+strconv.Itoa(masterID), nil, &master); err != nil {
return nil, err
}
return master, nil
}
// MasterVersions retrieves a list of all releases that are versions of this master.
type MasterVersions struct {
Pagination Page `json:"pagination"`
Versions []Version `json:"versions"`
}
// Versions retrieves a list of all Releases that are versions of this master
func (s *MasterService) Versions(masterID int, pagination *Pagination) (*MasterVersions, error) {
var versions *MasterVersions
if err := request(s.url+strconv.Itoa(masterID)+"/versions", pagination.toParams(), &versions); err != nil {
return nil, err
}
return versions, nil
}

View File

@ -1,90 +0,0 @@
package discogs
import (
"net/url"
"strconv"
)
// ReleaseService ...
type ReleaseService struct {
url string
currency string
}
func newReleaseService(url string, currency string) *ReleaseService {
return &ReleaseService{
url: url,
currency: currency,
}
}
// ReqRelease serves release request
type ReqRelease struct {
CurrAbbr string
}
// 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 *ReleaseService) Release(releaseID int) (*Release, error) {
params := url.Values{}
params.Set("CurrAbbr", s.currency)
var release *Release
if err := request(s.url+strconv.Itoa(releaseID), params, &release); err != nil {
return nil, err
}
return release, nil
}
// ReleaseRating serves response for community release rating request
type ReleaseRating struct {
ID int `json:"release_id"`
Rating Rating `json:"rating"`
}
// Rating retruns community release rating
func (s *ReleaseService) Rating(releaseID int) (*ReleaseRating, error) {
var rating *ReleaseRating
if err := request(s.url+strconv.Itoa(releaseID)+"/rating", nil, &rating); err != nil {
return nil, err
}
return rating, nil
}