Minor improvements (#39)

This commit is contained in:
Artem Piskun 2020-03-24 13:11:44 +03:00 committed by GitHub
parent d9deca7e18
commit aa374638bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 16 deletions

View File

@ -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
}) })
``` ```
@ -85,5 +88,3 @@ Example
fmt.Println(r.Title) fmt.Println(r.Title)
} }
``` ```
etc.

View File

@ -14,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 == "" {
@ -52,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

View File

@ -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
@ -53,7 +53,7 @@ func TestNewClient(t *testing.T) {
for name := range tests { for name := range tests {
tt := tests[name] 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)
} }
}) })

View File

@ -7,7 +7,7 @@ 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: "USD", Currency: "USD",
Token: "", Token: "",