-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.go
48 lines (37 loc) · 899 Bytes
/
ping.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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func pingTest(c *MonzoClient) error {
path := "ping/whoami"
requestURL := fmt.Sprintf("https://%s/%s", apiHostname, path)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
rsp, err := c.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", rsp.StatusCode)
}
if rsp.Body == nil {
return fmt.Errorf("response body is empty")
}
rspJson := map[string]interface{}{}
err = json.NewDecoder(rsp.Body).Decode(&rspJson)
if err != nil {
return err
}
_, ok := rspJson["user_id"].(string)
if !ok {
return fmt.Errorf("cannot find user ID in response")
}
fmt.Println("Test API call successful 🎉")
return nil
}