-
Notifications
You must be signed in to change notification settings - Fork 4
/
requests.go
123 lines (99 loc) · 3.17 KB
/
requests.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
package fossil
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
//***** Structures *****//
// Meta contains the metadata of an API response
type Meta struct {
Pagination struct {
Total int `json:"total"`
Count int `json:"count"`
PerPage int `json:"per_page"`
CurrentPage int `json:"current_page"`
TotalPages int `json:"total_pages"`
Links struct {
Previous string `json:"previous,omitempty"`
Next string `json:"next,omitempty"`
} `json:"links"`
} `json:"pagination"`
}
// Using the function through a variable allows stub testing
var query = queryURL
//***** Queries *****//
func (c *ApplicationCredentials) query(endpoint, method string, data []byte) ([]byte, error) {
target := fmt.Sprintf("%s/api/application/%s", c.URL, endpoint)
return query(target, c.Token, method, data)
}
func (c *ClientCredentials) query(endpoint, method string, data []byte) ([]byte, error) {
target := fmt.Sprintf("%s/api/client/%s", c.URL, endpoint)
return query(target, c.Token, method, data)
}
func queryURL(url, token, method string, data []byte) ([]byte, error) {
client := &http.Client{}
rq, _ := http.NewRequest(method, url, bytes.NewBuffer(data))
rq.Header.Set("Authorization", "Bearer "+token)
rq.Header.Set("Accept", "Application/vnd.pterodactyl.v1+json")
rq.Header.Set("Content-Type", "application/json")
rp, err := client.Do(rq)
if err != nil {
return nil, err
}
var body []byte
// Success status range
if rp.StatusCode < 200 || rp.StatusCode > 226 {
// Response was a not-success code
body, _ = ioutil.ReadAll(rp.Body)
// No additional error info given
if body == nil {
return nil, errors.New("remote server responded with status " + rp.Status)
}
rqErr, err := parseError(body)
// Info was there but unable to be decoded
if err != nil {
msg := fmt.Sprintf("remote server responded with status %s."+
" aditionaly another error occurred while decoding the error: %s", rp.Status, err.Error())
return nil, errors.New(msg)
}
// Was able to parse the error details
msg := fmt.Sprintf("remote server responded with status %s (%s): %s",
rqErr.Status, rqErr.Code, rqErr.Detail)
return nil, errors.New(msg)
}
if rp.Body != nil {
body, _ = ioutil.ReadAll(rp.Body)
}
rp.Body = ioutil.NopCloser(bytes.NewBuffer(body))
err = rp.Body.Close()
if err != nil {
return body, errors.New("unable to close response body")
}
return body, nil
}
//***** Errors *****//
// requestError contains error details for Pterodactyl requests errors
type requestError struct {
Code string `json:"code"` // For some reason the code is given as a string
Status string `json:"status"`
Detail string `json:"detail"`
}
// jsonRequestErrors allows the JSON provided to be decoded
type jsonRequestErrors struct {
Errors []*requestError `json:"errors"`
}
// parseError takes a Pterodactyl-formatted error and parses it into a struct
func parseError(bytes []byte) (*requestError, error) {
var rqErrors jsonRequestErrors
err := json.Unmarshal(bytes, &rqErrors)
if err != nil {
return nil, err
}
if len(rqErrors.Errors) < 1 {
return nil, errors.New("no error details given")
}
return rqErrors.Errors[0], nil
}