Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor context handling to support both customtimeout and ba… #252

Merged
merged 2 commits into from
Jun 25, 2024
Merged
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
20 changes: 12 additions & 8 deletions httpclient/multipartrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,19 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
return nil, fmt.Errorf("unsupported HTTP method: %s", method)
}

log.Info("Executing multipart file upload request", zap.String("method", method), zap.String("endpoint", endpoint))

url := (*c.Integration).GetFQDN() + endpoint

// Create a context with timeout based on the custom timeout duration
ctx, cancel := context.WithTimeout(context.Background(), c.config.CustomTimeout)
var ctx context.Context
var cancel context.CancelFunc

if c.config.CustomTimeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), c.config.CustomTimeout)
log.Info("Using timeout context for multipart request", zap.Duration("timeout", c.config.CustomTimeout))
} else {
ctx = context.Background()
cancel = func() {}
log.Info("Using background context for multipart request. Caller will handle timeouts")
}
defer cancel()

var body io.Reader
Expand All @@ -92,7 +99,7 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
if err != nil {
log.Error("Failed to create streaming multipart request body", zap.Error(err))
} else {
log.Info("Successfully created multipart request body")
log.Info("Successfully created streaming multipart request body", zap.String("content_type", contentType))
}
return err
}
Expand All @@ -111,9 +118,6 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
// Log the request details
log.Info("Created HTTP Multipart request", zap.String("method", method), zap.String("url", url), zap.String("content_type", contentType))

// Set the Content-Type header
req.Header.Set("Content-Type", contentType)

(*c.Integration).PrepRequestParamsAndAuth(req)

req.Header.Set("Content-Type", contentType)
Expand Down
Loading