Make DefaultClient a method that returns the global default client

I made this a method to prevent other users from reassigning the
previously exported DefaultClient, e.g. doing something like:

```go
reddit.DefaultClient = nil
```

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-11-01 22:25:13 -05:00
parent 8fe2410f4a
commit 78dc97a8d5
6 changed files with 13 additions and 9 deletions
+7 -3
View File
@@ -39,8 +39,12 @@ const (
headerRateLimitReset = "x-ratelimit-reset"
)
// DefaultClient is a readonly client with limited access to the Reddit API.
var DefaultClient, _ = NewReadonlyClient()
var defaultClient, _ = NewReadonlyClient()
// DefaultClient returns a valid, read-only client with limited access to the Reddit API.
func DefaultClient() *Client {
return defaultClient
}
// RequestCompletionCallback defines the type of the request callback function.
type RequestCompletionCallback func(*http.Request, *http.Response)
@@ -232,7 +236,7 @@ func (c *Client) UserAgent() string {
}
// NewRequest creates an API request with form data as the body.
// The path is the relative URL which will be resolves to the BaseURL of the Client.
// The path is the relative URL which will be resolved to the BaseURL of the Client.
// It should always be specified without a preceding slash.
func (c *Client) NewRequest(method string, path string, form url.Values) (*http.Request, error) {
u, err := c.BaseURL.Parse(path)
+1 -1
View File
@@ -134,7 +134,7 @@ func TestNewReadonlyClient_Error(t *testing.T) {
}
func TestDefaultClient(t *testing.T) {
require.NotNil(t, DefaultClient)
require.NotNil(t, DefaultClient())
}
func TestClient_Readonly_NewRequest(t *testing.T) {