-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
134 lines (122 loc) · 2.96 KB
/
api.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
package signalmgr
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"github.com/gofiber/fiber/v2"
)
var API_URL = "http://127.0.0.1:8080"
type errorResposne struct {
Error string `json:"error"`
}
type params map[string]string
func encodeParams(params params) string {
urlParams := url.Values{}
for key, val := range params {
urlParams.Add(key, val)
}
return urlParams.Encode()
}
// Sends a GET request to API_URL + path.
//
// Returns the response as raw bytes.
func getRaw(path string) (raw []byte, err error) {
return completeRequest(fiber.Get(strings.TrimSuffix(API_URL, "/") + path))
}
// Sends a GET request to API_URL + path.
//
// JSON parses the response into resp of provided type.
func get[T any](path string) (resp T, err error) {
raw, err := completeRequest(fiber.Get(strings.TrimSuffix(API_URL, "/") + path))
if err != nil {
return
}
if len(raw) == 0 {
return
}
err = json.Unmarshal(raw, &resp)
return
}
// Sends a POST request to API_URL + path, parsing data into JSON as the body.
//
// JSON parses the response into resp of provided type.
func post[T any](path string, data any) (resp T, err error) {
body, err := json.Marshal(data)
if err != nil {
return
}
req := fiber.Post(strings.TrimSuffix(API_URL, "/") + path)
req.Body(body)
req.ContentType("application/json")
raw, err := completeRequest(req)
if err != nil {
return
}
if len(raw) == 0 {
return
}
err = json.Unmarshal(raw, &resp)
return
}
// Sends a PUT request to API_URL + path, parsing data into JSON as the body.
//
// JSON parses the response into resp of provided type.
func put[T any](path string, data any) (resp T, err error) {
body, err := json.Marshal(data)
if err != nil {
return
}
req := fiber.Put(strings.TrimSuffix(API_URL, "/") + path)
req.Body(body)
req.ContentType("application/json")
raw, err := completeRequest(req)
if err != nil {
return
}
if len(raw) == 0 {
return
}
err = json.Unmarshal(raw, &resp)
return
}
// Sends a DELETE request to API_URL + path, parsing data into JSON as the body.
//
// JSON parses the response into resp of provided type.
func delete[T any](path string, data any) (resp T, err error) {
body, err := json.Marshal(data)
if err != nil {
return
}
req := fiber.Delete(strings.TrimSuffix(API_URL, "/") + path)
req.Body(body)
req.ContentType("application/json")
raw, err := completeRequest(req)
if err != nil {
return
}
if len(raw) == 0 {
return
}
err = json.Unmarshal(raw, &resp)
return
}
func completeRequest(req *fiber.Agent) (resp []byte, err error) {
status, resp, errs := req.Bytes()
if len(errs) > 0 {
err = fmt.Errorf("%d errors on post request: %+v", len(errs), errs)
return
}
var errResp errorResposne
if json.Unmarshal(resp, &errResp); errResp.Error != "" {
errResp.Error = strings.ReplaceAll(errResp.Error, "\n", "")
err = errors.New(errResp.Error)
return
}
if status < 200 || status > 299 {
err = fmt.Errorf("status %d and response received: %s", status, resp)
return
}
return
}