/
api.go
102 lines (83 loc) · 2.09 KB
/
api.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package exporter
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"strings"
"golang.org/x/net/publicsuffix"
)
// API is the interface to 3CX
type API struct {
Hostname string
Username string
Password string
client *http.Client
}
// ErrAuthentication is returned on HTTP status 401
var ErrAuthentication = errors.New("authentication failed")
// Login creates a user session
func (api *API) Login() error {
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
client := &http.Client{Jar: jar}
credentials := struct {
Username string
Password string
}{api.Username, api.Password}
body, _ := json.Marshal(&credentials)
resp, err := client.Post(api.buildURI("login"), "application/json", bytes.NewReader(body))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if string(respBody) != "AuthSuccess" {
return fmt.Errorf("failed to login: %s", respBody)
}
api.client = client
return nil
}
func (api *API) buildURI(path string) string {
return fmt.Sprintf("https://%s/api/%s", api.Hostname, path)
}
// getResponse does a GET request and parses the JSON response
func (api *API) getResponse(path string, response interface{}) error {
retried := false
request:
resp, err := api.client.Get(api.buildURI(path))
if err != nil {
return err
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
if contentType := resp.Header.Get("Content-Type"); !strings.HasPrefix(contentType, "application/json") {
return fmt.Errorf("unexpected content-type: %s", contentType)
}
return json.NewDecoder(resp.Body).Decode(response)
case 401:
// session expired
if retried {
return ErrAuthentication
}
// try to login again
err = api.Login()
if err != nil {
return err
}
// login successful, retry request
retried = true
goto request
default:
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
}