/
api.go
115 lines (98 loc) · 2.45 KB
/
api.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package upapi
import (
"context"
"github.com/hashicorp/go-cleanhttp"
)
const (
defaultBaseURL = "https://uptime.com/api/v1/"
defaultUserAgent = "uptime-client-go"
)
// API manages communication with the Uptime.com API.
type API interface {
Checks() ChecksEndpoint
Contacts() ContactsEndpoint
Dashboards() DashboardsEndpoint
Integrations() IntegrationsEndpoint
Tags() TagsEndpoint
Outages() OutagesEndpoint
ProbeServers() ProbeServersEndpoint
StatusPages() StatusPagesEndpoint
}
// New returns a new API client instance.
func New(opts ...Option) (api API, err error) {
var cbd CBD = &struct {
Doer
RequestBuilder
ResponseDecoder
}{
cleanhttp.DefaultClient(),
&requestBuilderImpl{},
&responseDecoderImpl{},
}
cbd, err = applyOptions(cbd, opts...)
if err != nil {
return nil, err
}
var defs []Option
if cbd.(Doer) == nil {
panic("no Doer")
}
rq, err := cbd.BuildRequest(context.Background(), "GET", "/", nil, nil)
if rq.URL.Host == "" {
defs = append(defs, WithBaseURL(defaultBaseURL))
}
if rq.Header.Get("User-Agent") == "" {
defs = append(defs, WithUserAgent(defaultUserAgent))
}
cbd, err = applyOptions(cbd, defs...)
if err != nil {
return nil, err
}
api = &apiImpl{
CBD: cbd,
checks: NewChecksEndpoint(cbd),
contacts: NewContactsEndpoint(cbd),
dashboards: NewDashboardsEndpoint(cbd),
integrations: NewIntegrationsEndpoint(cbd),
tags: NewTagsEndpoint(cbd),
outages: NewOutagesEndpoint(cbd),
probeServers: NewProbeServersEndpoint(cbd),
statusPages: NewStatusPagesEndpoint(cbd),
}
return api, nil
}
type apiImpl struct {
CBD
checks ChecksEndpoint
contacts ContactsEndpoint
dashboards DashboardsEndpoint
integrations IntegrationsEndpoint
tags TagsEndpoint
outages OutagesEndpoint
probeServers ProbeServersEndpoint
statusPages StatusPagesEndpoint
}
func (api *apiImpl) Checks() ChecksEndpoint {
return api.checks
}
func (api *apiImpl) Contacts() ContactsEndpoint {
return api.contacts
}
func (api *apiImpl) Dashboards() DashboardsEndpoint {
return api.dashboards
}
func (api *apiImpl) Integrations() IntegrationsEndpoint {
return api.integrations
}
func (api *apiImpl) Tags() TagsEndpoint {
return api.tags
}
func (api *apiImpl) Outages() OutagesEndpoint {
return api.outages
}
func (api *apiImpl) ProbeServers() ProbeServersEndpoint {
return api.probeServers
}
func (api *apiImpl) StatusPages() StatusPagesEndpoint {
return api.statusPages
}