Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions net/gclient/gclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Client struct {
authPass string // HTTP basic authentication: pass.
retryCount int // Retry count when request fails.
noUrlEncode bool // No url encoding for request parameters.
queryParams map[string]any // Query parameters map.
retryInterval time.Duration // Retry interval when request fails.
middlewareHandler []HandlerFunc // Interceptor handlers
discovery gsvc.Discovery // Discovery for service.
Expand Down Expand Up @@ -83,10 +84,11 @@ func New() *Client {
}).DialContext,
},
},
header: make(map[string]string),
cookies: make(map[string]string),
builder: gsel.GetBuilder(),
discovery: nil,
header: make(map[string]string),
cookies: make(map[string]string),
queryParams: make(map[string]any),
builder: gsel.GetBuilder(),
discovery: nil,
}
c.header[httpHeaderUserAgent] = defaultClientAgent
// It enables OpenTelemetry for client in default.
Expand All @@ -106,6 +108,10 @@ func (c *Client) Clone() *Client {
for k, v := range c.cookies {
newClient.cookies[k] = v
}
newClient.queryParams = make(map[string]any, len(c.queryParams))
for k, v := range c.queryParams {
newClient.queryParams[k] = v
}
return newClient
}

Expand Down
22 changes: 22 additions & 0 deletions net/gclient/gclient_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,25 @@ func (c *Client) NoUrlEncode() *Client {
newClient.SetNoUrlEncode(true)
return newClient
}

// Query is a chaining function, which sets query parameters with map for next request.
func (c *Client) Query(m map[string]any) *Client {
newClient := c.Clone()
newClient.SetQueryMap(m)
return newClient
}

// QueryParams is a chaining function, which sets query parameters with struct or map object for next request.
// The `params` can be type of: string/[]byte/map/struct/*struct.
func (c *Client) QueryParams(params any) *Client {
newClient := c.Clone()
newClient.SetQueryParams(params)
return newClient
}

// QueryPair is a chaining function, which sets a query parameter pair for next request.
func (c *Client) QueryPair(key string, value any) *Client {
newClient := c.Clone()
newClient.SetQuery(key, value)
return newClient
}
42 changes: 42 additions & 0 deletions net/gclient/gclient_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/gogf/gf/v2/net/gsvc"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)

// SetBrowserMode enables browser mode of the client.
Expand Down Expand Up @@ -216,3 +217,44 @@ func (c *Client) SetBuilder(builder gsel.Builder) {
func (c *Client) SetDiscovery(discovery gsvc.Discovery) {
c.discovery = discovery
}

// SetQuery sets a query parameter pair for the client.
func (c *Client) SetQuery(key string, value any) *Client {
if c.queryParams == nil {
c.queryParams = make(map[string]any)
}
// Filter out nil values to maintain consistency with SetQueryParams and mergeQueryParams
if value != nil {
c.queryParams[key] = value
}
return c
}

// SetQueryMap sets query parameters with map.
func (c *Client) SetQueryMap(m map[string]any) *Client {
if c.queryParams == nil {
c.queryParams = make(map[string]any)
}
for k, v := range m {
// Filter out nil values to maintain consistency with SetQueryParams and mergeQueryParams
if v != nil {
c.queryParams[k] = v
}
}
return c
}

// SetQueryParams sets query parameters with struct or map object.
// The `params` can be type of: string/[]byte/map/struct/*struct.
func (c *Client) SetQueryParams(params any) *Client {
if c.queryParams == nil {
c.queryParams = make(map[string]any)
}
m := gconv.Map(params)
for k, v := range m {
if v != nil {
c.queryParams[k] = v
}
}
return c
}
Loading
Loading