This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
125 lines (95 loc) · 2.57 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package raiponce
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
)
const (
grantTypePassword = "password"
grantTypeRefreshToken = "refresh_token"
)
type ClientConfig struct {
BaseURL string
ClientID string
ClientSecret string
APIVersion string
Locale string
}
// Client is used for request the api
type Client struct {
config ClientConfig
token token
client *http.Client
}
// New create a new Client for use API
func NewClient(config ClientConfig) *Client {
return &Client{
config: config,
client: &http.Client{},
}
}
func (client *Client) cget(uri string, response interface{}, query *QueryFilter) error {
if query != nil {
uri += fmt.Sprintf("?%s", query.buildQuery())
}
return client.sendRequest(http.MethodGet, uri, nil, response)
}
func (client *Client) get(uri string, response interface{}) error {
return client.sendRequest(http.MethodGet, uri, nil, response)
}
func (client *Client) post(uri string, body interface{}, response interface{}) error {
return client.sendRequest(http.MethodPost, uri, body, response)
}
func (client *Client) patch(uri string, body interface{}, response interface{}) error {
return client.sendRequest(http.MethodPatch, uri, body, response)
}
func (client *Client) remove(uri string) error {
return client.sendRequest(http.MethodDelete, uri, nil, nil)
}
func (client *Client) addHeader(request *http.Request) {
request.Header.Add("Content-Type", "application/json")
request.Header.Add("X-Accept-Version", client.config.APIVersion)
request.Header.Add("Accept-Language", client.config.Locale)
if len(client.token.AccessToken) > 0 {
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", client.token.AccessToken))
}
}
func (client *Client) sendRequest(method string, uri string, body interface{}, response interface{}) error {
var request *http.Request
var err error
url := client.config.BaseURL + uri
if body != nil {
jsonBody := structToReader(body)
request, err = http.NewRequest(method, url, jsonBody)
} else {
request, err = http.NewRequest(method, url, nil)
}
if err != nil {
log.Fatalln(err)
}
client.addHeader(request)
res, err := client.client.Do(request)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
// TODO handle error
if response != nil {
json.NewDecoder(res.Body).Decode(&response)
}
if res.StatusCode > 300 {
return errors.New("")
}
return nil
}
func structToReader(s interface{}) *bytes.Reader {
sliceBytes, err := json.Marshal(s)
if err != nil {
log.Fatalln(err)
}
reader := bytes.NewReader(sliceBytes)
return reader
}