-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
148 lines (117 loc) · 3.85 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
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
package golangzerotierserviceapi
import (
"crypto/tls"
"errors"
"io"
"net/http"
"strconv"
)
// makeGetRequest performs a GET request to the specified URL.
// It returns the response body ([]byte) and an error if any.
func (c *Client) MakeGetRequest(url string) ([]byte, error) {
var respBody []byte
var client *http.Client
if c.Insecure {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr}
} else {
// Create an HTTP client
client = &http.Client{}
}
// Create a new GET request with the provided URL
req, _ := http.NewRequest("GET", c.BaseURL+url, nil)
// Set the Authorization header with the provided c.Token
req.Header.Set("X-ZT1-Auth", c.Token)
// Perform the HTTP request
resp, err := client.Do(req)
// Check if there's an error during the request
if err != nil {
// Return empty response body and the error
return respBody, err
}
// Check if the response status code is not 200 OK
if resp.StatusCode != 200 {
// Create an error indicating non-200 status code
err := errors.New("Non 200 response: " + strconv.Itoa(resp.StatusCode))
// Return empty response body and the error
return respBody, err
}
// Read the response body
respBody, err = io.ReadAll(resp.Body)
if err != nil {
// Return empty response body and the error if reading fails
return respBody, err
}
// Return the response body and nil error
return respBody, err
}
// makePostRequest performs a POST request to the specified URL with the provided data.
// It returns the response body ([]byte) and an error if any.
func (c *Client) MakePostRequest(url string, data io.Reader) ([]byte, error) {
var respBody []byte
// Create an HTTP client
client := &http.Client{}
// Create a new POST request with the provided URL and data
req, _ := http.NewRequest("POST", c.BaseURL+url, data)
// Set the Authorization header with the provided c.Token
req.Header.Set("X-ZT1-Auth", c.Token)
// Set the Content-Type header to indicate JSON data
req.Header.Set("Content-Type", "application/json")
// Perform the HTTP request
resp, err := client.Do(req)
// Check if there's an error during the request
if err != nil {
// Return empty response body and the error
return respBody, err
}
// Check if the response status code is not 200 OK
if resp.StatusCode != 200 {
// Create an error indicating non-200 status code
err := errors.New("Non 200 response: " + strconv.Itoa(resp.StatusCode))
// Return empty response body and the error
return respBody, err
}
// Read the response body
respBody, err = io.ReadAll(resp.Body)
if err != nil {
// Return empty response body and the error if reading fails
return respBody, err
}
// Return the response body and nil error
return respBody, err
}
// makeDeleteRequest performs a DELETE request to the specified URL.
// It returns an error if any.
func (c *Client) MakeDeleteRequest(url string) ([]byte, error) {
var respBody []byte
// Create an HTTP client
client := &http.Client{}
// Create a new DELETE request with the provided URL
req, _ := http.NewRequest("DELETE", c.BaseURL+url, nil)
// Set the Authorization header with the provided c.Token
req.Header.Set("X-ZT1-Auth", c.Token)
// Perform the HTTP request
resp, err := client.Do(req)
// Check if there's an error during the request
if err != nil {
// Return the error
return respBody, err
}
// Check if the response status code is not 200 OK
if resp.StatusCode != 200 {
// Create an error indicating non-200 status code
err := errors.New("Non 200 response: " + strconv.Itoa(resp.StatusCode))
// Return the error
return respBody, err
}
// Read the response body
respBody, err = io.ReadAll(resp.Body)
if err != nil {
// Return empty response body and the error if reading fails
return respBody, err
}
// Return nil error if everything is successful
return respBody, err
}