oi! oi! oi!

This commit is contained in:
irlndts 2016-03-10 17:25:55 +03:00
parent 037a2762df
commit e790defb31
8 changed files with 276 additions and 111 deletions

125
commonstructs.go Normal file
View File

@ -0,0 +1,125 @@
package discogs
type Video struct {
Description string `json:"description"`
Duration int `json:"duration"`
Embed bool `json:"embed"`
Title string `json:"title"`
Uri string `json:"uri"`
}
type Artist struct {
Anv string `json:"anv"`
Id int `json:"id"`
Join string `json:"join"`
Name string `json:"name:`
Resource_url string `json:"resource_url"`
Role string `json:"role"`
Tracks string `json:"tracks"`
}
type Image struct {
Height int `json:"height"`
Width int `json:"width"`
Resource_url string `json:"resource_url"`
Type string `json:"type"`
Uri string `json:"uri"`
Uri150 string `json:"uri150"`
}
type Track struct {
Duration string `json:"duration"`
Position string `json:"position"`
Title string `json:"title"`
Type string `json:"type_"`
Extraartists []Extraartist `json:"extraartists"`
}
type Extraartist struct {
Anv string `json:"anv"`
Id int `json:"id"`
Join string `json:"join"`
Name string `json:"name"`
Resource_url string `json:"resource_url"`
Role string `json:"role"`
Tracks string `json:"tracks"`
}
type Label struct {
Catno string `json:"catno"`
Entity_type string `json:"entity_type"`
Id int `json:"id"`
Name string `json:"name"`
Resource_url string `json:"resource_url"`
}
type Identifier struct {
Type string `json:"type"`
Value string `json:"value"`
}
type Format struct {
Descriptions []string `json:"descriptions"`
Name string `json:"name"`
Qty string `json:"qty"`
}
type Company struct {
Catno string `json:"catno"`
Entity_type string `json:"entity_type"`
Entity_type_name string `json:"entity_type_name"`
Id int `json:"id"`
Name string `json:"name"`
Resource_url string `json:"resource_url"`
}
type Community struct {
Contributors []Contributor `json:"contributors"`
Data_quality string `json:"string"`
Have int `json:"have"`
Rating Rating `json:"rating"`
Status string `json:"status"`
Submitter Submitter `json:"submitter"`
Want int `json:"want"`
}
type Submitter struct {
Resource_url string `json:"resource_url"`
Username string `json:"username"`
}
type Rating struct {
Average float32 `json:"average"`
Count int `json:"count"`
}
type Contributor struct {
Resource_url string `json:"resource_url"`
Username string `json:"username"`
}
type Page struct {
Per_page int `json:"per_page"`
Items int `json:"items"`
Page int `json:"page"`
Urls URLS `json:"urls"`
Pages int `json:"pages"`
}
type URLS struct {
Last string `json:"last"`
Next string `json:"next"`
}
type Version struct {
Catno string `json:"catno"`
Country string `json:"country"`
Format string `json:"format"`
Id int `json:"id"`
Label string `json:"label"`
Released string `json:"released"`
Resource_url string `json:"resource_url"`
Status string `json:"status"`
Thumb string `json:"thumb"`
Title string `json:"title"`
}

View File

@ -14,6 +14,7 @@ const (
type Client struct {
api *apirequest.API
Release *ReleaseService
Master *MasterService
}
// NewClient returns a new Client.
@ -23,6 +24,7 @@ func NewClient(httpClient *http.Client) *Client {
return &Client{
api: base,
Release: newReleaseService(base.New()),
Master: newMasterService(base.New()),
}
}

17
discogs_test.go Normal file
View File

@ -0,0 +1,17 @@
package discogs
import (
"testing"
)
func check(t *testing.T, e error) {
if e != nil {
t.Error(e)
}
}
func assert(t *testing.T, condition bool, assertion string) {
if !condition {
t.Errorf("Assertion failed: %v", assertion)
}
}

View File

@ -9,12 +9,19 @@ import (
func main() {
client := &http.Client{}
d := discogs.NewClient(client).UserAgent("TestDiscogsClient/0.0.1 +http://irlndts.moscow")
params := &discogs.ReleaseParams{Release_id: "1"}
release, _, err := d.Release.Release(params)
/*
params := &discogs.ReleaseParams{Release_id: "8138518"}
release, _, err := d.Release.Release(params)
*/
params := &discogs.MasterVersionParams{Master_id: "1000", Page: 2, Per_page: 1}
master, _, err := d.Master.Versions(params)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(release)
//fmt.Println(release.Title)
fmt.Println(master)
}
//fmt.Println(resp)
}

63
masters.go Normal file
View File

@ -0,0 +1,63 @@
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)
}

29
masters_test.go Normal file
View File

