forked from griffin-stewie/go-backlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
executable file
·161 lines (132 loc) · 3.26 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
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
151
152
153
154
155
156
157
158
159
160
161
package gobacklog
import (
"bytes"
"encoding/json"
"path"
// "fmt"
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
// Verbose is bool
var Verbose = false
// BacklogErrorResponse is error model
type BacklogErrorResponse struct {
Errors BacklogErrorSlice `json:"errors"`
}
// BacklogError is error model
// +gen * slice:"Where,Count,SortBy,GroupBy[string],Select[string]"
type BacklogError struct {
Message string `json:"message,omitempty"`
Code int `json:"code,omitempty"`
MoreInfo string `json:"moreInfo,omitempty"`
}
// error returns Errors
func (e *BacklogErrorResponse) error() error {
if len(e.Errors) == 0 {
return nil
}
s := e.Errors.SelectString(func(b *BacklogError) string {
return b.Message
})
return errors.New(strings.Join(s, ", "))
}
// HTTP interface of HTTP METHODS's methods
type HTTP interface {
Get()
Post()
Put()
Delete()
}
// Client is
type Client struct {
BaseURL *url.URL
HTTPClient *http.Client
APIKey string
}
// NewClient returns Backlog HTTP Client
func NewClient(baseURL *url.URL, APIKey string) *Client {
s := &Client{
BaseURL: baseURL,
APIKey: APIKey,
}
return s
}
// Get GET method
func (c *Client) Get(endpoint string, params url.Values) ([]byte, error) {
return c.execute("GET", endpoint, params)
}
// Post POST method
func (c *Client) Post(endpoint string, params url.Values) ([]byte, error) {
return c.execute("POST", endpoint, params)
}
// Put PUT method
func (c *Client) Put(endpoint string, params url.Values) ([]byte, error) {
return c.execute("PUT", endpoint, params)
}
// Delete DELETE method
func (c *Client) Delete(endpoint string, params url.Values) ([]byte, error) {
return c.execute("DELETE", endpoint, params)
}
func (c *Client) composeURL(pathStr string, params url.Values) string {
copiedURL := *c.BaseURL
copiedURL.Path = path.Join(copiedURL.Path, pathStr)
params.Set("apiKey", c.APIKey)
copiedURL.RawQuery = params.Encode()
return copiedURL.String()
}
func (c *Client) parseBody(resp *http.Response) ([]byte, error) {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
if Verbose {
log.Println(err)
}
return []byte(``), err
}
if Verbose {
log.Printf("[DEBUG] resp: %#+v", resp)
log.Printf("[DEBUG] body: %v", string(body))
}
if resp.StatusCode != 200 {
var er BacklogErrorResponse
json.Unmarshal(body, &er)
return []byte(``), er.error()
}
return body, nil
}
func (c *Client) execute(method, endpoint string, params url.Values) ([]byte, error) {
resp, err := c.executeReturnsResponse(method, endpoint, params)
if err != nil {
return []byte(``), err
}
return c.parseBody(resp)
}
func (c *Client) executeReturnsResponse(method, endpoint string, params url.Values) (resp *http.Response, err error) {
if c.HTTPClient == nil {
c.HTTPClient = http.DefaultClient
}
var (
req *http.Request
requestErr error
)
if method != "GET" {
req, requestErr = http.NewRequest(method,
c.composeURL(endpoint, url.Values{}),
bytes.NewBufferString(params.Encode()),
)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
} else {
req, requestErr = http.NewRequest(method,
c.composeURL(endpoint, params),
nil,
)
}
if requestErr != nil {
panic(requestErr)
}
return c.HTTPClient.Do(req)
}