Create Credentials struct
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
@@ -18,9 +18,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() (err error) {
|
func run() (err error) {
|
||||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
credentials := &reddit.Credentials{
|
||||||
|
ID: "id",
|
||||||
|
Secret: "secret",
|
||||||
|
Username: "username",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
|
||||||
client, err := reddit.NewClient(nil, withCredentials)
|
client, err := reddit.NewClient(nil, credentials)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() (err error) {
|
func run() (err error) {
|
||||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
credentials := &reddit.Credentials{
|
||||||
|
ID: "id",
|
||||||
|
Secret: "secret",
|
||||||
|
Username: "username",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
|
||||||
client, err := reddit.NewClient(nil, withCredentials)
|
client, err := reddit.NewClient(nil, credentials)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() (err error) {
|
func run() (err error) {
|
||||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
credentials := &reddit.Credentials{
|
||||||
|
ID: "id",
|
||||||
|
Secret: "secret",
|
||||||
|
Username: "username",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
|
||||||
client, err := reddit.NewClient(nil, withCredentials)
|
client, err := reddit.NewClient(nil, credentials)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,9 +19,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() (err error) {
|
func run() (err error) {
|
||||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
credentials := &reddit.Credentials{
|
||||||
|
ID: "id",
|
||||||
|
Secret: "secret",
|
||||||
|
Username: "username",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
|
||||||
client, err := reddit.NewClient(nil, withCredentials)
|
client, err := reddit.NewClient(nil, credentials)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -41,7 +46,7 @@ func run() (err error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprintf(os.Stderr, "Error! %v", err)
|
fmt.Fprintf(os.Stderr, "Error! %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
@@ -17,9 +17,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() (err error) {
|
func run() (err error) {
|
||||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
credentials := &reddit.Credentials{
|
||||||
|
ID: "id",
|
||||||
|
Secret: "secret",
|
||||||
|
Username: "username",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
|
||||||
client, err := reddit.NewClient(nil, withCredentials)
|
client, err := reddit.NewClient(nil, credentials)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,17 +35,6 @@ func FromEnv(c *Client) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithCredentials sets the necessary values for the client to authenticate via OAuth2.
|
|
||||||
func WithCredentials(id, secret, username, password string) Opt {
|
|
||||||
return func(c *Client) error {
|
|
||||||
c.ID = id
|
|
||||||
c.Secret = secret
|
|
||||||
c.Username = username
|
|
||||||
c.Password = password
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithBaseURL sets the base URL for the client to make requests to.
|
// WithBaseURL sets the base URL for the client to make requests to.
|
||||||
func WithBaseURL(u string) Opt {
|
func WithBaseURL(u string) Opt {
|
||||||
return func(c *Client) error {
|
return func(c *Client) error {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func TestFromEnv(t *testing.T) {
|
|||||||
os.Setenv("GO_REDDIT_CLIENT_PASSWORD", "password1")
|
os.Setenv("GO_REDDIT_CLIENT_PASSWORD", "password1")
|
||||||
defer os.Unsetenv("GO_REDDIT_CLIENT_PASSWORD")
|
defer os.Unsetenv("GO_REDDIT_CLIENT_PASSWORD")
|
||||||
|
|
||||||
c, err := NewClient(nil, FromEnv)
|
c, err := NewClient(nil, nil, FromEnv)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
type values struct {
|
type values struct {
|
||||||
@@ -32,26 +32,16 @@ func TestFromEnv(t *testing.T) {
|
|||||||
require.Equal(t, expect, actual)
|
require.Equal(t, expect, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithCredentials(t *testing.T) {
|
|
||||||
withCredentials := WithCredentials("id1", "secret1", "username1", "password1")
|
|
||||||
c, err := NewClient(nil, withCredentials)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, "id1", c.ID)
|
|
||||||
require.Equal(t, "secret1", c.Secret)
|
|
||||||
require.Equal(t, "username1", c.Username)
|
|
||||||
require.Equal(t, "password1", c.Password)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithBaseURL(t *testing.T) {
|
func TestWithBaseURL(t *testing.T) {
|
||||||
baseURL := "http://localhost:8080"
|
baseURL := "http://localhost:8080"
|
||||||
c, err := NewClient(nil, WithBaseURL(baseURL))
|
c, err := NewClient(nil, nil, WithBaseURL(baseURL))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, baseURL, c.BaseURL.String())
|
require.Equal(t, baseURL, c.BaseURL.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithTokenURL(t *testing.T) {
|
func TestWithTokenURL(t *testing.T) {
|
||||||
tokenURL := "http://localhost:8080/api/v1/access_token"
|
tokenURL := "http://localhost:8080/api/v1/access_token"
|
||||||
c, err := NewClient(nil, WithTokenURL(tokenURL))
|
c, err := NewClient(nil, nil, WithTokenURL(tokenURL))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, tokenURL, c.TokenURL.String())
|
require.Equal(t, tokenURL, c.TokenURL.String())
|
||||||
}
|
}
|
||||||
|
|||||||
+41
-25
@@ -73,6 +73,14 @@ func (t *userAgentTransport) base() http.RoundTripper {
|
|||||||
// 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)
|
||||||
|
|
||||||
|
// Credentials used to authenticate to make requests to the Reddit API.
|
||||||
|
type Credentials struct {
|
||||||
|
ID string
|
||||||
|
Secret string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
// Client manages communication with the Reddit API.
|
// Client manages communication with the Reddit API.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
// HTTP client used to communicate with the Reddit API.
|
// HTTP client used to communicate with the Reddit API.
|
||||||
@@ -141,42 +149,50 @@ func newClient(httpClient *http.Client) *Client {
|
|||||||
baseURL, _ := url.Parse(defaultBaseURL)
|
baseURL, _ := url.Parse(defaultBaseURL)
|
||||||
tokenURL, _ := url.Parse(defaultTokenURL)
|
tokenURL, _ := url.Parse(defaultTokenURL)
|
||||||
|
|
||||||
c := &Client{client: httpClient, BaseURL: baseURL, TokenURL: tokenURL}
|
client := &Client{client: httpClient, BaseURL: baseURL, TokenURL: tokenURL}
|
||||||
|
|
||||||
c.Account = &AccountService{client: c}
|
client.Account = &AccountService{client: client}
|
||||||
c.Collection = &CollectionService{client: c}
|
client.Collection = &CollectionService{client: client}
|
||||||
c.Emoji = &EmojiService{client: c}
|
client.Emoji = &EmojiService{client: client}
|
||||||
c.Flair = &FlairService{client: c}
|
client.Flair = &FlairService{client: client}
|
||||||
c.Gold = &GoldService{client: c}
|
client.Gold = &GoldService{client: client}
|
||||||
c.Listings = &ListingsService{client: c}
|
client.Listings = &ListingsService{client: client}
|
||||||
c.Message = &MessageService{client: c}
|
client.Message = &MessageService{client: client}
|
||||||
c.Moderation = &ModerationService{client: c}
|
client.Moderation = &ModerationService{client: client}
|
||||||
c.Multi = &MultiService{client: c}
|
client.Multi = &MultiService{client: client}
|
||||||
c.Stream = &StreamService{client: c}
|
client.Stream = &StreamService{client: client}
|
||||||
c.Subreddit = &SubredditService{client: c}
|
client.Subreddit = &SubredditService{client: client}
|
||||||
c.User = &UserService{client: c}
|
client.User = &UserService{client: client}
|
||||||
|
|
||||||
postAndCommentService := &postAndCommentService{client: c}
|
postAndCommentService := &postAndCommentService{client: client}
|
||||||
c.Comment = &CommentService{client: c, postAndCommentService: postAndCommentService}
|
client.Comment = &CommentService{client: client, postAndCommentService: postAndCommentService}
|
||||||
c.Post = &PostService{client: c, postAndCommentService: postAndCommentService}
|
client.Post = &PostService{client: client, postAndCommentService: postAndCommentService}
|
||||||
|
|
||||||
return c
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a client that can make requests to the Reddit API.
|
// NewClient returns a new Reddit API client. If a nil httpClient is provided,
|
||||||
func NewClient(httpClient *http.Client, opts ...Opt) (c *Client, err error) {
|
// a new http.Client will be used.
|
||||||
c = newClient(httpClient)
|
func NewClient(httpClient *http.Client, creds *Credentials, opts ...Opt) (*Client, error) {
|
||||||
|
client := newClient(httpClient)
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
if err = opt(c); err != nil {
|
if err := opt(client); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
oauthTransport := oauthTransport(c)
|
if creds != nil {
|
||||||
c.client.Transport = oauthTransport
|
client.ID = creds.ID
|
||||||
|
client.Secret = creds.Secret
|
||||||
|
client.Username = creds.Username
|
||||||
|
client.Password = creds.Password
|
||||||
|
|
||||||
return
|
oauthTransport := oauthTransport(client)
|
||||||
|
client.client.Transport = oauthTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserAgent returns the client's user agent.
|
// UserAgent returns the client's user agent.
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func setup() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
client, _ = NewClient(nil,
|
client, _ = NewClient(nil,
|
||||||
WithCredentials("id1", "secret1", "user1", "password1"),
|
&Credentials{"id1", "secret1", "user1", "password1"},
|
||||||
WithBaseURL(server.URL),
|
WithBaseURL(server.URL),
|
||||||
WithTokenURL(server.URL+"/api/v1/access_token"),
|
WithTokenURL(server.URL+"/api/v1/access_token"),
|
||||||
)
|
)
|
||||||
@@ -92,7 +92,7 @@ func testClientServices(t *testing.T, c *Client) {
|
|||||||
|
|
||||||
func testClientDefaultUserAgent(t *testing.T, c *Client) {
|
func testClientDefaultUserAgent(t *testing.T, c *Client) {
|
||||||
expectedUserAgent := fmt.Sprintf("golang:%s:v%s (by /u/)", libraryName, libraryVersion)
|
expectedUserAgent := fmt.Sprintf("golang:%s:v%s (by /u/)", libraryName, libraryVersion)
|
||||||
require.Equal(t, expectedUserAgent, c.userAgent)
|
require.Equal(t, expectedUserAgent, c.UserAgent())
|
||||||
}
|
}
|
||||||
|
|
||||||
func testClientDefaults(t *testing.T, c *Client) {
|
func testClientDefaults(t *testing.T, c *Client) {
|
||||||
@@ -101,7 +101,7 @@ func testClientDefaults(t *testing.T, c *Client) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewClient(t *testing.T) {
|
func TestNewClient(t *testing.T) {
|
||||||
c, err := NewClient(nil)
|
c, err := NewClient(nil, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
testClientDefaults(t, c)
|
testClientDefaults(t, c)
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ func TestNewClient_Error(t *testing.T) {
|
|||||||
return errors.New("foo")
|
return errors.New("foo")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := NewClient(nil, errorOpt)
|
_, err := NewClient(nil, nil, errorOpt)
|
||||||
require.EqualError(t, err, "foo")
|
require.EqualError(t, err, "foo")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user