From 3bc3f47e7d44054bd6d0dc69d3925be254bdbb40 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sat, 24 Jun 2023 15:50:15 +0800 Subject: [PATCH] up: add more option set func and update readme --- README.md | 2 ++ README.zh-CN.md | 2 ++ builder.go | 13 ++++++++----- client.go | 20 +++++++++++++------- option.go | 29 ++++++++++++++++++++++------- 5 files changed, 47 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b2b91e2..38ee21b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/README.zh-CN.md b/README.zh-CN.md index 2ce7580..680d648 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -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 ## 功能说明 diff --git a/builder.go b/builder.go index e4a4ee7..7bdb9ae 100644 --- a/builder.go +++ b/builder.go @@ -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 { @@ -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) } @@ -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 ------------ diff --git a/client.go b/client.go index a8eef33..f5c370c 100644 --- a/client.go +++ b/client.go @@ -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" @@ -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 ------------ // @@ -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 @@ -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() @@ -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 ------------ diff --git a/option.go b/option.go index 8996712..49902f3 100644 --- a/option.go +++ b/option.go @@ -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) + } +}