Files
request/v2/json.go
Matheus Sampaio Queiroga e19f99af2d
All checks were successful
Golang test / go-test (push) Successful in 16s
Update gitea structs and add more functions
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2025-06-27 22:08:26 -03:00

43 lines
1015 B
Go

package request
import (
"encoding/json"
"net/http"
"net/url"
)
// Make request and process Body in target
func MakeDoJSON(Url *url.URL, v any, Option *Options) (*http.Response, error) {
res, err := MakeRequestWithStatus(Url, Option)
if err != nil {
return res, err
}
defer res.Body.Close()
return res, json.NewDecoder(res.Body).Decode(v)
}
// Make request and return struct body
func MakeJSON[T any](Url *url.URL, Option *Options) (v T, res *http.Response, err error) {
res, err = MakeDoJSON(Url, &v, Option)
return
}
// Make request and process Body in target
func DoJSON(Url string, v any, Option *Options) (*http.Response, error) {
requestUrl, err := url.Parse(Url)
if err != nil {
return nil, err
}
return MakeDoJSON(requestUrl, v, Option)
}
// Make request and return struct body
func JSON[T any](Url string, Option *Options) (v T, res *http.Response, err error) {
requestUrl, err2 := url.Parse(Url)
if err2 != nil {
err = err2
return
}
return MakeJSON[T](requestUrl, Option)
}