liuhaijun e94826ce29 add server
Change-Id: I0760f17f6a01c0121b59fcbfafc666032dbc30af
2024-09-19 09:44:15 +00:00

76 lines
1.4 KiB
Go

package utils
import (
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
type HttpRequest struct {
http.Client
Response *http.Response
Error error
}
// Request make a request
func (hr *HttpRequest) Request(method string, url string, body io.Reader, args ...any) *HttpRequest {
req, err := http.NewRequest(method, url, body)
if err != nil {
hr.Error = err
}
if args != nil {
if options, ok := args[0].(map[string]string); ok {
for k, v := range options {
req.Header.Set(k, v)
}
}
}
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}}
hr.Response, hr.Error = client.Do(req)
return hr
}
// ParseJson Parse the return value into json format
func (hr *HttpRequest) ParseJson(payload any) error {
bytes, err := hr.ParseBytes()
if err != nil {
return err
}
return json.Unmarshal(bytes, &payload)
}
// ParseBytes Parse the return value into []byte format
func (hr *HttpRequest) ParseBytes() ([]byte, error) {
if hr.Error != nil {
return nil, hr.Error
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err.Error())
}
}(hr.Response.Body)
return ioutil.ReadAll(hr.Response.Body)
}
// Raw Return the raw response data as a string
func (hr *HttpRequest) Raw() (string, error) {
str, err := hr.ParseBytes()
if err != nil {
return "", err
}
return string(str), nil
}