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

Corrected areas of concern. #190

Closed
wants to merge 6 commits into from
Closed

Corrected areas of concern. #190

wants to merge 6 commits into from

Conversation

Yamashou
Copy link
Owner

@Yamashou Yamashou commented Oct 10, 2023

Summary by CodeRabbit

"Refactor: Enhanced HTTP Header Handling in Client Requests"

  • Refactored the way HTTP headers are set in client requests, ensuring the 'Content-Type' and 'Accept' headers are always appropriately set.
  • Simplified the header handling logic by removing the header struct and directly manipulating the req.Header map.
  • Optimized the Post function in the Client struct to set headers based on the presence of multipart files, eliminating unnecessary buffer creation for the request body when no multipart files are present.

@Yamashou Yamashou self-assigned this Oct 10, 2023
@Yamashou Yamashou marked this pull request as ready for review October 10, 2023 02:09
@coderabbitai
Copy link

coderabbitai bot commented Oct 10, 2023

Walkthrough

The changes primarily focus on improving the handling of HTTP headers in client requests. The code now explicitly sets the "Content-Type" and "Accept" headers if they are not already set, ensuring correct content type and expected response format. The handling of headers has been simplified by directly manipulating the req.Header map, removing the need for a separate header struct.

Changes

File Summary
client/client.go Added logic to set "Content-Type" and "Accept" headers if not already set, preserving existing values if present.
clientv2/client.go Removed header struct, simplified header handling by directly manipulating req.Header map. Set "Content-Type" and "Accept" headers based on presence of multipart files.

🐇

"In the land of code, where the shadows lie,

A rabbit hopped, with a twinkle in its eye.

Headers set, and logic neat,

Simplified code, isn't that sweet? 🍬"


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • Mention @coderabbitai in any review comment for bot assistance.
  • Note: Review comments are made on code diffs or files, not on the PR overview.

Pause Incremental Reviews

  • Insert @coderabbitai: ignore in the PR description to halt the bot's ongoing reviews. Remove the line to resume.
  • Additionally, enter @coderabbitai pause to halt the bot's ongoing reviews as any issue or review comment. To
    resume reviews, enter @coderabbitai resume as any issue or review comment.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between d5b6a31 and c63c334.
Files selected for processing (2)
  • client/client.go (1 hunks)
  • clientv2/client.go (3 hunks)
Files skipped from review due to trivial changes (2)
  • client/client.go
  • clientv2/client.go

@@ -178,26 +172,31 @@ func (c *Client) Post(ctx context.Context, operationName, query string, respData
return fmt.Errorf("failed to prepare form body: %w", err)
}

headers = append(headers, header{key: "Content-Type", value: contentType})
multipartFilesGroupsContentType = contentType
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yamashou

Hello! It seems like you've tagged me but didn't provide any specific questions or requests. How can I assist you with this code review?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the mpartFilesGroupsContentType variable is redundant. Can we build the request without defining this variable?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yamashou

We could eliminate the multipartFilesGroupsContentType variable by setting the "Content-Type" header directly within the conditional block where multipart files are processed. Here's how the code would look:

@@ -178,26 +172,31 @@ func (c *Client) Post(ctx context.Context, operationName, query string, respData
 			return fmt.Errorf("failed to prepare form body: %w", err)
 		}
 
-		multipartFilesGroupsContentType = contentType
+		req.Header.Set("Content-Type", contentType)
 	} else {
 		requestBody, err := json.Marshal(r)
 		if err != nil {
 			return fmt.Errorf("encode: %w", err)
 		}
 
 		body = bytes.NewBuffer(requestBody)
 	}
 
 	req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL, body)
 	if err != nil {
 		return fmt.Errorf("create request struct failed: %w", err)
 	}
 
-	if req.Header.Get("Content-Type") == "" {
-		if multipartFilesGroupsContentType != "" {
-			req.Header.Set("Content-Type", multipartFilesGroupsContentType)
-		} else {
-			req.Header.Set("Content-Type", "application/json; charset=utf-8")
-		}
+	if req.Header.Get("Content-Type") == "" { 
+		req.Header.Set("Content-Type", "application/json; charset=utf-8")
 	}
 
 	if req.Header.Get("Accept") == "" {
 		req.Header.Set("Accept", "application/json; charset=utf-8")
 	}
 
 	f := ChainInterceptor(append([]RequestInterceptor{c.RequestInterceptor}, interceptors...)...)

This change would directly set the "Content-Type" header when multipart files are present, and if not, it would set the "Content-Type" to "application/json; charset=utf-8" after the request is created. This eliminates the need for the multipartFilesGroupsContentType variable.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require that the req be created before the body is created, which seems more verbose.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yamashou

You're correct. The http.NewRequestWithContext call needs the body as an argument, so it must be created after the body. My previous suggestion would indeed require the req to be created before the body, which is not ideal.

In that case, we can keep the multipartFilesGroupsContentType variable to hold the content type when multipart files are present. This way, we can create the body and req in their current order, and then set the headers afterwards. This approach maintains the current order of operations and avoids the verbosity of creating the req earlier.

Here's how the code would look:

@@ -178,26 +172,31 @@ func (c *Client) Post(ctx context.Context, operationName, query string, respData
 			return fmt.Errorf("failed to prepare form body: %w", err)
 		}
 
-		multipartFilesGroupsContentType = contentType
+		req.Header.Set("Content-Type", contentType)
 	} else {
 		requestBody, err := json.Marshal(r)
 		if err != nil {
 			return fmt.Errorf("encode: %w", err)
 		}
 
 		body = bytes.NewBuffer(requestBody)
 	}
 
 	req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL, body)
 	if err != nil {
 		return fmt.Errorf("create request struct failed: %w", err)
 	}
 
-	if req.Header.Get("Content-Type") == "" {
-		if multipartFilesGroupsContentType != "" {
-			req.Header.Set("Content-Type", multipartFilesGroupsContentType)
-		} else {
-			req.Header.Set("Content-Type", "application/json; charset=utf-8")
-		}
+	if req.Header.Get("Content-Type") == "" { 
+		req.Header.Set("Content-Type", "application/json; charset=utf-8")
 	}
 
 	if req.Header.Get("Accept") == "" {
 		req.Header.Set("Accept", "application/json; charset=utf-8")
 	}
 
 	f := ChainInterceptor(append([]RequestInterceptor{c.RequestInterceptor}, interceptors...)...)

This change would directly set the "Content-Type" header when multipart files are present, and if not, it would set the "Content-Type" to "application/json; charset=utf-8" after the request is created. This eliminates the need for the multipartFilesGroupsContentType variable.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between c63c334 and ab3bb8c.
Files selected for processing (1)
  • clientv2/client.go (3 hunks)
Additional comments (Suppressed): 3
clientv2/client.go (3)
  • 143-145: The header struct has been removed and the header handling logic has been simplified. Ensure that this change does not affect other parts of the code that might be using the header struct.

  • 152-157: The creation of a bytes.Buffer for the request body has been eliminated when there are no multipart files. This is a good optimization for resource usage.

  • 185-200: The "Content-Type" and "Accept" headers are now set directly on the req object. The "Content-Type" header is set based on the presence of multipart files, defaulting to "application/json; charset=utf-8" if none are present. The "Accept" header is always set to "application/json; charset=utf-8". This is a more direct and efficient way of setting headers.

@Yamashou Yamashou closed this Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant