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:
parent
8fe2410f4a
commit
78dc97a8d5
6 changed files with 13 additions and 9 deletions
|
@ -60,7 +60,7 @@ client, _ := reddit.NewClient(withCredentials, reddit.WithHTTPClient(httpClient)
|
||||||
|
|
||||||
### Read-Only Mode
|
### 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
|
```go
|
||||||
client, _ := reddit.NewReadonlyClient()
|
client, _ := reddit.NewReadonlyClient()
|
||||||
|
|
|
@ -17,7 +17,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func run() (err error) {
|
func run() (err error) {
|
||||||
sr, _, err := reddit.DefaultClient.Subreddit.Get(ctx, "golang")
|
sr, _, err := reddit.DefaultClient().Subreddit.Get(ctx, "golang")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ func run() (err error) {
|
||||||
// Let's get the top 200 posts of r/golang.
|
// Let's get the top 200 posts of r/golang.
|
||||||
// Reddit returns a maximum of 100 posts at a time,
|
// Reddit returns a maximum of 100 posts at a time,
|
||||||
// so we'll need to separate this into 2 requests.
|
// 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{
|
ListOptions: reddit.ListOptions{
|
||||||
Limit: 100,
|
Limit: 100,
|
||||||
},
|
},
|
||||||
|
@ -36,7 +36,7 @@ func run() (err error) {
|
||||||
|
|
||||||
// The After option sets the id of an item that Reddit
|
// The After option sets the id of an item that Reddit
|
||||||
// will use as an anchor point for the returned listing.
|
// 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{
|
ListOptions: reddit.ListOptions{
|
||||||
Limit: 100,
|
Limit: 100,
|
||||||
After: resp.After,
|
After: resp.After,
|
||||||
|
|
|
@ -18,7 +18,7 @@ func main() {
|
||||||
defer close(sig)
|
defer close(sig)
|
||||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
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()
|
defer stop()
|
||||||
|
|
||||||
timer := time.NewTimer(time.Minute)
|
timer := time.NewTimer(time.Minute)
|
||||||
|
|
|
@ -39,8 +39,12 @@ const (
|
||||||
headerRateLimitReset = "x-ratelimit-reset"
|
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.
|
// RequestCompletionCallback defines the type of the request callback function.
|
||||||
type RequestCompletionCallback func(*http.Request, *http.Response)
|
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.
|
// 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.
|
// It should always be specified without a preceding slash.
|
||||||
func (c *Client) NewRequest(method string, path string, form url.Values) (*http.Request, error) {
|
func (c *Client) NewRequest(method string, path string, form url.Values) (*http.Request, error) {
|
||||||
u, err := c.BaseURL.Parse(path)
|
u, err := c.BaseURL.Parse(path)
|
||||||
|
|
|
@ -134,7 +134,7 @@ func TestNewReadonlyClient_Error(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDefaultClient(t *testing.T) {
|
func TestDefaultClient(t *testing.T) {
|
||||||
require.NotNil(t, DefaultClient)
|
require.NotNil(t, DefaultClient())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClient_Readonly_NewRequest(t *testing.T) {
|
func TestClient_Readonly_NewRequest(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue