forked from badkaktus/gorocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorocket.go
150 lines (123 loc) · 2.7 KB
/
gorocket.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gorocket
import (
"context"
"encoding/json"
"github.com/google/go-querystring/query"
"log"
"net/http"
"time"
)
type Client struct {
baseURL string
userID string
xToken string
apiVersion string
HTTPClient *http.Client
timeout time.Duration
}
type PaginationStruct struct {
Count int `url:"count,omitempty"`
Offset int `url:"offset,omitempty"`
Sort string `url:"sort,omitempty"`
}
var pagination PaginationStruct
// NewClient creates new rocket.chat client with given API key
func NewClient(url string) *Client {
return &Client{
//userID: user,
HTTPClient: &http.Client{
Timeout: 5 * time.Minute,
},
//xToken: token,
baseURL: url,
apiVersion: "api/v1",
}
}
// NewWithOptions creates new rocket.chat client with options
func NewWithOptions(url string, opts ...Option) *Client {
c := &Client{
HTTPClient: &http.Client{
Timeout: 5 * time.Minute,
},
baseURL: url,
apiVersion: "api/v1",
}
for _, o := range opts {
o(c)
}
return c
}
type Option func(*Client)
func WithTimeout(d time.Duration) Option {
return func(c *Client) {
c.timeout = d
}
}
func WithUserID(userID string) Option {
return func(c *Client) {
c.userID = userID
}
}
func WithXToken(xtoken string) Option {
return func(c *Client) {
c.xToken = xtoken
}
}
func (c *Client) sendRequest(req *http.Request, v interface{}) error {
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Add("X-Auth-Token", c.xToken)
req.Header.Add("X-User-Id", c.userID)
if c.timeout > 0 {
ctx, cancel := context.WithTimeout(req.Context(), c.timeout)
defer cancel()
req = req.WithContext(ctx)
}
res, err := c.HTTPClient.Do(c.addQueryParams(req))
if err != nil {
log.Println(err)
return err
}
c.cleanup()
defer res.Body.Close()
resp := v
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
return err
}
return nil
}
func (c *Client) Count(val int) *Client {
pagination.Count = val
return c
}
func (c *Client) Offset(val int) *Client {
pagination.Offset = val
return c
}
func (c *Client) Sort(val map[string]int) *Client {
byteJson, err := json.Marshal(val)
if err != nil {
log.Printf("cant create sort. error: %s", err)
return c
}
pagination.Sort = string(byteJson)
return c
}
func (c *Client) addQueryParams(req *http.Request) *http.Request {
v, err := query.Values(pagination)
if err != nil {
log.Printf("error create query string: %s", err)
return req
}
q := req.URL.Query()
for k := range v {
q.Add(k, v.Get(k))
}
req.URL.RawQuery = q.Encode()
return req
}
func (c *Client) cleanup() {
pagination.Sort = ""
pagination.Offset = 0
pagination.Count = 0
}