-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_status.go
51 lines (47 loc) · 1.27 KB
/
api_status.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
package encodingcom
import (
"encoding/json"
"net"
"net/http"
"strings"
"time"
)
// APIStatusResponse is the response returned by the APIStatus function.
//
// It describes the current status of the Encoding.com API.
type APIStatusResponse struct {
Status string `json:"status"`
StatusCode string `json:"status_code"`
Incident string `json:"incident"`
}
// OK returns whether the given status represents no problem in the
// Encoding.com API.
func (s *APIStatusResponse) OK() bool {
return s.StatusCode == "ok"
}
// APIStatus queries the current status of the Encoding.com API.
//
// The host parameter is optional, and when omitted, will default to
// "http://status.encoding.com".
//
// See http://goo.gl/3JKSxy for more details.
func APIStatus(endpoint string) (*APIStatusResponse, error) {
client := http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
ResponseHeaderTimeout: 2 * time.Second,
},
}
url := strings.TrimRight(endpoint, "/") + "/status.php?format=json"
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var apiResp APIStatusResponse
err = json.NewDecoder(resp.Body).Decode(&apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}