-
Notifications
You must be signed in to change notification settings - Fork 232
/
dl_rest.go
80 lines (67 loc) · 2.35 KB
/
dl_rest.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package dl
import (
"fmt"
"net/url"
"time"
"github.com/CircleCI-Public/circleci-cli/api/rest"
"github.com/CircleCI-Public/circleci-cli/settings"
)
const defaultDlHost = "https://dl.circleci.com"
type dlRestClient struct {
client *rest.Client
}
// NewDlRestClient returns a new dlRestClient instance initialized with the
// values from the config.
func NewDlRestClient(config settings.Config) (*dlRestClient, error) { //
// We don't want the user to use this with Server as that's nor supported
// at them moment. In order to detect this we look if there's a config file
// or cli option that sets "host" to anything different than the default
if config.Host != "" && config.Host != "https://circleci.com" {
// Only error if there's no custom DlHost set. Since the end user can't
// a custom value set this in the config file, this has to have been
// manually been set in the code, presumably by the test suite to allow
// talking to a mock server, and we want to allow that.
if config.DlHost == "" {
return nil, &CloudOnlyErr{}
}
}
// what's the base URL?
unparsedURL := defaultDlHost
if config.DlHost != "" {
unparsedURL = config.DlHost
}
baseURL, err := url.Parse(unparsedURL)
if err != nil {
return nil, fmt.Errorf("cannot parse dl host URL '%s'", unparsedURL)
}
httpclient := config.HTTPClient
httpclient.Timeout = 10 * time.Second
// the dl endpoint is hardcoded to https://dl.circleci.com, since currently
// this implementation always refers to the cloud dl service
return &dlRestClient{
client: rest.New(
baseURL,
config.Token,
httpclient,
),
}, nil
}
func (c dlRestClient) PurgeDLC(projectid string) error {
// this calls a private circleci endpoint. We make no guarantees about
// this still existing in the future.
path := fmt.Sprintf("private/output/project/%s/dlc", projectid)
req, err := c.client.NewRequest("DELETE", &url.URL{Path: path}, nil)
if err != nil {
return err
}
status, err := c.client.DoRequest(req, nil)
// Futureproofing: If CircleCI ever removes the private backend endpoint
// this call uses, by having the endpoint return a 410 status code CircleCI
// can get everyone running an outdated client to display a helpful error
// telling them to upgrade (presumably by this point a version without this
// logic will have been released)
if status == 410 {
return &GoneErr{}
}
return err
}