-
Notifications
You must be signed in to change notification settings - Fork 66
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
Conversation
WalkthroughThe 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 Changes
TipsChat with CodeRabbit Bot (
|
There was a problem hiding this 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
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this 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
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 theheader
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.
Summary by CodeRabbit
"Refactor: Enhanced HTTP Header Handling in Client Requests"
header
struct and directly manipulating thereq.Header
map.Post
function in theClient
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.