Merge pull request #5 from irlndts/DSCG-4

DSCGS-4
This commit is contained in:
Artem Piskun 2017-04-26 16:02:07 +03:00 committed by GitHub
commit 86244f6665
8 changed files with 111 additions and 86 deletions

View File

@ -41,10 +41,9 @@ client := discogs.NewClient("TestDiscogsClient/0.0.1 +example.com", "sometoken")
#### Releases
```go
params := &discogs.ReleaseParams{Release_id: "8138518"}
release, _, err := client.Release.Release(params)
release, err := client..Release(9893847)
fmt.Println(fmt.Println(release.Artists[0].Name, " - ", release.Title)) // St. Petersburg Ska-Jazz Review - Elephant Riddim
fmt.Println(release.Artists[0].Name, " - ", release.Title) // St. Petersburg Ska-Jazz Review - Elephant Riddim
```
#### Artists

View File

@ -1,6 +1,7 @@
package discogs
import (
"fmt"
"net/http"
"github.com/irlndts/go-apirequest"
@ -13,7 +14,7 @@ const (
// Client is a Discogs client for making Discogs API requests.
type Client struct {
api *apirequest.API
Release *ReleaseService
currency string
Master *MasterService
Artist *ArtistService
Label *LabelService
@ -21,13 +22,42 @@ type Client struct {
}
// NewClient returns a new Client.
func NewClient(useragent, token string) *Client {
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI).Add("User-Agent", useragent).Add("Authorization", "Discogs token="+token)
func NewClient() *Client {
base := apirequest.New().Client(&http.Client{}).Base(discogsAPI)
return &Client{
api: base,
currency: "USD",
Artist: newArtistService(base.New()),
Label: newLabelService(base.New()),
Master: newMasterService(base.New()),
Release: newReleaseService(base.New()),
Search: newSearchService(base.New()),
}
}
// Token sets tokens, it's required for some queries like search
func (c *Client) Token(token string) *Client {
c.api.Set("Authorization", "Discogs token="+token)
return c
}
// UserAgent sets specified user agent
// Discogs requires it
func (c *Client) UserAgent(useragent string) *Client {
c.api.Set("User-Agent", useragent)
return c
}
// SetCurrency determines currency for marketplace data.
// Defaults to the authenticated users currency. Must be one of the following:
// USD GBP EUR CAD AUD JPY CHF MXN BRL NZD SEK ZAR
func (c *Client) Currency(currency string) error {
switch currency {
case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR":
c.currency = currency
default:
return fmt.Errorf("%v\n", "Invalid currency abbreviation.")
}
return nil
}

View File

@ -9,6 +9,7 @@ type APIError struct {
Message string `json:"message"`
}
// Error ...
func (e APIError) Error() string {
if e.Message != "" {
return fmt.Sprintf("discogs: %v", e.Message)

View File

@ -7,17 +7,17 @@ import (
)
func main() {
d := discogs.NewClient("TestDiscogsClient/0.0.1 +http://irlndts.moscow", "")
d := discogs.NewClient().UserAgent("TestDiscogsClient/0.0.1 +http://irlndts.moscow").Token()
request := &discogs.SearchRequest{Q: "The Reggaenauts - River Rock / Thursday Kick-off", Page: 0, Per_page: 1}
search, _, err := d.Search.Search(request)
if err != nil {
if err := d.Currency("EUR"); err != nil {
fmt.Println(err)
return
}
for _, r := range search.Results {
fmt.Println(r.Id, r.Title)
release, err := d.Release(9893847)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", release)
}

View File

@ -16,7 +16,7 @@ func TestLabelService_Label(t *testing.T) {
}
func TestLabelService_Releases(t *testing.T) {
expectedId := "Cha Cha Twist"
expectedId := "Ghetto Sol"
d := NewClient(testUserAgent, testToken)
label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"})

View File

@ -1,61 +1,56 @@
package discogs
import (
"github.com/irlndts/go-apirequest"
"net/http"
"strconv"
)
type ReleaseService struct {
api *apirequest.API
}
type ReleaseParams struct {
Release_id string
}
// Release serves relesase response from discogs
type Release struct {
Title string `json:"title"`
Id int `json:"id"`
ID int `json:"id"`
Artists []ArtistSource `json:"artists"`
Data_quality string `json:"data_quality"`
DataQuality string `json:"data_quality"`
Thumb string `json:"thumb"`
Community Community `json:"community"`
Companies []Company `json:"companies"`
Country string `json:"country"`
Date_added string `json:"date_added"`
Date_changed string `json:"date_changed"`
Estimated_weight int `json:"estimated_weight"`
Extraartists []ArtistSource `json:"extraartists"`
Format_quantity int `json:"format_quantity"`
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"`
Master_id int `json:"master_id"`
Master_url string `json:"master_url"`
Notes string `josn:"notes"`
LowestPrice float64 `json:"lowest_price"`
MasterID int `json:"master_id"`
MasterURL string `json:"master_url"`
Notes string `json:"notes,omitempty"`
NumForSale int `json:"numfor_sale,omitempty"`
Released string `json:"released"`
Released_formatted string `json:"released_formatted"`
Resource_url string `json:"resource_url"`
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"`
URI string `json:"uri"`
Videos []Video `json:"videos"`
Year int `json:"year"`
}
func newReleaseService(api *apirequest.API) *ReleaseService {
return &ReleaseService{
api: api.Path("releases/"),
}
type RequestRelease struct {
Curr_abbr string
}
func (self *ReleaseService) Release(params *ReleaseParams) (*Release, *http.Response, error) {
// Release returns release by release's ID
func (c *Client) Release(releaseID int) (*Release, error) {
release := new(Release)
apiError := new(APIError)
resp, err := self.api.New().Get(params.Release_id).Receive(release, apiError)
return release, resp, relevantError(err, *apiError)
req := &RequestRelease{Curr_abbr: c.currency}
_, err := c.api.New().Get("releases/"+strconv.Itoa(releaseID)).QueryStruct(req).Receive(release, apiError)
return release, relevantError(err, *apiError)
}

View File

@ -9,7 +9,7 @@ func TestReleaseService_Release(t *testing.T) {
expectedTitle := "Elephant Riddim"
d := NewClient(testUserAgent, testToken)
release, _, err := d.Release.Release(&ReleaseParams{Release_id: "8138518"})
release, err := d.Release.Release(8138518)
check(t, err)
assert(t, release.Title == expectedTitle, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedTitle, release.Title))

View File

@ -49,15 +49,15 @@ type Result struct {
Title string `json:"title,omitempty"`
Country string `json:"country,omitempty"`
Format []string `json:"format,omitempty"`
Uri string `json:"uri,omitempty"`
URI string `json:"uri,omitempty"`
Community Community `json:"community,omitempty"`
Label []string `json:"label,omitempty"`
Catno string `json:"catno,omitempty"`
Year string `json:"year,omitempty"`
Genre []string `json:"genre,omitempty"`
Resource_url string `json:"resource_url,omitempty"`
ResourceURL string `json:"resource_url,omitempty"`
Type string `json:"type,omitempty"`
Id int `json:"id,omitempty"`
ID int `json:"id,omitempty"`
}
func newSearchService(api *apirequest.API) *SearchService {