Skip to content

Commit

Permalink
up: add more option set func and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 24, 2023
1 parent e499a39 commit 3bc3f47
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
[![Unit-Tests](https://github.com/gookit/greq/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/greq/actions)
[![Coverage Status](https://coveralls.io/repos/github/gookit/greq/badge.svg?branch=main)](https://coveralls.io/github/gookit/greq?branch=main)

> [涓枃璇存槑](README.zh-CN.md) | [English](README.md)
**greq** A simple http client request builder and sender

## Features
Expand Down
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
[![Unit-Tests](https://github.com/gookit/greq/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/greq/actions)
[![Coverage Status](https://coveralls.io/repos/github/gookit/greq/badge.svg?branch=main)](https://coveralls.io/github/gookit/greq?branch=main)

> [涓枃璇存槑](README.zh-CN.md) | [English](README.md)
**greq** A simple http client request builder and sender

## 鍔熻兘璇存槑
Expand Down
13 changes: 8 additions & 5 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func NewBuilder(fns ...OptionFn) *Builder {
}

// BuilderWithClient create a new builder with client
func BuilderWithClient(c *Client) *Builder {
return NewBuilder().WithClient(c)
func BuilderWithClient(c *Client, optFns ...OptionFn) *Builder {
return NewBuilder(optFns...).WithClient(c)
}

func newBuilder(c *Client, method, pathURL string) *Builder {
Expand Down Expand Up @@ -104,9 +104,7 @@ func (b *Builder) AddQuery(key string, value any) *Builder {
// The value will be encoded as url Query parameters on send requests (see Send()).
func (b *Builder) QueryParams(ps any) *Builder {
if ps != nil {
queryValues := httpreq.ToQueryValues(ps)

for key, values := range queryValues {
for key, values := range httpreq.ToQueryValues(ps) {
for _, value := range values {
b.Query.Add(key, value)
}
Expand All @@ -122,6 +120,11 @@ func (b *Builder) QueryValues(values gourl.Values) *Builder {
return b.QueryParams(values)
}

// WithQuerySMap appends map[string]string to the Query string.
func (b *Builder) WithQuerySMap(smp map[string]string) *Builder {
return b.QueryParams(smp)
}

//
//
// ----------- HeaderM ------------
Expand Down
20 changes: 13 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
gourl "net/url"
"strings"

"github.com/gookit/goutil/basefn"
"github.com/gookit/goutil/netutil/httpctype"
"github.com/gookit/goutil/netutil/httpheader"
"github.com/gookit/goutil/netutil/httpreq"
Expand Down Expand Up @@ -218,6 +217,11 @@ func (h *Client) DefaultMethod(method string) *Client {
return h
}

// Builder create a new builder with current client.
func (h *Client) Builder(optFns ...OptionFn) *Builder {
return BuilderWithClient(h, optFns...)
}

//
// ------------ REST requests ------------
//
Expand All @@ -229,9 +233,8 @@ func (h *Client) Head(pathURL string) *Builder {

// HeadDo sets the method to HEAD and request the pathURL,
// then send request and return response.
func (h *Client) HeadDo(pathURL string, withOpt ...*Options) (*Response, error) {
opt := basefn.FirstOr(withOpt, &Options{})
return h.SendWithOpt(pathURL, opt.WithMethod(http.MethodHead))
func (h *Client) HeadDo(pathURL string, optFns ...OptionFn) (*Response, error) {
return h.SendWithOpt(pathURL, NewOpt2(optFns, http.MethodHead))
}

// Get sets the method to GET and sets the given pathURL
Expand Down Expand Up @@ -291,6 +294,11 @@ func (h *Client) DeleteDo(pathURL string, optFns ...OptionFn) (*Response, error)

// ----------- URL, Query params ------------

// WithContentType with custom Content-Type header
func (h *Client) WithContentType(value string) *Builder {
return BuilderWithClient(h).WithContentType(value)
}

// JSONType with json Content-Type header
func (h *Client) JSONType() *Builder {
return BuilderWithClient(h).JSONType()
Expand Down Expand Up @@ -350,9 +358,7 @@ func (h *Client) BodyReader(r io.Reader) *Builder {

// BodyProvider with custom body provider
func (h *Client) BodyProvider(bp BodyProvider) *Builder {
b := BuilderWithClient(h)
b.Provider = bp
return b
return BuilderWithClient(h).BodyProvider(bp)
}

// ----------- Do send request ------------
Expand Down
29 changes: 22 additions & 7 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,29 @@ func ensureOpt(opt *Options) *Options {
return opt
}

//
// ----------- built in OptionFn ------------
//

// WithMethod set method
func (opt *Options) WithMethod(method string) *Options {
if method != "" {
opt.Method = method
func WithMethod(method string) OptionFn {
return func(opt *Options) {
if method != "" {
opt.Method = method
}
}
return opt
}

//
// ----------- Build Request ------------
//
// WithContentType set content-type
func WithContentType(contentType string) OptionFn {
return func(opt *Options) {
opt.ContentType = contentType
}
}

// WithUserAgent set user-agent header
func WithUserAgent(userAgent string) OptionFn {
return func(opt *Options) {
opt.Header.Set("User-Agent", userAgent)
}
}

0 comments on commit 3bc3f47

Please sign in to comment.