mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
del setProxy and setProxys, open clientOption
This commit is contained in:
51
client.go
51
client.go
@@ -14,11 +14,11 @@ import (
|
|||||||
|
|
||||||
// Connection Management
|
// Connection Management
|
||||||
type Client struct {
|
type Client struct {
|
||||||
option ClientOption
|
ClientOption ClientOption
|
||||||
transport *roundTripper
|
transport *roundTripper
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cnl context.CancelFunc
|
cnl context.CancelFunc
|
||||||
closed bool
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultClient, _ = NewClient(context.TODO())
|
var defaultClient, _ = NewClient(context.TODO())
|
||||||
@@ -35,15 +35,15 @@ func NewClient(preCtx context.Context, options ...ClientOption) (*Client, error)
|
|||||||
result := new(Client)
|
result := new(Client)
|
||||||
result.ctx, result.cnl = context.WithCancel(preCtx)
|
result.ctx, result.cnl = context.WithCancel(preCtx)
|
||||||
result.transport = newRoundTripper(result.ctx)
|
result.transport = newRoundTripper(result.ctx)
|
||||||
result.option = option
|
result.ClientOption = option
|
||||||
if result.option.TlsConfig == nil {
|
if result.ClientOption.TlsConfig == nil {
|
||||||
result.option.TlsConfig = &tls.Config{
|
result.ClientOption.TlsConfig = &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
ClientSessionCache: tls.NewLRUClientSessionCache(0),
|
ClientSessionCache: tls.NewLRUClientSessionCache(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if result.option.UtlsConfig == nil {
|
if result.ClientOption.UtlsConfig == nil {
|
||||||
result.option.UtlsConfig = &utls.Config{
|
result.ClientOption.UtlsConfig = &utls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
ClientSessionCache: utls.NewLRUClientSessionCache(0),
|
ClientSessionCache: utls.NewLRUClientSessionCache(0),
|
||||||
InsecureSkipTimeVerify: true,
|
InsecureSkipTimeVerify: true,
|
||||||
@@ -52,39 +52,18 @@ func NewClient(preCtx context.Context, options ...ClientOption) (*Client, error)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//cookiesjar
|
//cookiesjar
|
||||||
if !result.option.DisCookie {
|
if !result.ClientOption.DisCookie {
|
||||||
if result.option.Jar == nil {
|
if result.ClientOption.Jar == nil {
|
||||||
result.option.Jar = NewJar()
|
result.ClientOption.Jar = NewJar()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
if result.option.Proxy != "" {
|
if result.ClientOption.Proxy != "" {
|
||||||
_, err = gtls.VerifyProxy(result.option.Proxy)
|
_, err = gtls.VerifyProxy(result.ClientOption.Proxy)
|
||||||
}
|
}
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modifying the client's proxy
|
|
||||||
func (obj *Client) SetProxy(proxyUrl string) (err error) {
|
|
||||||
_, err = gtls.VerifyProxy(proxyUrl)
|
|
||||||
if err == nil {
|
|
||||||
obj.option.Proxy = proxyUrl
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Modifying the client's proxy
|
|
||||||
func (obj *Client) SetProxys(proxyUrls []string) (err error) {
|
|
||||||
for _, proxy := range proxyUrls {
|
|
||||||
_, err = gtls.VerifyProxy(proxy)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
obj.option.Proxys = proxyUrls
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close idle connections. If the connection is in use, wait until it ends before closing
|
// Close idle connections. If the connection is in use, wait until it ends before closing
|
||||||
func (obj *Client) CloseConns() {
|
func (obj *Client) CloseConns() {
|
||||||
obj.transport.closeConns()
|
obj.transport.closeConns()
|
||||||
|
|||||||
12
jar.go
12
jar.go
@@ -29,31 +29,31 @@ func NewJar() *jar {
|
|||||||
|
|
||||||
// get cookies
|
// get cookies
|
||||||
func (obj *Client) GetCookies(href *url.URL) Cookies {
|
func (obj *Client) GetCookies(href *url.URL) Cookies {
|
||||||
if obj.option.Jar == nil {
|
if obj.ClientOption.Jar == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return obj.option.Jar.GetCookies(href)
|
return obj.ClientOption.Jar.GetCookies(href)
|
||||||
}
|
}
|
||||||
|
|
||||||
// set cookies
|
// set cookies
|
||||||
func (obj *Client) SetCookies(href *url.URL, cookies ...any) error {
|
func (obj *Client) SetCookies(href *url.URL, cookies ...any) error {
|
||||||
if obj.option.Jar == nil {
|
if obj.ClientOption.Jar == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
cooks, err := any2cookies(href, cookies...)
|
cooks, err := any2cookies(href, cookies...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
obj.option.Jar.SetCookies(href, cooks)
|
obj.ClientOption.Jar.SetCookies(href, cooks)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear cookies
|
// clear cookies
|
||||||
func (obj *Client) ClearCookies() {
|
func (obj *Client) ClearCookies() {
|
||||||
if obj.option.Jar == nil {
|
if obj.ClientOption.Jar == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
obj.option.Jar.ClearCookies()
|
obj.ClientOption.Jar.ClearCookies()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get cookies
|
// Get cookies
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ func (obj *RequestOption) initParams() (*url.URL, error) {
|
|||||||
return baseUrl, nil
|
return baseUrl, nil
|
||||||
}
|
}
|
||||||
func (obj *Client) newRequestOption(option RequestOption) (RequestOption, error) {
|
func (obj *Client) newRequestOption(option RequestOption) (RequestOption, error) {
|
||||||
err := tools.Merge(&option, obj.option)
|
err := tools.Merge(&option, obj.ClientOption)
|
||||||
//end
|
//end
|
||||||
if option.MaxRetries < 0 {
|
if option.MaxRetries < 0 {
|
||||||
option.MaxRetries = 0
|
option.MaxRetries = 0
|
||||||
@@ -223,7 +223,7 @@ func (obj *Client) newRequestOption(option RequestOption) (RequestOption, error)
|
|||||||
option.UJa3Spec = ja3.DefaultUSpec()
|
option.UJa3Spec = ja3.DefaultUSpec()
|
||||||
}
|
}
|
||||||
if option.UserAgent == "" {
|
if option.UserAgent == "" {
|
||||||
option.UserAgent = obj.option.UserAgent
|
option.UserAgent = obj.ClientOption.UserAgent
|
||||||
}
|
}
|
||||||
if option.DisCookie {
|
if option.DisCookie {
|
||||||
option.Jar = nil
|
option.Jar = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user