Skip to content

Commit 2ecd744

Browse files
committed
init
1 parent 04d3d7c commit 2ecd744

11 files changed

+565
-0
lines changed

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, build with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
/.idea
15+
.DS_Store

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## xhttp
2+
3+
http request library for Go

client.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package xhttp
2+
3+
import (
4+
"crypto/tls"
5+
"net"
6+
"net/http"
7+
"time"
8+
)
9+
10+
type Client struct {
11+
HttpClient *http.Client
12+
bodySize int // body size limit(MB), default is 10MB
13+
}
14+
15+
func defaultClient() *Client {
16+
return &Client{
17+
HttpClient: &http.Client{
18+
Timeout: 60 * time.Second,
19+
Transport: &http.Transport{
20+
Proxy: http.ProxyFromEnvironment,
21+
DialContext: defaultTransportDialContext(&net.Dialer{
22+
Timeout: 30 * time.Second,
23+
KeepAlive: 30 * time.Second,
24+
DualStack: true,
25+
}),
26+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
27+
MaxIdleConns: 100,
28+
IdleConnTimeout: 90 * time.Second,
29+
TLSHandshakeTimeout: 10 * time.Second,
30+
ExpectContinueTimeout: 1 * time.Second,
31+
DisableKeepAlives: true,
32+
ForceAttemptHTTP2: true,
33+
},
34+
},
35+
bodySize: 10, // default is 10MB
36+
}
37+
}
38+
39+
// NewClient , default tls.Config{InsecureSkipVerify: true}
40+
func NewClient() (client *Client) {
41+
return defaultClient()
42+
}
43+
44+
func (c *Client) SetTransport(transport *http.Transport) (client *Client) {
45+
c.HttpClient.Transport = transport
46+
return c
47+
}
48+
49+
func (c *Client) SetTLSConfig(tlsCfg *tls.Config) (client *Client) {
50+
c.HttpClient.Transport.(*http.Transport).TLSClientConfig = tlsCfg
51+
return c
52+
}
53+
54+
func (c *Client) SetTimeout(timeout time.Duration) (client *Client) {
55+
c.HttpClient.Timeout = timeout
56+
return c
57+
}
58+
59+
// set body size (MB), default is 10MB
60+
func (c *Client) SetBodySize(sizeMB int) (client *Client) {
61+
c.bodySize = sizeMB
62+
return c
63+
}
64+
65+
// typeStr is request type and response type
66+
// default is TypeJSON
67+
// first param is request type
68+
// second param is response data type
69+
func (c *Client) Req(typeStr ...string) *Request {
70+
var (
71+
reqTp = TypeJSON // default
72+
resTp = ResTypeJSON // default
73+
tLen = len(typeStr)
74+
)
75+
switch {
76+
case tLen == 1:
77+
tpp := typeStr[0]
78+
if _, ok := _ReqContentTypeMap[tpp]; ok {
79+
reqTp = tpp
80+
}
81+
case tLen > 1:
82+
// first param is request type
83+
tpp := typeStr[0]
84+
if _, ok := _ReqContentTypeMap[tpp]; ok {
85+
reqTp = tpp
86+
}
87+
// second param is response data type
88+
stpp := typeStr[1]
89+
if _, ok := _ResTypeMap[stpp]; ok {
90+
resTp = stpp
91+
}
92+
}
93+
if c == nil {
94+
c = defaultClient()
95+
}
96+
r := &Request{
97+
client: c,
98+
Header: make(http.Header),
99+
requestType: reqTp,
100+
responseType: resTp,
101+
}
102+
r.Header.Set("Content-Type", _ReqContentTypeMap[reqTp])
103+
return r
104+
}

