-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
109 lines (98 loc) · 2.94 KB
/
httpclient.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/joy12825/gf.
// Package httpclient provides http client used for SDK.
package httpclient
import (
"context"
"encoding/json"
"net/http"
"github.com/joy12825/gf/encoding/gurl"
"github.com/joy12825/gf/errors/gcode"
"github.com/joy12825/gf/errors/gerror"
"github.com/joy12825/gf/net/gclient"
"github.com/joy12825/gf/net/ghttp"
"github.com/joy12825/gf/text/gregex"
"github.com/joy12825/gf/text/gstr"
"github.com/joy12825/gf/util/gconv"
"github.com/joy12825/gf/util/gmeta"
"github.com/joy12825/gf/util/gtag"
)
// Client is a http client for SDK.
type Client struct {
*gclient.Client
config Config
}
// New creates and returns a http client for SDK.
func New(config Config) *Client {
return &Client{
Client: config.Client,
config: config,
}
}
func (c *Client) handleResponse(ctx context.Context, res *gclient.Response, out interface{}) error {
if c.config.RawDump {
c.config.Logger.Debugf(ctx, "raw request&response:\n%s", res.Raw())
}
var (
responseBytes = res.ReadAll()
result = ghttp.DefaultHandlerResponse{
Data: out,
}
)
if !json.Valid(responseBytes) {
return gerror.Newf(`invalid response content: %s`, responseBytes)
}
if err := json.Unmarshal(responseBytes, &result); err != nil {
return gerror.Wrapf(err, `json.Unmarshal failed with content:%s`, responseBytes)
}
if result.Code != gcode.CodeOK.Code() {
return gerror.NewCode(
gcode.New(result.Code, result.Message, nil),
result.Message,
)
}
return nil
}
// Request sends request to service by struct object `req`, and receives response to struct object `res`.
func (c *Client) Request(ctx context.Context, req, res interface{}) error {
var (
method = gmeta.Get(req, gtag.Method).String()
path = gmeta.Get(req, gtag.Path).String()
)
switch gstr.ToUpper(method) {
case http.MethodGet:
return c.Get(ctx, path, req, res)
default:
result, err := c.ContentJson().DoRequest(ctx, method, c.handlePath(path, req), req)
if err != nil {
return err
}
return c.handleResponse(ctx, result, res)
}
}
// Get sends a request using GET method.
func (c *Client) Get(ctx context.Context, path string, in, out interface{}) error {
if urlParams := ghttp.BuildParams(in); urlParams != "" {
path += "?" + ghttp.BuildParams(in)
}
res, err := c.ContentJson().Get(ctx, c.handlePath(path, in))
if err != nil {
return gerror.Wrap(err, `http request failed`)
}
return c.handleResponse(ctx, res, out)
}
func (c *Client) handlePath(path string, in interface{}) string {
if gstr.Contains(path, "{") {
data := gconv.MapStrStr(in)
path, _ = gregex.ReplaceStringFuncMatch(`\{(\w+)\}`, path, func(match []string) string {
if v, ok := data[match[1]]; ok {
return gurl.Encode(v)
}
return match[1]
})
}
return path
}