forked from deploymenttheory/go-api-sdk-jamfpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_methods.go
70 lines (58 loc) · 2.62 KB
/
http_methods.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
// http_methods.go
package http_client
import "net/http"
// Get sends a GET request to the specified endpoint and unmarshals the response into 'out'.
// The caller is responsible for closing the response body.
func (c *Client) Get(endpoint string, out interface{}) (*http.Response, error) {
c.logger.Info("Sending GET request", "endpoint", endpoint)
resp, err := c.DoRequest(http.MethodGet, endpoint, nil, out)
if err != nil {
c.logger.Error("GET request failed", "endpoint", endpoint, "error", err)
return nil, err
}
return resp, nil
}
// Post sends a POST request to the specified endpoint with the provided body and unmarshals the response into 'out'.
// The caller is responsible for closing the response body.
func (c *Client) Post(endpoint string, body, out interface{}) (*http.Response, error) {
c.logger.Info("Sending POST request", "endpoint", endpoint, "body", body)
resp, err := c.DoRequest(http.MethodPost, endpoint, body, out)
if err != nil {
c.logger.Error("POST request failed", "endpoint", endpoint, "error", err)
return nil, err
}
return resp, nil
}
// Put sends a PUT request to the specified endpoint with the provided body and unmarshals the response into 'out'.
// The caller is responsible for closing the response body.
func (c *Client) Put(endpoint string, body, out interface{}) (*http.Response, error) {
c.logger.Debug("Sending PUT request", "endpoint", endpoint, "body", body)
resp, err := c.DoRequest(http.MethodPut, endpoint, body, out)
if err != nil {
c.logger.Error("PUT request failed", "endpoint", endpoint, "error", err)
return nil, err
}
return resp, nil
}
// Delete sends a DELETE request to the specified endpoint and unmarshals the response into 'out'.
// The caller is responsible for closing the response body.
func (c *Client) Delete(endpoint string, out interface{}) (*http.Response, error) {
c.logger.Debug("Sending DELETE request", "endpoint", endpoint)
resp, err := c.DoRequest(http.MethodDelete, endpoint, nil, out)
if err != nil {
c.logger.Error("DELETE request failed", "endpoint", endpoint, "error", err)
return nil, err
}
return resp, nil
}
// Patch sends a PATCH request to the specified endpoint with the provided body and unmarshals the response into 'out'.
// The caller is responsible for closing the response body.
func (c *Client) Patch(endpoint string, body, out interface{}) (*http.Response, error) {
c.logger.Debug("Sending PATCH request", "endpoint", endpoint, "body", body)
resp, err := c.DoRequest(http.MethodPatch, endpoint, body, out)
if err != nil {
c.logger.Error("PATCH request failed", "endpoint", endpoint, "error", err)
return nil, err
}
return resp, nil
}