@@ 12,11 12,19 @@ import (
)
type Client struct {
- token string
+ token string
+ graphql *graphql.Client
}
func NewClient(token string) *Client {
- return &Client{token: token}
+ src := oauth2.StaticTokenSource(
+ &oauth2.Token{AccessToken: token},
+ )
+ httpClient := oauth2.NewClient(context.Background(), src)
+
+ client := graphql.NewClient("https://meta.sr.ht/query", httpClient)
+
+ return &Client{token: token, graphql: client}
}
type UserInfo struct {
@@ 32,12 40,6 @@ var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z
// User fetches the info of user, if user is empty, it fetches the logged in user's information
func (c *Client) User(user string) (*UserInfo, error) {
- src := oauth2.StaticTokenSource(
- &oauth2.Token{AccessToken: c.token},
- )
- httpClient := oauth2.NewClient(context.Background(), src)
-
- client := graphql.NewClient("https://meta.sr.ht/query", httpClient)
var userInfo UserInfo
@@ 46,7 48,7 @@ func (c *Client) User(user string) (*UserInfo, error) {
Me UserInfo
}
- err := client.Query(context.Background(), &query, nil)
+ err := c.graphql.Query(context.Background(), &query, nil)
if err != nil {
return nil, err
}
@@ 58,7 60,7 @@ func (c *Client) User(user string) (*UserInfo, error) {
UserByID UserInfo `graphql:"userByID(id: $id)"`
}
- err := client.Query(context.Background(), &query, map[string]interface{}{"id": graphql.Int(id)})
+ err := c.graphql.Query(context.Background(), &query, map[string]interface{}{"id": graphql.Int(id)})
if err != nil {
return nil, err
}
@@ 68,7 70,7 @@ func (c *Client) User(user string) (*UserInfo, error) {
UserByEmail UserInfo `graphql:"userByEmail(email: $email)"`
}
- err := client.Query(context.Background(), &query, map[string]interface{}{"email": graphql.String(user)})
+ err := c.graphql.Query(context.Background(), &query, map[string]interface{}{"email": graphql.String(user)})
if err != nil {
return nil, err
}
@@ 83,7 85,7 @@ func (c *Client) User(user string) (*UserInfo, error) {
UserByName UserInfo `graphql:"userByName(username: $username)"`
}
- err = client.Query(context.Background(), &query, map[string]interface{}{"username": graphql.String(user)})
+ err = c.graphql.Query(context.Background(), &query, map[string]interface{}{"username": graphql.String(user)})
if err != nil {
return nil, err
}
@@ 105,13 107,6 @@ type ConfigOptions struct {
}
func (c *Client) Config(cfg ConfigOptions) error {
- src := oauth2.StaticTokenSource(
- &oauth2.Token{AccessToken: c.token},
- )
- httpClient := oauth2.NewClient(context.Background(), src)
-
- client := graphql.NewClient("https://meta.sr.ht/query", httpClient)
-
if cfg.Url == nil && cfg.Location == nil && cfg.Bio == nil && cfg.Email == nil {
return errors.New("no mutation occurred")
}
@@ 136,6 131,6 @@ func (c *Client) Config(cfg ConfigOptions) error {
},
}
- err := client.Mutate(context.Background(), &mutation, variables)
+ err := c.graphql.Mutate(context.Background(), &mutation, variables)
return err
}