GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 4ce3bc...a2c608 )
by Victor Hugo
01:06 queued 11s
created

devto.NewClient   B

Complexity

Conditions 6

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 0
Metric Value
cc 6
eloc 19
nop 4
dl 0
loc 30
ccs 13
cts 14
cp 0.9286
crap 6.0131
rs 8.5166
c 0
b 0
f 0
1
package devto
2
3
import (
4
	"context"
5
	"errors"
6
	"io"
7
	"net/http"
8
	"net/url"
9
)
10
11
//Configuration constants
12
const (
13
	BaseURL      string = "https://dev.to"
14
	APIVersion   string = "0.5.1"
15
	APIKeyHeader string = "api-key"
16
)
17
18
// devto client errors
19
var (
20
	ErrMissingConfig     = errors.New("missing configuration")
21
	ErrProtectedEndpoint = errors.New("to use this resource you need to provide an authentication method")
22
)
23
24
type httpClient interface {
25
	Do(req *http.Request) (res *http.Response, err error)
26
}
27
28
// Client is the main data structure for performing actions
29
// against dev.to API
30
type Client struct {
31
	Context    context.Context
32
	BaseURL    *url.URL
33
	HTTPClient httpClient
34
	Config     *Config
35
	Articles   *ArticlesResource
36
}
37
38
// NewClient takes a context, a configuration pointer and optionally a
39
// base http client (bc) to build an Client instance.
40
func NewClient(ctx context.Context, conf *Config, bc httpClient, bu string) (dev *Client, err error) {
41 1
	if bc == nil {
42 1
		bc = http.DefaultClient
43
	}
44
45 1
	if ctx == nil {
46 1
		ctx = context.Background()
47
	}
48
49 1
	if conf == nil {
50 1
		return nil, ErrMissingConfig
51
	}
52
53 1
	if bu == "" {
54 1
		bu = BaseURL
55
	}
56
57 1
	u, err := url.Parse(bu)
58 1
	if err != nil {
59
		return nil, err
60
	}
61
62 1
	c := &Client{
63
		Context:    ctx,
64
		BaseURL:    u,
65
		HTTPClient: bc,
66
		Config:     conf,
67
	}
68 1
	c.Articles = &ArticlesResource{API: c}
69 1
	return c, nil
70
}
71
72
// NewRequest build the request relative to the client BaseURL
73
func (c *Client) NewRequest(method string, uri string, body io.Reader) (*http.Request, error) {
74 1
	u, err := url.Parse(uri)
75 1
	if err != nil {
76 1
		return nil, err
77
	}
78 1
	fu := c.BaseURL.ResolveReference(u).String()
79 1
	req, err := http.NewRequest(method, fu, body)
80 1
	if err != nil {
81 1
		return nil, err
82
	}
83 1
	return req, nil
84
}
85