2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-08-02 13:42:53 -04:00
|
|
|
// Opt is a configuration option to initialize a client.
|
2020-04-29 15:59:18 -04:00
|
|
|
type Opt func(*Client) error
|
|
|
|
|
|
|
|
// FromEnv configures the client with values from environment variables.
|
|
|
|
//
|
|
|
|
// Supported environment variables:
|
2020-07-11 13:49:07 -04:00
|
|
|
// GO_REDDIT_CLIENT_ID to set the client's id.
|
|
|
|
// GO_REDDIT_CLIENT_SECRET to set the client's secret.
|
|
|
|
// GO_REDDIT_CLIENT_USERNAME to set the client's username.
|
|
|
|
// GO_REDDIT_CLIENT_PASSWORD to set the client's password.
|
2020-04-29 15:59:18 -04:00
|
|
|
func FromEnv(c *Client) error {
|
2020-07-11 13:49:07 -04:00
|
|
|
if v, ok := os.LookupEnv("GO_REDDIT_CLIENT_ID"); ok {
|
2020-04-29 15:59:18 -04:00
|
|
|
c.ID = v
|
|
|
|
}
|
|
|
|
|
2020-07-11 13:49:07 -04:00
|
|
|
if v, ok := os.LookupEnv("GO_REDDIT_CLIENT_SECRET"); ok {
|
2020-04-29 15:59:18 -04:00
|
|
|
c.Secret = v
|
|
|
|
}
|
|
|
|
|
2020-07-11 13:49:07 -04:00
|
|
|
if v, ok := os.LookupEnv("GO_REDDIT_CLIENT_USERNAME"); ok {
|
2020-04-29 15:59:18 -04:00
|
|
|
c.Username = v
|
|
|
|
}
|
|
|
|
|
2020-07-11 13:49:07 -04:00
|
|
|
if v, ok := os.LookupEnv("GO_REDDIT_CLIENT_PASSWORD"); ok {
|
2020-04-29 15:59:18 -04:00
|
|
|
c.Password = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:42:53 -04:00
|
|
|
// WithBaseURL sets the base URL for the client to make requests to.
|
2020-04-29 15:59:18 -04:00
|
|
|
func WithBaseURL(u string) Opt {
|
|
|
|
return func(c *Client) error {
|
|
|
|
url, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.BaseURL = url
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:42:53 -04:00
|
|
|
// WithTokenURL sets the url used to get access tokens.
|
2020-04-29 15:59:18 -04:00
|
|
|
func WithTokenURL(u string) Opt {
|
|
|
|
return func(c *Client) error {
|
|
|
|
url, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.TokenURL = url
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|