-
Notifications
You must be signed in to change notification settings - Fork 1
/
checking.go
117 lines (97 loc) · 2.76 KB
/
checking.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
116
117
package acrolinx
import (
"fmt"
"net/http"
)
type CheckingService struct {
client *Client
}
func (s *CheckingService) GetCapabilities(opts *GetCapabilitiesOptions) (*Capabilities, Links, error) {
req, err := s.client.newRequest(http.MethodGet, "api/v1/checking/capabilities", nil)
if err != nil {
return nil, nil, err
}
if opts != nil && opts.Locale != "" {
req.Header.Set(headerLocale, opts.Locale)
}
var caps Capabilities
var reqError RequestError
links := make(Links)
resp := Response{
Data: &caps,
Links: links,
Error: &reqError,
}
err = s.client.do(req, &resp)
if err != nil {
return nil, nil, err
}
if reqError != (RequestError{}) {
return nil, links, &reqError
}
return &caps, links, nil
}
func (s *CheckingService) SubmitCheck(opts *SubmitCheckOptions) (*Check, Links, error) {
req, err := s.client.newRequest(http.MethodPost, "api/v1/checking/checks", opts)
if err != nil {
return nil, nil, fmt.Errorf("Error preparing check request: %w", err)
}
var check Check
links := make(Links)
var reqError RequestError
resp := Response{Data: &check, Links: links, Error: &reqError}
err = s.client.do(req, &resp)
if err != nil {
return nil, nil, fmt.Errorf("Error processing check request: %w", err)
}
if reqError != (RequestError{}) {
return nil, nil, &reqError
}
return &check, links, nil
}
func (s *CheckingService) GetCheckResult(check *Check) (*CheckResult, Links, error) {
path := fmt.Sprintf("api/v1/checking/checks/%s", check.ID)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, fmt.Errorf("Error preparing check request: %w", err)
}
var result CheckResult
var progress Progress
links := make(Links)
var reqError RequestError
resp := Response{Data: &result, Links: links, Progress: &progress, Error: &reqError}
err = s.client.do(req, &resp)
if err != nil {
return nil, nil, fmt.Errorf("Error processing check request%s: %w", check.ID, err)
}
if reqError != (RequestError{}) {
return nil, nil, &reqError
}
if progress != (Progress{}) {
result.Progress = &progress
}
return &result, links, nil
}
func (s *CheckingService) CancelCheck(check *Check) (*CancelledCheck, Links, error) {
path := fmt.Sprintf("api/v1/checking/checks/%s", check.ID)
req, err := s.client.newRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, nil, fmt.Errorf("Error preparing cancel request: %w", err)
}
var result CancelledCheck
links := make(Links)
var reqError RequestError
resp := Response{
Data: &result,
Links: links,
Error: &reqError,
}
err = s.client.do(req, &resp)
if err != nil {
return nil, nil, fmt.Errorf("Error cancelling check request %s: %w", check.ID, err)
}
if reqError != (RequestError{}) {
return nil, nil, &reqError
}
return &result, nil, nil
}