forked from justmiles/go-confluence
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
179 lines (142 loc) · 4.25 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package confluence
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
log "github.com/sirupsen/logrus"
)
// Client for the Confluence API
type Client struct {
Cookie string
Username string
Password string
AccessToken string
Endpoint string
Debug bool
}
func (client *Client) request(method string, apiEndpoint string, queryParams string, payloadString string) ([]byte, error) {
if client.Debug {
log.SetLevel(log.DebugLevel)
}
var payload io.Reader
url := client.Endpoint + apiEndpoint
if queryParams != "" {
url = url + "?" + queryParams
}
if payloadString != "" {
payload = strings.NewReader(payloadString)
}
log.Debug(fmt.Sprintf("%s %s %s", method, url, payloadString))
req, _ := http.NewRequest(method, url, payload)
req.Header["X-Atlassian-Token"] = []string{"no-check"}
req.Header["Content-Type"] = []string{"application/json"}
if client.Cookie != "" {
req.Header.Set("Cookie", fmt.Sprintf("JSESSIONID=%v", client.Cookie))
} else if client.AccessToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", client.AccessToken))
} else {
req.SetBasicAuth(client.Username, client.Password)
}
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
log.Debugf("Response Status Code: %d", res.StatusCode)
log.Debugf("Response Body: '%s'", string(body))
var apiResponse APIResponse
if string(body) != "" {
err := json.Unmarshal(body, &apiResponse)
if err != nil {
log.Error("Unable to unmarshal API response. Received: '", string(body), "'")
return body, err
}
if apiResponse.Message != "" {
log.Error(apiResponse.Message)
if len(apiResponse.Data.Errors) > 0 {
for _, e := range apiResponse.Data.Errors {
log.Error(" " + e.Message.Key)
}
}
return body, errors.New(apiResponse.Message)
}
}
return body, nil
}
// PreRequestFn ...
type PreRequestFn func(request *http.Request)
func (client *Client) requestWithFunc(method string, apiEndpoint string, queryParams string, payloadByte *bytes.Buffer, preFn PreRequestFn) ([]byte, error) {
if client.Debug {
log.SetLevel(log.DebugLevel)
}
var payload io.Reader
url := client.Endpoint + apiEndpoint
if queryParams != "" {
url = url + "?" + queryParams
}
if payloadByte != nil {
payload = payloadByte
}
log.Debug(fmt.Sprintf("%s %s", method, url))
req, _ := http.NewRequest(method, url, payload)
req.Header["X-Atlassian-Token"] = []string{"no-check"}
req.Header["Content-Type"] = []string{"application/json"}
preFn(req)
req.SetBasicAuth(client.Username, client.Password)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
log.Debugf("Response Status Code: %d", res.StatusCode)
log.Debugf("Response Body: '%s'", string(body))
var apiResponse APIResponse
if string(body) != "" {
err := json.Unmarshal(body, &apiResponse)
if err != nil {
log.Error("Unable to unmarshal API response. Received: '", string(body), "'")
return body, err
}
if apiResponse.Message != "" {
log.Error(apiResponse.Message)
if len(apiResponse.Data.Errors) > 0 {
for _, e := range apiResponse.Data.Errors {
log.Error(" " + e.Message.Key)
}
}
return body, errors.New(apiResponse.Message)
}
}
return body, nil
}
// Delete deletes various API types
func (client *Client) Delete(class interface{}) error {
switch v := class.(type) {
case Content:
return client.DeleteContent(class.(Content))
default:
return fmt.Errorf("unable to delete type %T", v)
}
}
// QueryParameters provides default query parameters for client
type QueryParameters struct {
Expand []string `url:"expand,omitempty"`
Status string `url:"status,omitempty"`
}
// APIResponse provides default response from API
type APIResponse struct {
StatusCode int `json:"statusCode,omitempty"`
Data struct {
Authorized bool `json:"authorized,omitempty"`
Valid bool `json:"valid,omitempty"`
Errors []struct {
Message struct {
Key string `json:"key,omitempty"`
Args []interface{} `json:"args,omitempty"`
} `json:"message,omitempty"`
} `json:"errors,omitempty"`
Successful bool `json:"successful,omitempty"`
} `json:"data,omitempty"`
Message string `json:"message,omitempty"`
}