-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
155 lines (128 loc) · 2.65 KB
/
request.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
package httplib
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/MC2BP/MicroS-Go/lib/errorlib"
"github.com/MC2BP/MicroS-Go/lib/loglib"
)
const (
ContentJS = "application/javascript"
ContentOGG = "application/ogg"
ContentPDF = "application/pdf"
ContentXHTML = "application/xhtml+xml"
ContentJSON = "application/json"
ContentXML = "application/xml"
ContentZIP = "application/zip"
)
type Request struct {
Method string
URL string
ContentType string
Headers []Header
Parameter []Parameter
Body []byte
Status int
}
type Header struct {
Key string
Value string
}
type Parameter struct {
Key string
Value interface{}
}
func (r *Request) Send(ctx context.Context) (response []byte, err error) {
req, err := r.getHttpRequest()
if err != nil {
return
}
req.WithContext(ctx)
// Send request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
r.Status = resp.StatusCode
return
}
func (r *Request) SendWithoutContext() (response []byte, err error) {
req, err := r.getHttpRequest()
if err != nil {
return
}
// Send request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
response, err = io.ReadAll(resp.Body)
if err != nil {
return
}
r.Status = resp.StatusCode
return
}
func (r *Request) getHttpRequest() (req *http.Request, err error) {
req, err = http.NewRequest(r.Method, r.URL, bytes.NewBuffer(r.Body))
if err != nil {
return
}
// build url
q := req.URL.Query()
for _, parameter := range r.Parameter {
q.Add(parameter.Key, fmt.Sprint(parameter.Value))
}
req.URL.RawQuery = q.Encode()
for _, header := range r.Headers {
req.Header.Add(header.Key, header.Value)
}
return
}
func ReadBody(r *http.Request) (body []byte, err error) {
body, err = io.ReadAll(r.Body)
return
}
func ParseBody(r *http.Request, body *interface{}) error {
rawBody, err := ReadBody(r)
if err != nil {
return err
}
response := Response{
Data: body,
}
err = json.Unmarshal(rawBody, &response)
if err != nil {
return err
}
return nil
}
func SendError(w http.ResponseWriter, err error) {
loglib.Warning(err)
response := Response{
Success: false,
ErrorCode: errorlib.GetErrorCode(err),
Message: errorlib.GetError(err).Error(),
}
responseBody, err := json.Marshal(response)
if err != nil {
loglib.Error(err)
}
fmt.Fprintf(w, string(responseBody))
}
func Send(w http.ResponseWriter, body interface{}) {
response := Response{
Success: true,
Data: body,
}
responseBody, err := json.Marshal(response)
if err != nil {
loglib.Error(err)
}
fmt.Fprintf(w, string(responseBody))
}