forked from leoht/aircall
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
103 lines (84 loc) · 2.55 KB
/
client.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
103
package aircall
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
)
const (
apiBaseURL = "https://api.aircall.io/"
apiDefaultVersion = "v1"
)
// A Client struct is holding developer app information
// to perform API requests
type Client struct {
AppID string
AppToken string
Version string
}
// NewClient creates a new Client struct using the default API version
//
// client := aircall.NewClient(appId, appSecret)
// res, err := client.Ping()
// fmt.Println(res.Ping) // "pong"
func NewClient(id, token string) *Client {
return &Client{
AppID: id,
AppToken: token,
Version: apiDefaultVersion,
}
}
// Get is a shorthand of Request("GET", path, params, Request{})
func (client *Client) Get(path string, params map[string]string) ([]byte, error) {
return client.Request("GET", path, params, Request{})
}
// Post is a shorthand of Request("POST", path, params, Request{})
func (client *Client) Post(path string, request interface{}) ([]byte, error) {
return client.Request("POST", path, map[string]string{}, request)
}
// Delete is a shorthand of Request("DELETE", path, params, Request{})
func (client *Client) Delete(path string, params map[string]string) ([]byte, error) {
return client.Request("DELETE", path, params, Request{})
}
// Request sends a HTTP request to the API
func (client *Client) Request(method string, path string, params map[string]string, body interface{}) ([]byte, error) {
url := buildURL(client, path, params)
b, err := json.Marshal(body)
req, err := http.NewRequest(method, url, bytes.NewBuffer(b))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Basic "+basicAuthHeader(client.AppID, client.AppToken))
c := &http.Client{}
res, err := c.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode >= 400 {
return nil, errors.New("Client error: " + res.Status)
} else if res.StatusCode >= 500 {
return nil, errors.New("Server error: " + res.Status)
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
func basicAuthHeader(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func buildURL(client *Client, path string, params map[string]string) string {
var urlBuf bytes.Buffer
urlBuf.WriteString(apiBaseURL)
urlBuf.WriteString(client.Version)
urlBuf.WriteString(path)
values := url.Values{}
for k, v := range params {
values.Add(k, v)
}
urlBuf.WriteString("?" + values.Encode())
return urlBuf.String()
}