Skip to content

Commit

Permalink
chore: improve set digest transport
Browse files Browse the repository at this point in the history
  • Loading branch information
ldming committed Jan 15, 2025
1 parent 2a2b787 commit 9fbec2d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
32 changes: 16 additions & 16 deletions .generator/src/generator/templates/client.j2
Original file line number Diff line number Diff line change
Expand Up @@ -392,36 +392,36 @@ func (c *APIClient) PrepareRequest(
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
}

// Set transport TLSClientConfig
setTLSClientConfig := func(transport *http.Transport, insecureSkipVerify bool) {
if transport.TLSClientConfig == nil {
transport.TLSClientConfig = &tls.Config{}
}
transport.TLSClientConfig.InsecureSkipVerify = insecureSkipVerify
}

// Get base transport
transport := c.Cfg.HTTPClient.Transport
if transport == nil {
transport = http.DefaultTransport
}

// Set insecureSkipVerify
// Set transport TLSClientConfig
if transport, ok := transport.(*http.Transport); ok {
if insecureSkipVerify, ok := ctx.Value(ContextInsecureSkipVerify).(bool); ok {
setTLSClientConfig(transport, insecureSkipVerify)
if transport.TLSClientConfig == nil {
transport.TLSClientConfig = &tls.Config{}
}
transport.TLSClientConfig.InsecureSkipVerify = insecureSkipVerify
}
}

// Digest Authentication
if auth, ok := ctx.Value(ContextDigestAuth).(DigestAuth); ok {
c.Cfg.HTTPClient.Transport = &digest.Transport{
Username: auth.UserName,
Password: auth.Password,
Transport: transport,
// Check if transport is already a digest.Transport
if _, ok := transport.(*digest.Transport); !ok {
transport = &digest.Transport{
Username: auth.UserName,
Password: auth.Password,
Transport: transport,
}
}
} else {
c.Cfg.HTTPClient.Transport = transport
}

// Set the final transport
c.Cfg.HTTPClient.Transport = transport
}

for header, value := range c.Cfg.DefaultHeader {
Expand Down
32 changes: 16 additions & 16 deletions api/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,36 +392,36 @@ func (c *APIClient) PrepareRequest(
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
}

// Set transport TLSClientConfig
setTLSClientConfig := func(transport *http.Transport, insecureSkipVerify bool) {
if transport.TLSClientConfig == nil {
transport.TLSClientConfig = &tls.Config{}
}
transport.TLSClientConfig.InsecureSkipVerify = insecureSkipVerify
}

// Get base transport
transport := c.Cfg.HTTPClient.Transport
if transport == nil {
transport = http.DefaultTransport
}

// Set insecureSkipVerify
// Set transport TLSClientConfig
if transport, ok := transport.(*http.Transport); ok {
if insecureSkipVerify, ok := ctx.Value(ContextInsecureSkipVerify).(bool); ok {
setTLSClientConfig(transport, insecureSkipVerify)
if transport.TLSClientConfig == nil {
transport.TLSClientConfig = &tls.Config{}
}
transport.TLSClientConfig.InsecureSkipVerify = insecureSkipVerify
}
}

// Digest Authentication
if auth, ok := ctx.Value(ContextDigestAuth).(DigestAuth); ok {
c.Cfg.HTTPClient.Transport = &digest.Transport{
Username: auth.UserName,
Password: auth.Password,
Transport: transport,
// Check if transport is already a digest.Transport
if _, ok := transport.(*digest.Transport); !ok {
transport = &digest.Transport{
Username: auth.UserName,
Password: auth.Password,
Transport: transport,
}
}
} else {
c.Cfg.HTTPClient.Transport = transport
}

// Set the final transport
c.Cfg.HTTPClient.Transport = transport
}

for header, value := range c.Cfg.DefaultHeader {
Expand Down

0 comments on commit 9fbec2d

Please sign in to comment.