11 const queryStringTag = "querystring"
13 // Test represents a statuscake Test
15 // ThiTestID is an int, use this to get more details about this test. If not provided will insert a new check, else will update
16 TestID int `json:"TestID" querystring:"TestID" querystringoptions:"omitempty"`
18 // Sent tfalse To Unpause and true To Pause.
19 Paused bool `json:"Paused" querystring:"Paused"`
21 // Website name. Tags are stripped out
22 WebsiteName string `json:"WebsiteName" querystring:"WebsiteName"`
24 // CustomHeader. A special header that will be sent along with the HTTP tests.
25 CustomHeader string `json:"CustomHeader" querystring:"CustomHeader"`
27 // Use to populate the test with a custom user agent
28 UserAgent string `json:"UserAgent" queryString:"UserAgent"`
30 // Test location, either an IP (for TCP and Ping) or a fully qualified URL for other TestTypes
31 WebsiteURL string `json:"WebsiteURL" querystring:"WebsiteURL"`
33 // A Port to use on TCP Tests
34 Port int `json:"Port" querystring:"Port"`
36 // Contact group ID - will return int of contact group used else 0
37 ContactID int `json:"ContactID" querystring:"ContactGroup"`
39 // Current status at last test
40 Status string `json:"Status"`
43 Uptime float64 `json:"Uptime"`
45 // Any test locations seperated by a comma (using the Node Location IDs)
46 NodeLocations []string `json:"NodeLocations" querystring:"NodeLocations"`
48 // Timeout in an int form representing seconds.
49 Timeout int `json:"Timeout" querystring:"Timeout"`
51 // A URL to ping if a site goes down.
52 PingURL string `json:"PingURL" querystring:"PingURL"`
54 Confirmation int `json:"Confirmationi,string" querystring:"Confirmation"`
56 // The number of seconds between checks.
57 CheckRate int `json:"CheckRate" querystring:"CheckRate"`
59 // A Basic Auth User account to use to login
60 BasicUser string `json:"BasicUser" querystring:"BasicUser"`
62 // If BasicUser is set then this should be the password for the BasicUser
63 BasicPass string `json:"BasicPass" querystring:"BasicPass"`
65 // Set 1 to enable public reporting, 0 to disable
66 Public int `json:"Public" querystring:"Public"`
68 // A URL to a image to use for public reporting
69 LogoImage string `json:"LogoImage" querystring:"LogoImage"`
71 // Set to 0 to use branding (default) or 1 to disable public reporting branding
72 Branding int `json:"Branding" querystring:"Branding"`
74 // Used internally by the statuscake API
75 WebsiteHost string `json:"WebsiteHost"`
77 // Enable virus checking or not. 1 to enable
78 Virus int `json:"Virus" querystring:"Virus"`
80 // A string that should either be found or not found.
81 FindString string `json:"FindString" querystring:"FindString"`
83 // If the above string should be found to trigger a alert. true will trigger if FindString found
84 DoNotFind bool `json:"DoNotFind" querystring:"DoNotFind"`
86 // What type of test type to use. Accepted values are HTTP, TCP, PING
87 TestType string `json:"TestType" querystring:"TestType"`
89 // Use 1 to TURN OFF real browser testing
90 RealBrowser int `json:"RealBrowser" querystring:"RealBrowser"`
92 // How many minutes to wait before sending an alert
93 TriggerRate int `json:"TriggerRate" querystring:"TriggerRate"`
95 // Tags should be seperated by a comma - no spacing between tags (this,is,a set,of,tags)
96 TestTags string `json:"TestTags" querystring:"TestTags"`
98 // Comma Seperated List of StatusCodes to Trigger Error on (on Update will replace, so send full list each time)
99 StatusCodes string `json:"StatusCodes" querystring:"StatusCodes"`
101 // Set to 1 to enable the Cookie Jar. Required for some redirects.
102 UseJar int `json:"UseJar" querystring:"UseJar"`
104 // Raw POST data seperated by an ampersand
105 PostRaw string `json:"PostRaw" querystring:"PostRaw"`
107 // Use to specify the expected Final URL in the testing process
108 FinalEndpoint string `json:"FinalEndpoint" querystring:"FinalEndpoint"`
110 // Use to specify whether redirects should be followed
111 FollowRedirect bool `json:"FollowRedirect" querystring:"FollowRedirect"`
114 // Validate checks if the Test is valid. If it's invalid, it returns a ValidationError with all invalid fields. It returns nil otherwise.
115 func (t *Test) Validate() error {
116 e := make(ValidationError)
118 if t.WebsiteName == "" {
119 e["WebsiteName"] = "is required"
122 if t.WebsiteURL == "" {
123 e["WebsiteURL"] = "is required"
126 if t.Timeout != 0 && (t.Timeout < 6 || t.Timeout > 99) {
127 e["Timeout"] = "must be 0 or between 6 and 99"
130 if t.Confirmation < 0 || t.Confirmation > 9 {
131 e["Confirmation"] = "must be between 0 and 9"
134 if t.CheckRate < 0 || t.CheckRate > 23999 {
135 e["CheckRate"] = "must be between 0 and 23999"
138 if t.Public < 0 || t.Public > 1 {
139 e["Public"] = "must be 0 or 1"
142 if t.Virus < 0 || t.Virus > 1 {
143 e["Virus"] = "must be 0 or 1"
146 if t.TestType != "HTTP" && t.TestType != "TCP" && t.TestType != "PING" {
147 e["TestType"] = "must be HTTP, TCP, or PING"
150 if t.RealBrowser < 0 || t.RealBrowser > 1 {
151 e["RealBrowser"] = "must be 0 or 1"
154 if t.TriggerRate < 0 || t.TriggerRate > 59 {
155 e["TriggerRate"] = "must be between 0 and 59"
158 if t.PostRaw != "" && t.TestType != "HTTP" {
159 e["PostRaw"] = "must be HTTP to submit a POST request"
162 if t.FinalEndpoint != "" && t.TestType != "HTTP" {
163 e["FinalEndpoint"] = "must be a Valid URL"
166 var jsonVerifiable map[string]interface{}
167 if json.Unmarshal([]byte(t.CustomHeader), &jsonVerifiable) != nil {
168 e["CustomHeader"] = "must be provided as json string"
178 // ToURLValues returns url.Values of all fields required to create/update a Test.
179 func (t Test) ToURLValues() url.Values {
180 values := make(url.Values)
181 st := reflect.TypeOf(t)
182 sv := reflect.ValueOf(t)
183 for i := 0; i < st.NumField(); i++ {
185 tag := sf.Tag.Get(queryStringTag)
187 if ft.Name() == "" && ft.Kind() == reflect.Ptr {
193 options := sf.Tag.Get("querystringoptions")
194 omit := options == "omitempty" && isEmptyValue(v)
196 if tag != "" && !omit {
197 values.Set(tag, valueToQueryStringValue(v))
204 func isEmptyValue(v reflect.Value) bool {
206 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
210 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
212 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
214 case reflect.Float32, reflect.Float64:
215 return v.Float() == 0
216 case reflect.Interface, reflect.Ptr:
223 func valueToQueryStringValue(v reflect.Value) string {
224 if v.Type().Name() == "bool" {
232 if v.Type().Kind() == reflect.Slice {
233 if ss, ok := v.Interface().([]string); ok {
234 return strings.Join(ss, ",")
241 // Tests is a client that implements the `Tests` API.
242 type Tests interface {
243 All() ([]*Test, error)
244 Detail(int) (*Test, error)
245 Update(*Test) (*Test, error)
246 Delete(TestID int) error
253 func newTests(c apiClient) Tests {
259 func (tt *tests) All() ([]*Test, error) {
260 resp, err := tt.client.get("/Tests", nil)
264 defer resp.Body.Close()
267 err = json.NewDecoder(resp.Body).Decode(&tests)
272 func (tt *tests) Update(t *Test) (*Test, error) {
273 resp, err := tt.client.put("/Tests/Update", t.ToURLValues())
277 defer resp.Body.Close()
279 var ur updateResponse
280 err = json.NewDecoder(resp.Body).Decode(&ur)
286 return nil, &updateError{Issues: ur.Issues}
290 t2.TestID = ur.InsertID
295 func (tt *tests) Delete(testID int) error {
296 resp, err := tt.client.delete("/Tests/Details", url.Values{"TestID": {fmt.Sprint(testID)}})
300 defer resp.Body.Close()
302 var dr deleteResponse
303 err = json.NewDecoder(resp.Body).Decode(&dr)
309 return &deleteError{Message: dr.Error}
315 func (tt *tests) Detail(testID int) (*Test, error) {
316 resp, err := tt.client.get("/Tests/Details", url.Values{"TestID": {fmt.Sprint(testID)}})
320 defer resp.Body.Close()
322 var dr *detailResponse
323 err = json.NewDecoder(resp.Body).Decode(&dr)
328 return dr.test(), nil