Skip to content

Commit 959e29f

Browse files
committed
Refactor HTTP client to use streaming multipart request with retry logic
1 parent f898c14 commit 959e29f

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

httpclient/multipartrequestwithretry.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,36 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
9393

9494
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log)
9595

96-
var resp *http.Response
97-
var requestErr error
96+
// Create a context with timeout based on the custom timeout duration
97+
ctx, cancel := context.WithTimeout(context.Background(), c.clientConfig.ClientOptions.Timeout.CustomTimeout.Duration())
98+
defer cancel()
9899

99-
// Retry logic
100-
maxRetries := 3
101-
for attempt := 1; attempt <= maxRetries; attempt++ {
102-
// Create a context with timeout based on the custom timeout duration
103-
ctx, cancel := context.WithTimeout(context.Background(), c.clientConfig.ClientOptions.Timeout.CustomTimeout.Duration())
104-
defer cancel()
100+
body, contentType, err := createStreamingMultipartRequestBody(files, formDataFields, fileContentTypes, formDataPartHeaders, log)
101+
if err != nil {
102+
log.Error("Failed to create streaming multipart request body", zap.Error(err))
103+
return nil, err
104+
}
105105

106-
body, contentType, err := createStreamingMultipartRequestBody(files, formDataFields, fileContentTypes, formDataPartHeaders, log)
107-
if err != nil {
108-
log.Error("Failed to create streaming multipart request body", zap.Error(err))
109-
return nil, err
110-
}
106+
req, err := http.NewRequestWithContext(ctx, method, url, body)
107+
if err != nil {
108+
log.Error("Failed to create HTTP request", zap.Error(err))
109+
return nil, err
110+
}
111111

112-
req, err := http.NewRequestWithContext(ctx, method, url, body)
113-
if err != nil {
114-
log.Error("Failed to create HTTP request", zap.Error(err))
115-
return nil, err
116-
}
112+
cookiejar.ApplyCustomCookies(req, c.clientConfig.ClientOptions.Cookies.CustomCookies, c.Logger)
117113

118-
cookiejar.ApplyCustomCookies(req, c.clientConfig.ClientOptions.Cookies.CustomCookies, c.Logger)
114+
req.Header.Set("Content-Type", contentType)
119115

120-
req.Header.Set("Content-Type", contentType)
116+
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler)
117+
headerHandler.SetRequestHeaders(endpoint)
118+
headerHandler.LogHeaders(c.clientConfig.ClientOptions.Logging.HideSensitiveData)
121119

122-
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler)
123-
headerHandler.SetRequestHeaders(endpoint)
124-
headerHandler.LogHeaders(c.clientConfig.ClientOptions.Logging.HideSensitiveData)
120+
var resp *http.Response
121+
var requestErr error
125122

123+
// Retry logic
124+
maxRetries := 3
125+
for attempt := 1; attempt <= maxRetries; attempt++ {
126126
startTime := time.Now()
127127

128128
resp, requestErr = c.httpClient.Do(req)

0 commit comments

Comments
 (0)