-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
49 lines (39 loc) · 980 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package github
import (
"context"
"errors"
"net/http"
"github.com/gamoch/popular_repos/pkg/graphql"
"github.com/gamoch/popular_repos/pkg/logs"
)
const endpoint = "https://api.github.com/graphql"
type client struct {
token string
client *graphql.Client
}
func NewClient(token string, options ...graphql.ClientOption) *client {
if token == "" {
logs.Error.Fatalln("GITHUB_TOKEN is required")
}
return &client{
token: token,
client: graphql.NewClient(endpoint, options...),
}
}
func (c *client) Run(ctx context.Context, req *graphql.Request, res interface{}) error {
if req != nil {
req.Header.Set("Authorization", "bearer "+c.token)
}
err := c.client.Run(ctx, req, res)
if cause := errors.Unwrap(err); cause != nil {
if errStatus, ok := cause.(*graphql.ErrStatus); ok {
if errStatus.StatusCode == http.StatusUnauthorized {
return &Err{err: &ErrInvalidToken{err: err}}
}
}
}
if err != nil {
return &Err{err: err}
}
return nil
}