-
Notifications
You must be signed in to change notification settings - Fork 0
/
webreq.go
149 lines (128 loc) · 3.22 KB
/
webreq.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
package webreq
import (
"bytes"
"context"
"io"
"net/http"
"time"
)
const (
MethodGet = "GET"
MethodPost = "POST"
)
type HeadersMap map[string]string
type Headers struct {
ListHeaders HeadersMap
}
// NewHeaders creates a new Headers instance, initializing with provided headers or an empty map
func NewHeaders(headers map[string]string) *Headers {
if headers != nil {
return &Headers{
ListHeaders: headers,
}
}
return &Headers{
ListHeaders: make(HeadersMap),
}
}
// Add adds a new header key-value pair to the Headers
func (header *Headers) Add(key string, value string) {
if key != "" && value != "" {
header.ListHeaders[key] = value
}
}
type Request struct {
URL string
TimeoutDuration time.Duration
Headers HeadersMap
Method string
Data []byte
StatusCode int
ErrorMessage string
}
// NewRequest creates a new Request with the specified method
func NewRequest(method string) *Request {
return &Request{
TimeoutDuration: 10 * time.Second,
Method: method,
}
}
// SetURL sets the URL of the request
func (request *Request) SetURL(urlValue string) *Request {
if urlValue == "" {
request.ErrorMessage = "url is empty"
return nil
}
request.URL = urlValue
return request
}
// SetTimeout sets the request timeout duration
func (request *Request) SetTimeout(timeout int) *Request {
if timeout > 0 {
request.TimeoutDuration = time.Duration(timeout) * time.Second
}
return request
}
// SetHeaders sets the headers of the request
func (request *Request) SetHeaders(headers HeadersMap) *Request {
if len(headers) > 0 {
request.Headers = headers
} else {
request.ErrorMessage = "headers are empty"
}
return request
}
// SetData sets the data to be sent with the request
func (request *Request) SetData(bodyValue []byte) *Request {
if len(bodyValue) > 0 {
request.Data = bodyValue
} else {
request.ErrorMessage = "body is empty"
}
return request
}
// SetMethod sets the method of the request
func (request *Request) SetMethod(requestMethod string) *Request {
if requestMethod != "" {
request.Method = requestMethod
} else {
request.ErrorMessage = "request method is empty"
}
return request
}
// SetStatusCode sets the status code of the response
func (request *Request) SetStatusCode(statusCodeValue int) *Request {
if statusCodeValue > 0 {
request.StatusCode = statusCodeValue
} else {
request.ErrorMessage = "status code is empty"
}
return request
}
func (request *Request) Check() error {
return nil
}
// Execute sends the request and returns the response body and error if any
func (request *Request) Execute() ([]byte, error) {
client := &http.Client{}
ctx, cancel := context.WithTimeout(context.Background(), request.TimeoutDuration)
defer cancel()
webRequest, err := http.NewRequestWithContext(ctx, request.Method, request.URL, bytes.NewReader(request.Data))
if err != nil {
return nil, err
}
for key, value := range request.Headers {
webRequest.Header.Add(key, value)
}
response, err := client.Do(webRequest)
if err != nil {
return nil, err
}
defer response.Body.Close()
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
request.StatusCode = response.StatusCode
return responseBody, nil
}