client_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package xhttp
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"github.com/go-pay/bm"
9+
"github.com/go-pay/xlog"
10+
)
11+
12+
type HttpGet struct {
13+
Code int `json:"code"`
14+
Message string `json:"message"`
15+
Data any `json:"data,omitempty"`
16+
}
17+
18+
var ctx = context.Background()
19+
20+
func TestHttpGet(t *testing.T) {
21+
xlog.Level = xlog.DebugLevel
22+
var client *Client
23+
// test
24+
_, bs, err := client.Req().Get("http://www.baidu.com").EndBytes(ctx)
25+
if err != nil {
26+
xlog.Error(err)
27+
return
28+
}
29+
xlog.Debug(string(bs))
30+
31+
//rsp := new(HttpGet)
32+
//_, err = client.Type(TypeJSON).Get("http://api.igoogle.ink/app/v1/ping").EndStruct(ctx, rsp)
33+
//if err != nil {
34+
// xlog.Error(err)
35+
// return
36+
//}
37+
//xlog.Debug(rsp)
38+
}
39+
40+
func TestHttpUploadFile(t *testing.T) {
41+
xlog.Level = xlog.DebugLevel
42+
fileContent, err := os.ReadFile("logo.png")
43+
if err != nil {
44+
xlog.Error(err)
45+
return
46+
}
47+
//xlog.Debug("fileByte:", string(fileContent))
48+
49+
bmm := make(bm.BodyMap)
50+
bmm.SetBodyMap("meta", func(bm bm.BodyMap) {
51+
bm.Set("filename", "123.jpg").
52+
Set("sha256", "ad4465asd4fgw5q")
53+
}).SetFormFile("image", &bm.File{Name: "logo.png", Content: fileContent})
54+
55+
client := NewClient()
56+
57+
rsp := new(HttpGet)
58+
_, err = client.Req(TypeMultipartFormData).
59+
Post("http://localhost:2233/admin/v1/oss/uploadImage").
60+
SendMultipartBodyMap(bmm).
61+
EndStruct(ctx, rsp)
62+
if err != nil {
63+
xlog.Error(err)
64+
return
65+
}
66+
xlog.Debugf("%+v", rsp)
67+
}

go.mod

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/go-pay/xhttp
2+
3+
go 1.20.0
4+
5+
require (
6+
github.com/go-pay/bm v0.0.0-20231104140628-922a8107c7dd
7+
github.com/go-pay/xlog v0.0.0-20231104134525-b6ce65b7e035
8+
)
9+
10+
require (
11+
go.uber.org/multierr v1.11.0 // indirect
12+
go.uber.org/zap v1.26.0 // indirect
13+
)

go.sum

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/go-pay/bm v0.0.0-20231104140628-922a8107c7dd h1:FxRmde5/CEIh5hy6zHfhqzKJGyhbI9JoNf6h/LlbiVM=
3+
github.com/go-pay/bm v0.0.0-20231104140628-922a8107c7dd/go.mod h1:puiU1FnDsC1m2h8ArJc4n1WZuzAMZEYex0Gp10PY3fI=
4+
github.com/go-pay/xlog v0.0.0-20231104134525-b6ce65b7e035 h1:jlc9l6rgHlUSYP/hN8JzH8mTGn2YzQD7KJ1tCW+PwR8=
5+
github.com/go-pay/xlog v0.0.0-20231104134525-b6ce65b7e035/go.mod h1:9PX534KUJqcKyxfEgNDms41EiwSm+IR0NxY01k31YHs=
6+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
8+
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
9+
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
10+
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
11+
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
12+
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
13+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

logo.png

31.5 KB
Loading

model.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package xhttp
2+
3+
import "encoding/json"
4+
5+
const (
6+
GET = "GET"
7+
POST = "POST"
8+
PUT = "PUT"
9+
DELETE = "DELETE"
10+
PATCH = "PATCH"
11+
12+
ResTypeJSON = "json"
13+
ResTypeXML = "xml"
14+
15+
TypeJSON = "json"
16+
TypeXML = "xml"
17+
TypeFormData = "form-data"
18+
TypeMultipartFormData = "multipart-form-data"
19+
)
20+
21+
var (
22+
_ReqContentTypeMap = map[string]string{
23+
TypeJSON: "application/json",
24+
TypeXML: "application/xml",
25+
TypeFormData: "application/x-www-form-urlencoded",
26+
TypeMultipartFormData: "multipart/form-data",
27+
}
28+
29+
_ResTypeMap = map[string]string{
30+
ResTypeJSON: "application/json",
31+
ResTypeXML: "application/xml",
32+
}
33+
)
34+
35+
func ConvertToString(v any) (str string) {
36+
if v == nil {
37+
return ""
38+
}
39+
var (
40+
bs []byte
41+
err error
42+
)
43+
if bs, err = json.Marshal(v); err != nil {
44+
return ""
45+
}
46+
str = string(bs)
47+
return
48+
}

0 commit comments

Comments
 (0)