@ -0,0 +1,29 @@
package discogs
import (
"fmt"
"net/http"
"testing"
)
func TestMasterService_Master(t *testing.T) {
expectedTitle := "Elephant Riddim"
client := &http.Client{}
d := NewClient(client).UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs")
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"
client := &http.Client{}
d := NewClient(client).UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs")
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))
}

View File

@ -16,22 +16,22 @@ type ReleaseParams struct {
type Release struct {
Title string `json:"title"`
Id int `json:"id"`
Artists []artist `json:"artists"`
Artists []Artist `json:"artists"`
Data_quality string `json:"data_quality"`
Thumb string `json:"thumb"`
Community community `json:"community"`
Companies []company `json:"companies"`
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 []extraartist `json:"extraartists"`
Extraartists []Extraartist `json:"extraartists"`
Format_quantity int `json:"format_quantity"`
Formats []format `json:"formats"`
Formats []Format `json:"formats"`
Genres []string `json:"genres"`
Identifiers []identifier `json:"identifiers"`
Images []image `json:"images"`
Labels []label `json:"labels"`
Identifiers []Identifier `json:"identifiers"`
Images []Image `json:"images"`
Labels []Label `json:"labels"`
Master_id int `json:"master_id"`
Master_url string `json:"master_url"`
Notes string `josn:"notes"`
@ -40,109 +40,12 @@ type Release struct {
Resource_url string `json:"resource_url"`
Status string `json:"status"`
Styles []string `json:"styles"`
Tracklist []tracklist `json:"tracklist"`
Tracklist []Track `json:"tracklist"`
Uri string `json:"uri"`
Videos []video `json:"videos"`
Videos []Video `json:"videos"`
Year int `json:"year"`
}
type video struct {
Description string `json:"description"`
Duration int `json:"duration"`
Embed bool `json:"embed"`
Title string `json:"title"`
Uri string `json:"uri"`
}
type tracklist struct {
Duration string `json:"duration"`
Position string `json:"position"`
Title string `json:"title"`
Type string `json:"type_"`
}
type label struct {
Catno string `json:"catno"`
Entity_type string `json:"entity_type"`
Id int `json:"id"`
Name string `json:"name"`
Resource_url string `json:"resource_url"`
}
type image struct {
Height int `json:"height"`
Width int `json:"width"`
Resource_url string `json:"resource_url"`
Type string `json:"type"`
Uri string `json:"uri"`
Uri150 string `json:"uri150"`
}
type identifier struct {
Type string `json:"type"`
Value string `json:"value"`
}
type format struct {
Descriptions []string `json:"descriptions"`
Name string `json:"name"`
Qty string `json:"qty"`
}
type extraartist struct {
Anv string `json:"anv"`
Id int `json:"id"`
Join string `json:"join"`
Name string `json:"name"`
Resource_url string `json:"resource_url"`
Role string `json:"role"`
Tracks string `json:"tracks"`
}
type company struct {
Catno string `json:"catno"`
Entity_type string `json:"entity_type"`
Entity_type_name string `json:"entity_type_name"`
Id int `json:"id"`
Name string `json:"name"`
Resource_url string `json:"resource_url"`
}
type community struct {
Contributors []contributor `json:"contributors"`
Data_quality string `json:"string"`
Have int `json:"have"`
Rating rating `json:"rating"`
Status string `json:"status"`
Submitter submitter `json:"submitter"`
Want int `json:"want"`
}
type submitter struct {
Resource_url string `json:"resource_url"`
Username string `json:"username"`
}
type rating struct {
Average float32 `json:"average"`
Count int `json:"count"`
}
type contributor struct {
Resource_url string `json:"resource_url"`
Username string `json:"username"`
}
type artist struct {
Anv string `json:"anv"`
Id int `json:"id"`
Join string `json:"join"`
Name string `json:"name:`
Resource_url string `json:"resource_url"`
Role string `json:"role"`
Tracks string `json:"tracks"`
}
func newReleaseService(api *apirequest.API) *ReleaseService {
return &ReleaseService{
api: api.Path("releases/"),
@ -152,6 +55,7 @@ func newReleaseService(api *apirequest.API) *ReleaseService {
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).QueryStruct(params).Receive(release, apiError)
resp, err := self.api.New().Get(params.Release_id).Receive(release, apiError)
return release, resp, relevantError(err, *apiError)
}

18
releases_test.go Normal file
View File

@ -0,0 +1,18 @@
package discogs
import (
"fmt"
"net/http"
"testing"
)
func TestReleaseService_Release(t *testing.T) {
expectedTitle := "Elephant Riddim"
client := &http.Client{}
d := NewClient(client).UserAgent("UnitTestClient/0.0.1 +https://github.com/irlndts/go-discogs")
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))
}