Skip to content

Commit 2bdd828

Browse files
committed
more renaming
1 parent a818d55 commit 2bdd828

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

httpclient/multipartrequest.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
113113
return nil, err
114114
}
115115

116-
// Log the request details
116+
// sugar the request details
117117
c.Sugar.Info("Created HTTP Multipart request", zap.String("method", method), zap.String("url", url), zap.String("content_type", contentType))
118118

119119
(*c.Integration).PrepRequestParamsAndAuth(req)
@@ -152,43 +152,43 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
152152
// content type (e.g., "image/jpeg").
153153
// - formDataPartHeaders: A map specifying custom headers for each part of the multipart form data. The key is the field name
154154
// and the value is an http.Header containing the headers for that part.
155-
// - log: An instance of a logger implementing the logger.Logger interface, used to log informational messages, warnings,
155+
// - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
156156
// and errors encountered during the construction of the multipart request body.
157157

158158
// Returns:
159159
// - io.Reader: The constructed multipart request body reader. This reader streams the multipart form data payload ready to be sent.
160160
// - string: The content type of the multipart request body. This includes the boundary string used by the multipart writer.
161161
// - error: An error object indicating failure during the construction of the multipart request body. This could be due to issues
162162
// such as file reading errors or multipart writer errors.
163-
func createStreamingMultipartRequestBody(files map[string][]string, formDataFields map[string]string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, log *zap.SugaredLogger) (io.Reader, string, error) {
163+
func createStreamingMultipartRequestBody(files map[string][]string, formDataFields map[string]string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, sugar *zap.SugaredLogger) (io.Reader, string, error) {
164164
pr, pw := io.Pipe()
165165
writer := multipart.NewWriter(pw)
166166

167167
go func() {
168168
defer func() {
169169
if err := writer.Close(); err != nil {
170-
log.Error("Failed to close multipart writer", zap.Error(err))
170+
sugar.Error("Failed to close multipart writer", zap.Error(err))
171171
}
172172
if err := pw.Close(); err != nil {
173-
log.Error("Failed to close pipe writer", zap.Error(err))
173+
sugar.Error("Failed to close pipe writer", zap.Error(err))
174174
}
175175
}()
176176

177177
for fieldName, filePaths := range files {
178178
for _, filePath := range filePaths {
179-
log.Debug("Adding file part", zap.String("field_name", fieldName), zap.String("file_path", filePath))
180-
if err := addFilePart(writer, fieldName, filePath, fileContentTypes, formDataPartHeaders, log); err != nil {
181-
log.Error("Failed to add file part", zap.Error(err))
179+
sugar.Debug("Adding file part", zap.String("field_name", fieldName), zap.String("file_path", filePath))
180+
if err := addFilePart(writer, fieldName, filePath, fileContentTypes, formDataPartHeaders, sugar); err != nil {
181+
sugar.Error("Failed to add file part", zap.Error(err))
182182
pw.CloseWithError(err)
183183
return
184184
}
185185
}
186186
}
187187

188188
for key, val := range formDataFields {
189-
log.Debug("Adding form field", zap.String("field_name", key), zap.String("field_value", val))
190-
if err := addFormField(writer, key, val, log); err != nil {
191-
log.Error("Failed to add form field", zap.Error(err))
189+
sugar.Debug("Adding form field", zap.String("field_name", key), zap.String("field_value", val))
190+
if err := addFormField(writer, key, val, sugar); err != nil {
191+
sugar.Error("Failed to add form field", zap.Error(err))
192192
pw.CloseWithError(err)
193193
return
194194
}
@@ -209,16 +209,16 @@ func createStreamingMultipartRequestBody(files map[string][]string, formDataFiel
209209
// content type (e.g., "image/jpeg").
210210
// - formDataPartHeaders: A map specifying custom headers for each part of the multipart form data. The key is the field name
211211
// and the value is an http.Header containing the headers for that part.
212-
// - log: An instance of a logger implementing the logger.Logger interface, used to log informational messages, warnings,
212+
// - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
213213
// and errors encountered during the addition of the file part.
214214

215215
// Returns:
216216
// - error: An error object indicating failure during the addition of the file part. This could be due to issues such as
217217
// file reading errors or multipart writer errors.
218-
func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, log *zap.SugaredLogger) error {
218+
func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, sugar *zap.SugaredLogger) error {
219219
file, err := os.Open(filePath)
220220
if err != nil {
221-
log.Error("Failed to open file", zap.String("filePath", filePath), zap.Error(err))
221+
sugar.Error("Failed to open file", zap.String("filePath", filePath), zap.Error(err))
222222
return err
223223
}
224224
defer file.Close()
@@ -233,7 +233,7 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte
233233

234234
part, err := writer.CreatePart(header)
235235
if err != nil {
236-
log.Error("Failed to create form file part", zap.String("fieldName", fieldName), zap.Error(err))
236+
sugar.Error("Failed to create form file part", zap.String("fieldName", fieldName), zap.Error(err))
237237
return err
238238
}
239239

@@ -242,14 +242,14 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte
242242

243243
fileSize, err := file.Stat()
244244
if err != nil {
245-
log.Error("Failed to get file info", zap.String("filePath", filePath), zap.Error(err))
245+
sugar.Error("Failed to get file info", zap.String("filePath", filePath), zap.Error(err))
246246
return err
247247
}
248248

249-
progressLogger := logUploadProgress(file, fileSize.Size(), log)
249+
progressLogger := logUploadProgress(file, fileSize.Size(), sugar)
250250
uploadState := &UploadState{}
251-
if err := chunkFileUpload(file, encoder, progressLogger, uploadState, log); err != nil {
252-
log.Error("Failed to copy file content", zap.String("filePath", filePath), zap.Error(err))
251+
if err := chunkFileUpload(file, encoder, progressLogger, uploadState, sugar); err != nil {
252+
sugar.Error("Failed to copy file content", zap.String("filePath", filePath), zap.Error(err))
253253
return err
254254
}
255255

@@ -263,20 +263,20 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte
263263
// - writer: The multipart writer used to construct the multipart request body.
264264
// - key: The field name for the form field.
265265
// - val: The value of the form field.
266-
// - log: An instance of a logger implementing the logger.Logger interface, used to log informational messages, warnings,
266+
// - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
267267
// and errors encountered during the addition of the form field.
268268

269269
// Returns:
270270
// - error: An error object indicating failure during the addition of the form field. This could be due to issues such as
271271
// multipart writer errors.
272-
func addFormField(writer *multipart.Writer, key, val string, log *zap.SugaredLogger) error {
272+
func addFormField(writer *multipart.Writer, key, val string, sugar *zap.SugaredLogger) error {
273273
fieldWriter, err := writer.CreateFormField(key)
274274
if err != nil {
275-
log.Error("Failed to create form field", zap.String("key", key), zap.Error(err))
275+
sugar.Error("Failed to create form field", zap.String("key", key), zap.Error(err))
276276
return err
277277
}
278278
if _, err := fieldWriter.Write([]byte(val)); err != nil {
279-
log.Error("Failed to write form field", zap.String("key", key), zap.Error(err))
279+
sugar.Error("Failed to write form field", zap.String("key", key), zap.Error(err))
280280
return err
281281
}
282282
return nil
@@ -321,15 +321,15 @@ func setFormDataPartHeader(fieldname, filename, contentType string, customHeader
321321
// Parameters:
322322
// - file: The file to be uploaded.
323323
// - writer: The writer to which the file content will be written.
324-
// - log: An instance of a logger implementing the logger.Logger interface, used to log informational messages, warnings,
324+
// - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
325325
// and errors encountered during the file upload.
326326
// - updateProgress: A function to update the upload progress, typically used for logging purposes.
327327
// - uploadState: A pointer to an UploadState struct used to track the progress of the file upload for resumable uploads.
328328

329329
// Returns:
330330
// - error: An error object indicating failure during the file upload. This could be due to issues such as file reading errors
331331
// or writer errors.
332-
func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64), uploadState *UploadState, log *zap.SugaredLogger) error {
332+
func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64), uploadState *UploadState, sugar *zap.SugaredLogger) error {
333333
const chunkSize = 8 * 1024 * 1024 // 8 MB
334334
buffer := make([]byte, chunkSize)
335335
totalWritten := int64(0)
@@ -371,7 +371,7 @@ func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64)
371371

372372
if chunkWritten >= chunkSize {
373373
currentChunk++
374-
log.Debug("File Upload Chunk Sent",
374+
sugar.Debug("File Upload Chunk Sent",
375375
zap.String("file_name", fileName),
376376
zap.Int64("chunk_number", currentChunk),
377377
zap.Int64("total_chunks", totalChunks),
@@ -381,10 +381,10 @@ func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64)
381381
}
382382
}
383383

384-
// Log any remaining bytes that were written but didn't reach the log threshold
384+
// sugar any remaining bytes that were written but didn't reach the sugar threshold
385385
if chunkWritten > 0 {
386386
currentChunk++
387-
log.Debug("Final Upload Chunk Sent",
387+
sugar.Debug("Final Upload Chunk Sent",
388388
zap.String("file_name", fileName),
389389
zap.Int64("chunk_number", currentChunk),
390390
zap.Int64("total_chunks", totalChunks),
@@ -401,15 +401,15 @@ func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64)
401401
// Parameters:
402402
// - file: The file being uploaded. used for logging the file name.
403403
// - fileSize: The total size of the file being uploaded.
404-
// - log: An instance of a logger implementing the logger.Logger interface, used to log informational messages, warnings,
404+
// - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
405405
// and errors encountered during the upload.
406406

407407
// Returns:
408408
// - func(int64): A function that takes the number of bytes written as an argument and logs the upload progress.
409409
// logUploadProgress logs the upload progress based on the percentage of the total file size.
410-
func logUploadProgress(file *os.File, fileSize int64, log *zap.SugaredLogger) func(int64) {
410+
func logUploadProgress(file *os.File, fileSize int64, sugar *zap.SugaredLogger) func(int64) {
411411
var uploaded int64 = 0
412-
const logInterval = 5 // Log every 5% increment
412+
const logInterval = 5 // sugar every 5% increment
413413
lastLoggedPercentage := int64(0)
414414
startTime := time.Now()
415415
fileName := filepath.Base(file.Name())
@@ -421,9 +421,9 @@ func logUploadProgress(file *os.File, fileSize int64, log *zap.SugaredLogger) fu
421421
if percentage >= lastLoggedPercentage+logInterval {
422422
elapsedTime := time.Since(startTime)
423423

424-
log.Info("Upload progress",
424+
sugar.Info("Upload progress",
425425
zap.String("file_name", fileName),
426-
zap.Float64("uploaded_MB's", float64(uploaded)/1048576), // Log in MB (1024 * 1024)
426+
zap.Float64("uploaded_MB's", float64(uploaded)/1048576), // sugar in MB (1024 * 1024)
427427
zap.Float64("total_filesize_in_MB", float64(fileSize)/1048576),
428428
zap.String("total_uploaded_percentage", fmt.Sprintf("%d%%", percentage)),
429429
zap.Duration("elapsed_time", elapsedTime))

response/error.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (e *APIError) Error() string {
4343
}
4444

4545
// HandleAPIErrorResponse handles the HTTP error response from an API and logs the error.
46-
func HandleAPIErrorResponse(resp *http.Response, log *zap.SugaredLogger) *APIError {
46+
func HandleAPIErrorResponse(resp *http.Response, sugar *zap.SugaredLogger) *APIError {
4747
apiError := &APIError{
4848
StatusCode: resp.StatusCode,
4949
Method: resp.Request.Method,
@@ -60,13 +60,13 @@ func HandleAPIErrorResponse(resp *http.Response, log *zap.SugaredLogger) *APIErr
6060
mimeType, _ := ParseContentTypeHeader(resp.Header.Get("Content-Type"))
6161
switch mimeType {
6262
case "application/json":
63-
parseJSONResponse(bodyBytes, apiError, resp, log)
63+
parseJSONResponse(bodyBytes, apiError, resp, sugar)
6464
case "application/xml", "text/xml":
65-
parseXMLResponse(bodyBytes, apiError, resp, log)
65+
parseXMLResponse(bodyBytes, apiError, resp, sugar)
6666
case "text/html":
67-
parseHTMLResponse(bodyBytes, apiError, resp, log)
67+
parseHTMLResponse(bodyBytes, apiError, resp, sugar)
6868
case "text/plain":
69-
parseTextResponse(bodyBytes, apiError, resp, log)
69+
parseTextResponse(bodyBytes, apiError, resp, sugar)
7070
default:
7171
apiError.RawResponse = string(bodyBytes)
7272
apiError.Message = "Unknown content type error"
@@ -76,7 +76,7 @@ func HandleAPIErrorResponse(resp *http.Response, log *zap.SugaredLogger) *APIErr
7676
}
7777

7878
// parseJSONResponse attempts to parse the JSON error response and update the APIError structure.
79-
func parseJSONResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, log *zap.SugaredLogger) {
79+
func parseJSONResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, sugar *zap.SugaredLogger) {
8080
if err := json.Unmarshal(bodyBytes, apiError); err != nil {
8181
apiError.RawResponse = string(bodyBytes)
8282
} else {
@@ -88,7 +88,7 @@ func parseJSONResponse(bodyBytes []byte, apiError *APIError, resp *http.Response
8888
}
8989

9090
// parseXMLResponse dynamically parses XML error responses and accumulates potential error messages.
91-
func parseXMLResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, log *zap.SugaredLogger) {
91+
func parseXMLResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, sugar *zap.SugaredLogger) {
9292
// Always set the Raw field to the entire XML content for debugging purposes.
9393
apiError.RawResponse = string(bodyBytes)
9494

@@ -121,7 +121,7 @@ func parseXMLResponse(bodyBytes []byte, apiError *APIError, resp *http.Response,
121121
}
122122

123123
// parseTextResponse updates the APIError structure based on a plain text error response and logs it.
124-
func parseTextResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, log *zap.SugaredLogger) {
124+
func parseTextResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, sugar *zap.SugaredLogger) {
125125
// Convert the body bytes to a string and assign it to both the message and RawResponse fields of APIError.
126126
bodyText := string(bodyBytes)
127127
apiError.RawResponse = bodyText
@@ -133,7 +133,7 @@ func parseTextResponse(bodyBytes []byte, apiError *APIError, resp *http.Response
133133

134134
// parseHTMLResponse extracts meaningful information from an HTML error response,
135135
// concatenating all text within <p> tags and links found within them.
136-
func parseHTMLResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, log *zap.SugaredLogger) {
136+
func parseHTMLResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, sugar *zap.SugaredLogger) {
137137
// Set the entire HTML content as the RawResponse for debugging purposes.
138138
apiError.RawResponse = string(bodyBytes)
139139

0 commit comments

Comments
 (0)