From bcf5c89215197c81387ad086411379c83336581e Mon Sep 17 00:00:00 2001 From: irlndts Date: Tue, 24 Mar 2020 13:08:35 +0300 Subject: [PATCH] Minor improvements --- README.md | 7 ++++--- discogs.go | 20 ++++++++++++-------- discogs_test.go | 8 ++++---- examples/discogs_example.go | 2 +- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c71bebe..97e6ff5 100644 --- a/README.md +++ b/README.md @@ -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. +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 * Database * [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). This is token way example: ```go -client, err := discogs.NewClient(&discogs.Options{ +client, err := discogs.New(&discogs.Options{ UserAgent: "Some Name", Currency: "EUR", // optional, "USD" (default), "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR" are allowed Token: "Some Token", // optional + URL: "https://api.discogs.com", // optional }) ``` @@ -85,5 +88,3 @@ Example fmt.Println(r.Title) } ``` - -etc. diff --git a/discogs.go b/discogs.go index 0cde216..b513e07 100644 --- a/discogs.go +++ b/discogs.go @@ -14,22 +14,26 @@ const ( // Options is a set of options to use discogs API client type Options struct { - URL string - Currency string + // Discogs API endpoint (optional). + URL string + // Currency to use (optional, default is USD). + Currency string + // UserAgent to to call discogs api with. UserAgent string - Token string + // Token provided by discogs (optional). + Token string } -// Client is a Discogs client for making Discogs API requests. -type Client struct { +// Discogs is a Discogs' client for making Discogs API requests. +type Discogs struct { Database *DatabaseService Search *SearchService } var header *http.Header -// NewClient returns a new Client. -func NewClient(o *Options) (*Client, error) { +// New returns a new discogs API client. +func New(o *Options) (*Discogs, error) { header = &http.Header{} if o == nil || o.UserAgent == "" { @@ -52,7 +56,7 @@ func NewClient(o *Options) (*Client, error) { o.URL = discogsAPI } - return &Client{ + return &Discogs{ Database: newDatabaseService(o.URL, cur), Search: newSearchService(o.URL + "/database/search"), }, nil diff --git a/discogs_test.go b/discogs_test.go index e82167f..915b19c 100644 --- a/discogs_test.go +++ b/discogs_test.go @@ -9,7 +9,7 @@ const ( testToken = "" ) -func initDiscogsClient(t *testing.T, options *Options) *Client { +func initDiscogsClient(t *testing.T, options *Options) *Discogs { if options == nil { options = &Options{ UserAgent: testUserAgent, @@ -22,7 +22,7 @@ func initDiscogsClient(t *testing.T, options *Options) *Client { options.UserAgent = testUserAgent } - client, err := NewClient(options) + client, err := New(options) if err != nil { t.Fatalf("failed to create client: %s", err) } @@ -30,7 +30,7 @@ func initDiscogsClient(t *testing.T, options *Options) *Client { return client } -func TestNewClient(t *testing.T) { +func TestNew(t *testing.T) { tests := map[string]struct { options *Options err error @@ -53,7 +53,7 @@ func TestNewClient(t *testing.T) { for name := range tests { tt := tests[name] 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) } }) diff --git a/examples/discogs_example.go b/examples/discogs_example.go index 3d9d9f9..6acdb61 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -7,7 +7,7 @@ import ( ) func main() { - d, err := discogs.NewClient(&discogs.Options{ + d, err := discogs.New(&discogs.Options{ UserAgent: "TestDiscogsClient/0.0.1 +http://example.com", Currency: "USD", Token: "",