forked from inexio/eve-ng-restapi-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
235 lines (201 loc) · 5.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package evengclient
import (
"encoding/json"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
const (
// path to the eve_ng api endpoint
endpointPath = "api/"
)
type client struct {
*clientData
}
/*
clientData - Contains data of a client
*/
type clientData struct {
baseURL string
username string
password string
resty *resty.Client
useAuth bool
}
/*
NotValidError - Is returned when the client was not initialized properly
*/
type NotValidError struct{}
func (m *NotValidError) Error() string {
return "client was not created properly with the func New...Client(baseURL string)"
}
/*
isValid - returns true if a client is valid and false if a client is invalid
*/
func (c *client) isValid() bool {
return c.clientData != nil
}
/*
SetUsernameAndPassword - Is used to set a username and password for https auth
*/
func (c *client) SetUsernameAndPassword(username, password string) error {
if !c.isValid() {
return &NotValidError{}
}
if username == "" {
return errors.New("invalid username")
}
if password == "" {
return errors.New("invalid password")
}
c.username = username
c.password = password
c.useAuth = true
return nil
}
/*
init is used for initialising the config file
*/
func init() {
// Search config in home directory with name "eve-ng-api" (without extension).
viper.AddConfigPath("config/")
viper.SetConfigType("yaml")
viper.SetConfigName("eve-ng-api")
//Set env var prefix to only match certain vars
viper.SetEnvPrefix("EVE_NG_API")
// read in environment variables that match
viper.AutomaticEnv()
// If a config file is found, read it in.
viper.ReadInConfig()
}
/*
request - Is used to send either GET, POST, PUT or DELETE requests
*/
func (c *client) request(method string, path string, body string, header, queryParams map[string]string) (*resty.Response, error) {
request := c.resty.R()
request.SetHeader("Content-Type", "application/json")
if header != nil {
request.SetHeaders(header)
}
if queryParams != nil {
request.SetQueryParams(queryParams)
}
if body != "" {
request.SetBody(body)
}
if c.useAuth {
request.SetBasicAuth(c.username, c.password)
}
var response *resty.Response
response = nil
var err error
err = nil
switch method {
case "GET":
response, err = request.Get(c.baseURL + urlEscapePath(path))
case "POST":
response, err = request.Post(c.baseURL + urlEscapePath(path))
case "PUT":
response, err = request.Put(c.baseURL + urlEscapePath(path))
case "DELETE":
response, err = request.Delete(c.baseURL + urlEscapePath(path))
default:
return nil, errors.New("invalid http method: " + method)
}
if err != nil {
return nil, errors.Wrap(err, "error during http request")
}
if response.StatusCode() != 200 && response.StatusCode() != 201 {
return nil, errors.Wrap(getHTTPError(response), "http request responded with an error")
}
return response, err
}
//Http error handling
/*
HTTPError - Represents an http error returned by the api.
*/
type HTTPError struct {
StatusCode int
Status string
Body *ErrorResponse
}
/*
ErrorResponse - Contains error information.
*/
type ErrorResponse struct {
Message string `json:"message"`
Status int `json:"status"`
}
func (h HTTPError) Error() string {
msg := "http error: status code: " + strconv.Itoa(h.StatusCode) + " // status: " + h.Status
if h.Body != nil {
msg += " // message: " + h.Body.Message
}
return msg
}
func getHTTPError(response *resty.Response) error {
httpError := HTTPError{
StatusCode: response.StatusCode(),
Status: response.Status(),
}
var errorResponse ErrorResponse
err := json.Unmarshal(response.Body(), &errorResponse)
if err != nil {
return httpError
}
httpError.Body = &errorResponse
return httpError
}
//---------- helper functions ----------//
/*
unmarshalDataIntoStruct - Is used to unmarshal responses from the eve-ng REST API into certain structs
*/
func (c *client) unmarshalDataIntoStruct(responseBody []byte, i interface{}) error {
isResponseDataEmpty, err := checkForEmptyResponseData(responseBody)
if err != nil {
return err
}
if !isResponseDataEmpty {
var basicResponse BasicResponse
basicResponse.Data = i
err := json.Unmarshal(responseBody, &basicResponse)
if err != nil {
return err
}
}
return nil
}
//the following lines of code have to be done because otherwise a empty data provided in the api response could not be correctly unmarshaled
func checkForEmptyResponseData(responseBody []byte) (bool, error) {
var basicResponse BasicResponse
err := json.Unmarshal(responseBody, &basicResponse)
if err != nil {
return false, err
}
emptyData := make([]interface{}, 0)
return reflect.DeepEqual(emptyData, basicResponse.Data), nil
}
/*
urlEscapePath - Escapes special characters of a given url path
*/
func urlEscapePath(unescaped string) string {
arr := strings.Split(unescaped, "/")
for i, partString := range strings.Split(unescaped, "/") {
arr[i] = url.QueryEscape(partString)
}
return strings.Join(arr, "/")
}
/*
jsonEscape - Escapes special characters of a given json string
*/
func jsonEscape(unescaped string) (string, error) {
escaped, err := json.Marshal(unescaped)
if err != nil {
return "", errors.Wrap(err, "json marshal failed")
}
return string(escaped)[1 : len(escaped)-1], nil
}