Skip to content

Commit

Permalink
✨ feat: add more feature for make request and update some deps
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 3, 2023
1 parent bb5bc3f commit 551d19c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: '## Change Log'
style: gh-release
# group names
names: [Refactor, Fixed, Feature, Update, Other]
repo_url: https://github.com/gookit/goutil
#repo_url: https://github.com/gookit/goutil

filters:
# message length should >= 12
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ jobs:
tag_name: ${{ env.RELEASE_TAG }}
body_path: changelog.md
token: ${{ secrets.GITHUB_TOKEN }}
# files: macos-chlog.exe
15 changes: 15 additions & 0 deletions ext.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package greq

import (
"context"
"time"
)

func ensureOpt(opt *ReqOption) *ReqOption {
if opt == nil {
opt = &ReqOption{}
}
if opt.Context == nil {
opt.Context = context.Background()
}

if opt.Timeout > 0 {
opt.Context, opt.TCancelFunc = context.WithTimeout(
opt.Context,
time.Duration(opt.Timeout)*time.Millisecond,
)
}
return opt
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module github.com/gookit/greq

go 1.19

require github.com/gookit/goutil v0.6.7
require github.com/gookit/goutil v0.6.8

require (
github.com/gookit/color v1.5.2 // indirect
github.com/gookit/color v1.5.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
Expand Down
20 changes: 4 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI=
github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg=
github.com/gookit/goutil v0.6.7 h1:Sz0y5eslPZ8M2lUhRaSMnwEeX5nb9z7SoCsw0ZbMwe4=
github.com/gookit/goutil v0.6.7/go.mod h1:ti+JpLBGSN83ga6SSZa6uozhntToWSzOPm2z1hvpQSc=
github.com/gookit/color v1.5.3 h1:twfIhZs4QLCtimkP7MOxlF3A0U/5cDPseRT9M/+2SCE=
github.com/gookit/color v1.5.3/go.mod h1:NUzwzeehUfl7GIb36pqId+UGmRfQcU/WiiyTTeNjHtE=
github.com/gookit/goutil v0.6.8 h1:B2XXSCGav5TXWtKRT9i/s/owOLXXB7sY6UsfqeSLroE=
github.com/gookit/goutil v0.6.8/go.mod h1:u+Isykc6RQcZ4GQzulsaGm+Famd97U5Tzp3aQyo+jyA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88 changes: 33 additions & 55 deletions hireq.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,35 @@ func (h *HiReq) SendRaw(raw string) (*Response, error) {
return h.SendRequest(req)
}

// DoWithCtx request with context, then return response
func (h *HiReq) DoWithCtx(ctx context.Context, pathURLAndMethod ...string) (*Response, error) {
return h.SendWithCtx(ctx, pathURLAndMethod...)
}

// ReqOption type
type ReqOption = httpreq.ReqOption

// SendWithOpt request with context, then return response
// SendWithOpt send request with option, then return response
func (h *HiReq) SendWithOpt(pathURL string, opt *ReqOption) (*Response, error) {
// ensure option
opt = ensureOpt(opt)
return h.SendWithCtx(opt.Context, pathURL, opt.Method)

// build request
req, err := h.NewRequestWithCtx(opt.Context, pathURL, opt.Method)
if err != nil {
return nil, err
}

// set headers
if len(opt.HeaderMap) > 0 {
httpreq.AddHeaderMap(req, opt.HeaderMap)
}
if len(opt.ContentType) > 0 {
req.Header.Set("Content-Type", opt.ContentType)
}

// do send
return h.SendRequest(req)
}

// DoWithCtx request with context, then return response
func (h *HiReq) DoWithCtx(ctx context.Context, pathURLAndMethod ...string) (*Response, error) {
return h.SendWithCtx(ctx, pathURLAndMethod...)
}

// SendWithCtx request with context, then return response
Expand Down Expand Up @@ -602,11 +619,12 @@ func (h *HiReq) NewRequest(pathURLAndMethod ...string) (*http.Request, error) {

// NewRequestWithCtx build new request with context
func (h *HiReq) NewRequestWithCtx(ctx context.Context, pathURLAndMethod ...string) (*http.Request, error) {
method := h.method
pathURL := h.pathURL
if ln := len(pathURLAndMethod); ln > 0 {
pathURL = pathURLAndMethod[0]
if ln > 1 && len(pathURLAndMethod[1]) > 0 {
h.method = pathURLAndMethod[1]
method = pathURLAndMethod[1]
}
}

Expand All @@ -623,9 +641,12 @@ func (h *HiReq) NewRequestWithCtx(ctx context.Context, pathURLAndMethod ...strin
if err != nil {
return nil, err
}
if reqURL.Scheme == "" {
reqURL.Scheme = "https"
}

// append query params
if err = appendQueryParams(reqURL, h.queryParams); err != nil {
if err = httpreq.AppendQueryToURL(reqURL, h.queryParams); err != nil {
return nil, err
}

Expand All @@ -641,7 +662,7 @@ func (h *HiReq) NewRequestWithCtx(ctx context.Context, pathURLAndMethod ...strin
}
}

req, err := http.NewRequestWithContext(ctx, h.method, reqURL.String(), body)
req, err := http.NewRequestWithContext(ctx, method, reqURL.String(), body)
if err != nil {
return nil, err
}
Expand All @@ -656,52 +677,9 @@ func (h *HiReq) NewRequestWithCtx(ctx context.Context, pathURLAndMethod ...strin

// String request to string.
func (h *HiReq) String() string {
return ToString(h.Build())
}

func appendQueryParams(reqURL *url.URL, uv url.Values) error {
urlValues, err := url.ParseQuery(reqURL.RawQuery)
if err != nil {
return err
}

for key, values := range uv {
for _, value := range values {
urlValues.Add(key, value)
}
}

// url.Values format to a sorted "url encoded" string.
// e.g. "key=val&foo=bar"
reqURL.RawQuery = urlValues.Encode()
return nil
}

// ToString convert http Request to string
func ToString(r *http.Request, err error) string {
r, err := h.Build()
if err != nil {
return ""
}

buf := &bytes.Buffer{}
buf.WriteString(r.Method)
buf.WriteByte(' ')
buf.WriteString(r.URL.String())
buf.WriteByte(' ')
buf.WriteString(r.Proto)
buf.WriteByte('\n')

for key, values := range r.Header {
buf.WriteString(key)
buf.WriteString(": ")
buf.WriteString(strings.Join(values, ";"))
buf.WriteByte('\n')
}

if r.Body != nil {
buf.WriteByte('\n')
_, _ = buf.ReadFrom(r.Body)
}

return buf.String()
return httpreq.RequestToString(r)
}
5 changes: 2 additions & 3 deletions resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (r *Response) ContentType() string {
//
// Usage:
//
// resp, err := greq.Post("some.host/path")
// ok := resp.IsContentType("application/xml")
// resp, err := greq.Post("some.host/path")
// ok := resp.IsContentType("application/xml")
func (r *Response) IsContentType(prefix string) bool {
val := r.Header.Get(httpctype.Key)
return val != "" && strings.HasPrefix(val, prefix)
Expand Down Expand Up @@ -85,7 +85,6 @@ func (r *Response) BodyBuffer() *bytes.Buffer {
if err != nil {
panic(err)
}

return buf
}

Expand Down

0 comments on commit 551d19c

Please sign in to comment.