Skip to content

Commit

Permalink
Merge pull request #358 from askuy/feature/cookiejar20230817
Browse files Browse the repository at this point in the history
http client support cookie jar
  • Loading branch information
askuy authored Aug 17, 2023
2 parents 081a0ae + 78509c5 commit 3002a2b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
12 changes: 4 additions & 8 deletions client/ehttp/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package ehttp
import (
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"

"github.com/go-resty/resty/v2"
"github.com/gotomicro/ego/client/ehttp/resolver"
"github.com/gotomicro/ego/core/eregistry"
"golang.org/x/net/publicsuffix"

"github.com/gotomicro/ego/core/eapp"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/core/eregistry"
)

// PackageName 设置包名
Expand Down Expand Up @@ -44,21 +41,20 @@ func newComponent(name string, config *Config, logger *elog.Component) *Componen
addr = strings.ReplaceAll(config.Addr, egoTarget.Scheme+"://", "http://")
}
builder := resolver.Get(egoTarget.Scheme)
resolver, err := builder.Build(addr)
resolverBuild, err := builder.Build(addr)
if err != nil {
elog.Panic("build resolver error", elog.FieldErr(err), elog.FieldKey(config.Addr))
}

// resty的默认方法,无法设置长连接个数,和是否开启长连接,这里重新构造http client。
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
interceptors := []interceptor{fixedInterceptor, logInterceptor, metricInterceptor, traceInterceptor}
cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: cookieJar}).
cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: config.cookieJar}).
SetDebug(config.RawDebug).
SetTimeout(config.ReadTimeout).
SetHeader("app", eapp.Name()).
SetBaseURL(addr)
for _, interceptor := range interceptors {
onBefore, onAfter, onErr := interceptor(name, config, logger, resolver)
onBefore, onAfter, onErr := interceptor(name, config, logger, resolverBuild)
if onBefore != nil {
cli.OnBeforeRequest(onBefore)
}
Expand Down
30 changes: 16 additions & 14 deletions client/ehttp/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ehttp

import (
"net/http"
"regexp"
"runtime"
"time"
Expand All @@ -10,20 +11,21 @@ import (

// Config HTTP配置选项
type Config struct {
Addr string // 连接地址
Debug bool // 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据
RawDebug bool // 是否开启原生调试,默认不开启
ReadTimeout time.Duration // 读超时,默认2s
SlowLogThreshold time.Duration // 慢日志记录的阈值,默认500ms
IdleConnTimeout time.Duration // 设置空闲连接时间,默认90 * time.Second
MaxIdleConns int // 设置最大空闲连接数
MaxIdleConnsPerHost int // 设置长连接个数
EnableTraceInterceptor bool // 是否开启链路追踪,默认开启
EnableKeepAlives bool // 是否开启长连接,默认打开
EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启
EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启
PathRelabel []Relabel // path 重命名 (metric 用)
EnableMetricsInterceptor bool // 是否开启监控,默认开启
Addr string // 连接地址
Debug bool // 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据
RawDebug bool // 是否开启原生调试,默认不开启
ReadTimeout time.Duration // 读超时,默认2s
SlowLogThreshold time.Duration // 慢日志记录的阈值,默认500ms
IdleConnTimeout time.Duration // 设置空闲连接时间,默认90 * time.Second
MaxIdleConns int // 设置最大空闲连接数
MaxIdleConnsPerHost int // 设置长连接个数
EnableTraceInterceptor bool // 是否开启链路追踪,默认开启
EnableKeepAlives bool // 是否开启长连接,默认打开
EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启
EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启
PathRelabel []Relabel // path 重命名 (metric 用)
cookieJar http.CookieJar // 用于缓存cookie
EnableMetricsInterceptor bool // 是否开启监控,默认开启
}

// Relabel ...
Expand Down
13 changes: 12 additions & 1 deletion client/ehttp/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ehttp

import "time"
import (
"net/http"
"time"
)

// WithAddr 设置HTTP地址
func WithAddr(addr string) Option {
Expand Down Expand Up @@ -99,3 +102,11 @@ func WithPathRelabel(match string, replacement string) Option {
c.config.PathRelabel = append(c.config.PathRelabel, Relabel{Match: match, Replacement: replacement})
}
}

// WithJar 设置Cookie,设置后,请求第一次接口后获取Cookie,第二次请求会带上Cookie,适合一些登录场景
// 例子:cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
func WithJar(jar http.CookieJar) Option {
return func(c *Container) {
c.config.cookieJar = jar
}
}

0 comments on commit 3002a2b

Please sign in to comment.