Skip to content

Commit

Permalink
增加 client 初始化配置
Browse files Browse the repository at this point in the history
  • Loading branch information
ViolaPioggia committed Jun 7, 2024
1 parent 7ce34d5 commit ae38876
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 12 deletions.
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestSetQueryParam(t *testing.T) {
c := MustNew()
c := MustNewClient(&Option{})
c.SetQueryParam("test1", "test1")
c.SetQueryParams(map[string]string{"test2": "test2", "test3": "test3"})
c.SetQueryParamsFromValues(map[string][]string{"test4": {"test41", "test42"}})
Expand Down
145 changes: 136 additions & 9 deletions easy_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,148 @@

package easy_http

import "github.com/cloudwego/hertz/pkg/app/client"
import (
"crypto/tls"
"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/app/client/retry"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/network"
"time"
)

func New() (*Client, error) {
c, err := client.NewClient()
type Option struct {
c *client.Client
F []config.ClientOption
}

func NewOption() *Option {
return &Option{}
}

func (o *Option) WithHertzRawOption(c *client.Client) *Option {
o.c = c
return o
}

func (o *Option) WithDialTimeout(dialTimeout time.Duration) *Option {
o.F = append(o.F, client.WithDialTimeout(dialTimeout))
return o
}

func (o *Option) WithMaxConnsPerHost(mc int) *Option {
o.F = append(o.F, client.WithMaxConnsPerHost(mc))
return o
}

func (o *Option) WithMaxIdleConnDuration(t time.Duration) *Option {
o.F = append(o.F, client.WithMaxIdleConnDuration(t))
return o
}

func (o *Option) WithMaxConnDuration(t time.Duration) *Option {
o.F = append(o.F, client.WithMaxConnDuration(t))
return o
}

func (o *Option) WithMaxConnWaitTimeout(t time.Duration) *Option {
o.F = append(o.F, client.WithMaxConnWaitTimeout(t))
return o
}

func (o *Option) WithKeepAlive(keepAlive bool) *Option {
o.F = append(o.F, client.WithKeepAlive(keepAlive))
return o
}

func (o *Option) WithTLSConfig(tlsConfig *tls.Config) *Option {
o.F = append(o.F, client.WithTLSConfig(tlsConfig))
return o
}

func (o *Option) WithDialer(dialer network.Dialer) *Option {
o.F = append(o.F, client.WithDialer(dialer))
return o
}

func (o *Option) WithResponseBodyStream(flag bool) *Option {
o.F = append(o.F, client.WithResponseBodyStream(flag))
return o
}

func (o *Option) WithDisableHeaderNamesNormalizing(flag bool) *Option {
o.F = append(o.F, client.WithDisableHeaderNamesNormalizing(flag))
return o
}

func (o *Option) WithName(name string) *Option {
o.F = append(o.F, client.WithName(name))
return o
}

func (o *Option) WithNoDefaultUserAgentHeader(flag bool) *Option {
o.F = append(o.F, client.WithNoDefaultUserAgentHeader(flag))
return o
}

func (o *Option) WithDisablePathNormalizing(flag bool) *Option {
o.F = append(o.F, client.WithDisablePathNormalizing(flag))
return o
}

func (o *Option) WithRetryConfig(retryConfig retry.Option) *Option {
o.F = append(o.F, client.WithRetryConfig(retryConfig))
return o
}

func (o *Option) WithWriteTimeout(t time.Duration) *Option {
o.F = append(o.F, client.WithWriteTimeout(t))
return o
}

func (o *Option) WithConnStateObserve(hs config.HostClientStateFunc, interval ...time.Duration) *Option {
o.F = append(o.F, client.WithConnStateObserve(hs, interval...))
return o
}

func (o *Option) WithDialFunc(f network.DialFunc) *Option {
o.F = append(o.F, client.WithDialFunc(f))
return o
}

func (o *Option) WithHostClientConfigHook(h func(hc interface{}) error) *Option {
o.F = append(o.F, client.WithHostClientConfigHook(h))
return o
}

func NewClient(opts *Option) (*Client, error) {
var hertzOptions []config.ClientOption

if opts.c != nil {
return createClient(opts.c), nil
}

for _, f := range opts.F {

Check failure on line 139 in easy_http.go

View workflow job for this annotation

GitHub Actions / lint

S1011: should replace loop with `hertzOptions = append(hertzOptions, opts.F...)` (gosimple)
hertzOptions = append(hertzOptions, f)
}

c, err := client.NewClient(hertzOptions...)
return createClient(c), err
}

func MustNew() *Client {
c, err := client.NewClient()
func MustNewClient(opts *Option) *Client {
var hertzOptions []config.ClientOption

if opts.c != nil {
return createClient(opts.c)
}

for _, f := range opts.F {

Check failure on line 154 in easy_http.go

View workflow job for this annotation

GitHub Actions / lint

S1011: should replace loop with `hertzOptions = append(hertzOptions, opts.F...)` (gosimple)
hertzOptions = append(hertzOptions, f)
}

c, err := client.NewClient(hertzOptions...)
if err != nil {
panic(err)
}
return createClient(c)
}

func NewWithHertzClient(c *client.Client) *Client {
return createClient(c)
}
23 changes: 21 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,34 @@ package main

import (
"fmt"
"github.com/cloudwego/hertz/pkg/app/client"
"github.com/hertz-contrib/easy_http"
)

func main() {
c := easy_http.MustNew()
opts1 := easy_http.NewOption().WithDialTimeout(10).WithWriteTimeout(10)
hertzClient, _ := client.NewClient(client.WithDialTimeout(10), client.WithWriteTimeout(10))
opts2 := easy_http.NewOption().WithHertzRawOption(hertzClient)

res, err := c.SetHeader("test", "test").SetQueryParam("test1", "test1").R().Get("http://www.baidu.com")
c1, _ := easy_http.NewClient(opts1)
c2 := easy_http.MustNewClient(opts2)
c3 := easy_http.MustNewClient(&easy_http.Option{})

res, err := c1.SetHeader("test", "test").SetQueryParam("test1", "test1").R().Get("http://www.baidu.com")
if err != nil {
panic(err)
}
fmt.Println(res)

res, err = c2.SetHeader("test", "test").SetQueryParam("test1", "test1").R().Get("http://www.baidu.com")

Check failure on line 24 in example/main.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to res (ineffassign)
if err != nil {
panic(err)
}

res, err = c3.SetHeader("test", "test").SetQueryParam("test1", "test1").R().Get("http://www.baidu.com")
if err != nil {
panic(err)
}

fmt.Println(res)
}

0 comments on commit ae38876

Please sign in to comment.