-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
92 lines (67 loc) · 3.06 KB
/
api.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
package requests
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
)
// ------------------------------------------------- --------------------------------------------------------------------
// GetYaml 响应内容是YAML格式的
func GetYaml[Response any](ctx context.Context, targetUrl string, options ...*Options[any, Response]) (Response, error) {
if len(options) == 0 {
options = append(options, NewOptions[any, Response](targetUrl, YamlResponseHandler[Response]()))
}
options[0] = options[0].WithTargetURL(targetUrl).WithYamlResponseHandler()
return SendRequest[any, Response](ctx, options[0])
}
// ------------------------------------------------- --------------------------------------------------------------------
// GetJson 响应内容是JSON格式的
func GetJson[Response any](ctx context.Context, targetUrl string, options ...*Options[any, Response]) (Response, error) {
if len(options) == 0 {
options = append(options, NewOptions[any, Response](targetUrl, JsonResponseHandler[Response]()))
}
options[0] = options[0].WithTargetURL(targetUrl).
WithJsonResponseHandler().
AppendRequestSetting(func(client *http.Client, request *http.Request) error {
request.Header.Set("Content-Type", "application/json")
return nil
})
return SendRequest[any, Response](ctx, options[0])
}
func PostJson[Request any, Response any](ctx context.Context, targetUrl string, request Request, options ...*Options[Request, Response]) (Response, error) {
if len(options) == 0 {
options = append(options, NewOptions[Request, Response](targetUrl, JsonResponseHandler[Response]()))
}
marshal, err := json.Marshal(request)
if err != nil {
var zero Response
return zero, fmt.Errorf("PostJson json marshal request error: %s, typer = %s", err.Error(), reflect.TypeOf(request).String())
}
options[0] = options[0].WithTargetURL(targetUrl).
WithJsonResponseHandler().
AppendRequestSetting(func(client *http.Client, request *http.Request) error {
request.Header.Set("Content-Type", "application/json")
return nil
}).
WithBody(marshal)
return SendRequest[Request, Response](ctx, options[0])
}
// ------------------------------------------------- --------------------------------------------------------------------
func GetBytes(ctx context.Context, targetUrl string, options ...*Options[any, []byte]) ([]byte, error) {
if len(options) == 0 {
options = append(options, NewOptions[any, []byte](targetUrl, BytesResponseHandler()))
}
options[0] = options[0].WithTargetURL(targetUrl).
WithResponseHandler(BytesResponseHandler())
return SendRequest[any, []byte](ctx, options[0])
}
// ------------------------------------------------- --------------------------------------------------------------------
func GetString(ctx context.Context, targetUrl string, options ...*Options[any, []byte]) (string, error) {
responseBytes, err := GetBytes(ctx, targetUrl, options...)
if err != nil {
return "", err
}
return string(responseBytes), nil
}
// ------------------------------------------------- --------------------------------------------------------------------