11 const queryStringTag = "querystring"
13 // Test represents a statuscake Test
15 // TestID 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 - deprecated in favor of ContactGroup but still provided in the API detail response
37 ContactID int `json:"ContactID"`
39 // Contact group IDs - will return list of ints or empty if not provided
40 ContactGroup []string `json:"ContactGroup" querystring:"ContactGroup"`
42 // Current status at last test
43 Status string `json:"Status"`
46 Uptime float64 `json:"Uptime"`
48 // Any test locations seperated by a comma (using the Node Location IDs)
49 NodeLocations []string `json:"NodeLocations" querystring:"NodeLocations"`
51 // Timeout in an int form representing seconds.
52 Timeout int `json:"Timeout" querystring:"Timeout"`
54 // A URL to ping if a site goes down.
55 PingURL string `json:"PingURL" querystring:"PingURL"`
57 Confirmation int `json:"Confirmation,string" querystring:"Confirmation"`
59 // The number of seconds between checks.
60 CheckRate int `json:"CheckRate" querystring:"CheckRate"`
62 // A Basic Auth User account to use to login
63 BasicUser string `json:"BasicUser" querystring:"BasicUser"`
65 // If BasicUser is set then this should be the password for the BasicUser
66 BasicPass string `json:"BasicPass" querystring:"BasicPass"`
68 // Set 1 to enable public reporting, 0 to disable
69 Public int `json:"Public" querystring:"Public"`
71 // A URL to a image to use for public reporting
72 LogoImage string `json:"LogoImage" querystring:"LogoImage"`
74 // Set to 0 to use branding (default) or 1 to disable public reporting branding
75 Branding int `json:"Branding" querystring:"Branding"`
77 // Used internally by the statuscake API
78 WebsiteHost string `json:"WebsiteHost" querystring:"WebsiteHost"`
80 // Enable virus checking or not. 1 to enable
81 Virus int `json:"Virus" querystring:"Virus"`
83 // A string that should either be found or not found.
84 FindString string `json:"FindString" querystring:"FindString"`
86 // If the above string should be found to trigger a alert. true will trigger if FindString found
87 DoNotFind bool `json:"DoNotFind" querystring:"DoNotFind"`
89 // What type of test type to use. Accepted values are HTTP, TCP, PING
90 TestType string `json:"TestType" querystring:"TestType"`
92 // Use 1 to TURN OFF real browser testing
93 RealBrowser int `json:"RealBrowser" querystring:"RealBrowser"`
95 // How many minutes to wait before sending an alert
96 TriggerRate int `json:"TriggerRate" querystring:"TriggerRate"`
98 // Tags should be seperated by a comma - no spacing between tags (this,is,a set,of,tags)
99 TestTags []string `json:"TestTags" querystring:"TestTags"`
101 // Comma Seperated List of StatusCodes to Trigger Error on (on Update will replace, so send full list each time)
102 StatusCodes string `json:"StatusCodes" querystring:"StatusCodes"`
104 // Set to 1 to enable the Cookie Jar. Required for some redirects.
105 UseJar int `json:"UseJar" querystring:"UseJar"`
107 // Raw POST data seperated by an ampersand
108 PostRaw string `json:"PostRaw" querystring:"PostRaw"`
110 // Use to specify the expected Final URL in the testing process
111 FinalEndpoint string `json:"FinalEndpoint" querystring:"FinalEndpoint"`
113 // Use to enable SSL validation
114 EnableSSLAlert bool `json:"EnableSSLAlert" querystring:"EnableSSLAlert"`
116 // Use to specify whether redirects should be followed
117 FollowRedirect bool `json:"FollowRedirect" querystring:"FollowRedirect"`
120 // Validate checks if the Test is valid. If it's invalid, it returns a ValidationError with all invalid fields. It returns nil otherwise.
121 func (t *Test) Validate() error {
122 e := make(ValidationError)
124 if t.WebsiteName == "" {
125 e["WebsiteName"] = "is required"
128 if t.WebsiteURL == "" {
129 e["WebsiteURL"] = "is required"
132 if t.Timeout != 0 && (t.Timeout < 6 || t.Timeout > 99) {
133 e["Timeout"] = "must be 0 or between 6 and 99"
136 if t.Confirmation < 0 || t.Confirmation > 9 {
137 e["Confirmation"] = "must be between 0 and 9"
140 if t.CheckRate < 0 || t.CheckRate > 23999 {
141 e["CheckRate"] = "must be between 0 and 23999"
144 if t.Public < 0 || t.Public > 1 {
145 e["Public"] = "must be 0 or 1"
148 if t.Virus < 0 || t.Virus > 1 {
149 e["Virus"] = "must be 0 or 1"
152 if t.TestType != "HTTP" && t.TestType != "TCP" && t.TestType != "PING" {
153 e["TestType"] = "must be HTTP, TCP, or PING"
156 if t.RealBrowser < 0 || t.RealBrowser > 1 {
157 e["RealBrowser"] = "must be 0 or 1"
160 if t.TriggerRate < 0 || t.TriggerRate > 59 {
161 e["TriggerRate"] = "must be between 0 and 59"
164 if t.PostRaw != "" && t.TestType != "HTTP" {
165 e["PostRaw"] = "must be HTTP to submit a POST request"
168 if t.FinalEndpoint != "" && t.TestType != "HTTP" {
169 e["FinalEndpoint"] = "must be a Valid URL"
172 if t.CustomHeader != "" {
173 var jsonVerifiable map[string]interface{}
174 if json.Unmarshal([]byte(t.CustomHeader), &jsonVerifiable) != nil {
175 e["CustomHeader"] = "must be provided as json string"
186 // ToURLValues returns url.Values of all fields required to create/update a Test.
187 func (t Test) ToURLValues() url.Values {
188 values := make(url.Values)
189 st := reflect.TypeOf(t)
190 sv := reflect.ValueOf(t)
191 for i := 0; i < st.NumField(); i++ {
193 tag := sf.Tag.Get(queryStringTag)
195 if ft.Name() == "" && ft.Kind() == reflect.Ptr {
201 options := sf.Tag.Get("querystringoptions")
202 omit := options == "omitempty" && isEmptyValue(v)
204 if tag != "" && !omit {
205 values.Set(tag, valueToQueryStringValue(v))
212 func isEmptyValue(v reflect.Value) bool {
214 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
218 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
220 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
222 case reflect.Float32, reflect.Float64:
223 return v.Float() == 0
224 case reflect.Interface, reflect.Ptr:
231 func valueToQueryStringValue(v reflect.Value) string {
232 if v.Type().Name() == "bool" {
240 if v.Type().Kind() == reflect.Slice {
241 if ss, ok := v.Interface().([]string); ok {
242 return strings.Join(ss, ",")
249 // Tests is a client that implements the `Tests` API.
250 type Tests interface {
251 All() ([]*Test, error)
252 AllWithFilter(url.Values) ([]*Test, error)
253 Detail(int) (*Test, error)
254 Update(*Test) (*Test, error)
255 Delete(TestID int) error
262 func newTests(c apiClient) Tests {
268 func (tt *tests) All() ([]*Test, error) {
269 resp, err := tt.client.get("/Tests", nil)
273 defer resp.Body.Close()
276 err = json.NewDecoder(resp.Body).Decode(&tests)
281 func (tt *tests) AllWithFilter(filterOptions url.Values) ([]*Test, error) {
282 resp, err := tt.client.get("/Tests", filterOptions)
286 defer resp.Body.Close()
289 err = json.NewDecoder(resp.Body).Decode(&tests)
294 func (tt *tests) Update(t *Test) (*Test, error) {
295 resp, err := tt.client.put("/Tests/Update", t.ToURLValues())
299 defer resp.Body.Close()
301 var ur updateResponse
302 err = json.NewDecoder(resp.Body).Decode(&ur)
308 return nil, &updateError{Issues: ur.Issues, Message: ur.Message}
312 t2.TestID = ur.InsertID
317 func (tt *tests) Delete(testID int) error {
318 resp, err := tt.client.delete("/Tests/Details", url.Values{"TestID": {fmt.Sprint(testID)}})
322 defer resp.Body.Close()
324 var dr deleteResponse
325 err = json.NewDecoder(resp.Body).Decode(&dr)
331 return &deleteError{Message: dr.Error}
337 func (tt *tests) Detail(testID int) (*Test, error) {
338 resp, err := tt.client.get("/Tests/Details", url.Values{"TestID": {fmt.Sprint(testID)}})
342 defer resp.Body.Close()
344 var dr *detailResponse
345 err = json.NewDecoder(resp.Body).Decode(&dr)
350 return dr.test(), nil