diff --git a/README.md b/README.md index c27f055..345891b 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ client, _ := reddit.NewClient(withCredentials, reddit.WithHTTPClient(httpClient) ### Read-Only Mode -The global `DefaultClient` variable is a valid, read-only client with limited access to the Reddit API, much like a logged out user. You can initialize your own via `NewReadonlyClient`: +The `DefaultClient` method returns a valid, read-only client with limited access to the Reddit API, much like a logged out user. You can initialize your own and configure it further using options via `NewReadonlyClient`: ```go client, _ := reddit.NewReadonlyClient() diff --git a/examples/get-subreddit/main.go b/examples/get-subreddit/main.go index 7d4ad6b..5e2c5e3 100644 --- a/examples/get-subreddit/main.go +++ b/examples/get-subreddit/main.go @@ -17,7 +17,7 @@ func main() { } func run() (err error) { - sr, _, err := reddit.DefaultClient.Subreddit.Get(ctx, "golang") + sr, _, err := reddit.DefaultClient().Subreddit.Get(ctx, "golang") if err != nil { return } diff --git a/examples/get-top-posts/main.go b/examples/get-top-posts/main.go index 6a797f1..1f62e0b 100644 --- a/examples/get-top-posts/main.go +++ b/examples/get-top-posts/main.go @@ -20,7 +20,7 @@ func run() (err error) { // Let's get the top 200 posts of r/golang. // Reddit returns a maximum of 100 posts at a time, // so we'll need to separate this into 2 requests. - posts, resp, err := reddit.DefaultClient.Subreddit.TopPosts(ctx, "golang", &reddit.ListPostOptions{ + posts, resp, err := reddit.DefaultClient().Subreddit.TopPosts(ctx, "golang", &reddit.ListPostOptions{ ListOptions: reddit.ListOptions{ Limit: 100, }, @@ -36,7 +36,7 @@ func run() (err error) { // The After option sets the id of an item that Reddit // will use as an anchor point for the returned listing. - posts, _, err = reddit.DefaultClient.Subreddit.TopPosts(ctx, "golang", &reddit.ListPostOptions{ + posts, _, err = reddit.DefaultClient().Subreddit.TopPosts(ctx, "golang", &reddit.ListPostOptions{ ListOptions: reddit.ListOptions{ Limit: 100, After: resp.After, diff --git a/examples/stream-posts/main.go b/examples/stream-posts/main.go index afeae94..9ade73b 100644 --- a/examples/stream-posts/main.go +++ b/examples/stream-posts/main.go @@ -18,7 +18,7 @@ func main() { defer close(sig) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) - posts, errs, stop := reddit.DefaultClient.Stream.Posts("AskReddit", reddit.StreamInterval(time.Second*3), reddit.StreamDiscardInitial) + posts, errs, stop := reddit.DefaultClient().Stream.Posts("AskReddit", reddit.StreamInterval(time.Second*3), reddit.StreamDiscardInitial) defer stop() timer := time.NewTimer(time.Minute) diff --git a/reddit/reddit.go b/reddit/reddit.go index 7c2d086..8b1b4d6 100644 --- a/reddit/reddit.go +++ b/reddit/reddit.go @@ -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) diff --git a/reddit/reddit_test.go b/reddit/reddit_test.go index 2aea1d4..01187e9 100644 --- a/reddit/reddit_test.go +++ b/reddit/reddit_test.go @@ -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) {