/
circleci.go
55 lines (47 loc) · 1.16 KB
/
circleci.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
50
51
52
53
54
55
// Package circleci provides a Client for making requests to CircleCI.
package circleci
import (
"context"
"fmt"
"net/http"
)
// Project represents a CircleCI project.
type Project struct {
Name string
Organization string
VCSType string
}
// Client is used to make HTTP requests to CircleCI.
type Client struct {
client *http.Client
rootURL string
project *Project
token string
}
// New creates a Client.
func New(client *http.Client, rootURL string, project *Project, token string) *Client {
return &Client{
client: client,
rootURL: rootURL,
project: project,
token: token,
}
}
func (c *Client) baseURL() string {
return fmt.Sprintf(
"%s/api/v1.1/project/%s/%s/%s",
c.rootURL,
c.project.VCSType,
c.project.Organization,
c.project.Name,
)
}
// do sends an HTTP request and returns an HTTP response.
// It is a thin wrapper around http.Client.Do that will
// set the auth using the Client's token, and set headers.
func (c *Client) do(ctx context.Context, req *http.Request) (*http.Response, error) {
req = req.WithContext(ctx)
req.SetBasicAuth(c.token, "")
req.Header.Set("Accept", "application/json")
return c.client.Do(req)
}