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 // Test location, either an IP (for TCP and Ping) or a fully qualified URL for other TestTypes
25 WebsiteURL string `json:"WebsiteURL" querystring:"WebsiteURL"`
27 // A Port to use on TCP Tests
28 Port int `json:"Port" querystring:"Port"`
30 // Contact group ID - will return int of contact group used else 0
31 ContactID int `json:"ContactID" querystring:"ContactGroup"`
33 // Current status at last test
34 Status string `json:"Status"`
37 Uptime float64 `json:"Uptime"`
39 // Any test locations seperated by a comma (using the Node Location IDs)
40 NodeLocations []string `json:"NodeLocations" querystring:"NodeLocations"`
42 // Timeout in an int form representing seconds.
43 Timeout int `json:"Timeout" querystring:"Timeout"`
45 // A URL to ping if a site goes down.
46 PingURL string `json:"PingURL" querystring:"PingURL"`
48 Confirmation int `json:"Confirmationi,string" querystring:"Confirmation"`
50 // The number of seconds between checks.
51 CheckRate int `json:"CheckRate" querystring:"CheckRate"`
53 // A Basic Auth User account to use to login
54 BasicUser string `json:"BasicUser" querystring:"BasicUser"`
56 // If BasicUser is set then this should be the password for the BasicUser
57 BasicPass string `json:"BasicPass" querystring:"BasicPass"`
59 // Set 1 to enable public reporting, 0 to disable
60 Public int `json:"Public" querystring:"Public"`
62 // A URL to a image to use for public reporting
63 LogoImage string `json:"LogoImage" querystring:"LogoImage"`
65 // Set to 0 to use branding (default) or 1 to disable public reporting branding
66 Branding int `json:"Branding" querystring:"Branding"`
68 // Used internally by the statuscake API
69 WebsiteHost string `json:"WebsiteHost"`
71 // Enable virus checking or not. 1 to enable
72 Virus int `json:"Virus" querystring:"Virus"`
74 // A string that should either be found or not found.
75 FindString string `json:"FindString" querystring:"FindString"`
77 // If the above string should be found to trigger a alert. true will trigger if FindString found
78 DoNotFind bool `json:"DoNotFind" querystring:"DoNotFind"`
80 // What type of test type to use. Accepted values are HTTP, TCP, PING
81 TestType string `json:"TestType" querystring:"TestType"`
83 // Use 1 to TURN OFF real browser testing
84 RealBrowser int `json:"RealBrowser" querystring:"RealBrowser"`
86 // How many minutes to wait before sending an alert
87 TriggerRate int `json:"TriggerRate" querystring:"TriggerRate"`
89 // Tags should be seperated by a comma - no spacing between tags (this,is,a set,of,tags)
90 TestTags string `json:"TestTags" querystring:"TestTags"`
92 // Comma Seperated List of StatusCodes to Trigger Error on (on Update will replace, so send full list each time)
93 StatusCodes string `json:"StatusCodes" querystring:"StatusCodes"`
96 // Validate checks if the Test is valid. If it's invalid, it returns a ValidationError with all invalid fields. It returns nil otherwise.
97 func (t *Test) Validate() error {
98 e := make(ValidationError)
100 if t.WebsiteName == "" {
101 e["WebsiteName"] = "is required"
104 if t.WebsiteURL == "" {
105 e["WebsiteURL"] = "is required"
108 if t.Timeout != 0 && (t.Timeout < 6 || t.Timeout > 99) {
109 e["Timeout"] = "must be 0 or between 6 and 99"
112 if t.Confirmation < 0 || t.Confirmation > 9 {
113 e["Confirmation"] = "must be between 0 and 9"
116 if t.CheckRate < 0 || t.CheckRate > 23999 {
117 e["CheckRate"] = "must be between 0 and 23999"
120 if t.Public < 0 || t.Public > 1 {
121 e["Public"] = "must be 0 or 1"
124 if t.Virus < 0 || t.Virus > 1 {
125 e["Virus"] = "must be 0 or 1"
128 if t.TestType != "HTTP" && t.TestType != "TCP" && t.TestType != "PING" {
129 e["TestType"] = "must be HTTP, TCP, or PING"
132 if t.RealBrowser < 0 || t.RealBrowser > 1 {
133 e["RealBrowser"] = "must be 0 or 1"
136 if t.TriggerRate < 0 || t.TriggerRate > 59 {
137 e["TriggerRate"] = "must be between 0 and 59"
147 // ToURLValues returns url.Values of all fields required to create/update a Test.
148 func (t Test) ToURLValues() url.Values {
149 values := make(url.Values)
150 st := reflect.TypeOf(t)
151 sv := reflect.ValueOf(t)
152 for i := 0; i < st.NumField(); i++ {
154 tag := sf.Tag.Get(queryStringTag)
156 if ft.Name() == "" && ft.Kind() == reflect.Ptr {
162 options := sf.Tag.Get("querystringoptions")
163 omit := options == "omitempty" && isEmptyValue(v)
165 if tag != "" && !omit {
166 values.Set(tag, valueToQueryStringValue(v))
173 func isEmptyValue(v reflect.Value) bool {
175 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
179 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
181 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
183 case reflect.Float32, reflect.Float64:
184 return v.Float() == 0
185 case reflect.Interface, reflect.Ptr:
192 func valueToQueryStringValue(v reflect.Value) string {
193 if v.Type().Name() == "bool" {
201 if v.Type().Kind() == reflect.Slice {
202 if ss, ok := v.Interface().([]string); ok {
203 return strings.Join(ss, ",")
210 // Tests is a client that implements the `Tests` API.
211 type Tests interface {
212 All() ([]*Test, error)
213 Detail(int) (*Test, error)
214 Update(*Test) (*Test, error)
215 Delete(TestID int) error
222 func newTests(c apiClient) Tests {
228 func (tt *tests) All() ([]*Test, error) {
229 resp, err := tt.client.get("/Tests", nil)
233 defer resp.Body.Close()
236 err = json.NewDecoder(resp.Body).Decode(&tests)
241 func (tt *tests) Update(t *Test) (*Test, error) {
242 resp, err := tt.client.put("/Tests/Update", t.ToURLValues())
246 defer resp.Body.Close()
248 var ur updateResponse
249 err = json.NewDecoder(resp.Body).Decode(&ur)
255 return nil, &updateError{Issues: ur.Issues}
259 t2.TestID = ur.InsertID
264 func (tt *tests) Delete(testID int) error {
265 resp, err := tt.client.delete("/Tests/Details", url.Values{"TestID": {fmt.Sprint(testID)}})
269 defer resp.Body.Close()
271 var dr deleteResponse
272 err = json.NewDecoder(resp.Body).Decode(&dr)
278 return &deleteError{Message: dr.Error}
284 func (tt *tests) Detail(testID int) (*Test, error) {
285 resp, err := tt.client.get("/Tests/Details", url.Values{"TestID": {fmt.Sprint(testID)}})
289 defer resp.Body.Close()
291 var dr *detailResponse
292 err = json.NewDecoder(resp.Body).Decode(&dr)
297 return dr.test(), nil