-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.go
48 lines (40 loc) · 1.22 KB
/
parse.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
package httpc
import (
"encoding/xml"
"io"
"net/http"
jsoniter "github.com/json-iterator/go"
"gopkg.in/yaml.v3"
)
// HTTPError represents an error that occurred while handling a request
// Identical to struct used in labstack/echo
type HTTPError struct {
Code int `json:"code,omitempty"`
Message any `json:"message,omitempty"`
Internal error // Stores the error returned by an external dependency
}
// Copy copies the response body into any io.Writer
func Copy(w io.Writer) func(resp *http.Response) error {
return func(resp *http.Response) error {
_, err := io.Copy(w, resp.Body)
return err
}
}
// ParseJSON parses the response body as JSON into a struct
func ParseJSON(v any) func(resp *http.Response) error {
return func(resp *http.Response) error {
return jsoniter.NewDecoder(resp.Body).Decode(v)
}
}
// ParseYAML parses the response body as YAML into a struct
func ParseYAML(v any) func(resp *http.Response) error {
return func(resp *http.Response) error {
return yaml.NewDecoder(resp.Body).Decode(v)
}
}
// ParseXML parses the response body as XML into a struct
func ParseXML(v any) func(resp *http.Response) error {
return func(resp *http.Response) error {
return xml.NewDecoder(resp.Body).Decode(v)
}
}