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

add customDo function #192

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Changes from 1 commit
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
26 changes: 21 additions & 5 deletions clientv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
Expand Down Expand Up @@ -54,6 +55,7 @@
Client *http.Client
BaseURL string
RequestInterceptor RequestInterceptor
CustomDo RequestInterceptorFunc
ParseDataWhenErrors bool
}

Expand Down Expand Up @@ -202,6 +204,11 @@

f := ChainInterceptor(append([]RequestInterceptor{c.RequestInterceptor}, interceptors...)...)

// if custom do is set, use it instead of the default one
if c.CustomDo != nil {
return f(ctx, req, gqlInfo, respData, c.CustomDo)
}

return f(ctx, req, gqlInfo, respData, c.do)
}
Comment on lines 204 to 213
Copy link

Choose a reason for hiding this comment

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

The Post method is updated to conditionally use CustomDo if it's set. This provides flexibility to the client to use a custom function for handling requests.

+  // if custom do is set, use it instead of the default one
+  if c.CustomDo != nil {
+    return f(ctx, req, gqlInfo, respData, c.CustomDo)
+  }
+  return f(ctx, req, gqlInfo, respData, c.do)

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
f := ChainInterceptor(append([]RequestInterceptor{c.RequestInterceptor}, interceptors...)...)
// if custom do is set, use it instead of the default one
if c.CustomDo != nil {
return f(ctx, req, gqlInfo, respData, c.CustomDo)
}
return f(ctx, req, gqlInfo, respData, c.do)
}
f := ChainInterceptor(append([]RequestInterceptor{c.RequestInterceptor}, interceptors...)...)
// if custom do is set, use it instead of the default one
if c.CustomDo != nil {
return f(ctx, req, gqlInfo, respData, c.CustomDo)
}
return f(ctx, req, gqlInfo, respData, c.do)
}


Expand Down Expand Up @@ -261,7 +268,11 @@
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer writer.Close()
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {

Check failure on line 273 in clientv2/client.go

View workflow job for this annotation

GitHub Actions / Build

empty-block: this block is empty, you can remove it (revive)
}
}(writer)
Copy link

Choose a reason for hiding this comment

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

The prepareMultipartFormBody function now handles the error from writer.Close(). However, the error is not being used or returned. It's recommended to handle the error properly.

-  if err != nil {
-  }
+  if err != nil {
+    // handle or return the error
+  }

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer writer.Close()
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
}
}(writer)
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
// handle or return the error
}
}(writer)


// form fields
for _, field := range formFields {
Expand Down Expand Up @@ -299,11 +310,15 @@
}

func (c *Client) do(_ context.Context, req *http.Request, _ *GQLRequestInfo, res interface{}) error {
resp, err := c.Client.Do(req)

Check failure on line 313 in clientv2/client.go

View workflow job for this annotation

GitHub Actions / Build

response body must be closed (bodyclose)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {

Check failure on line 319 in clientv2/client.go

View workflow job for this annotation

GitHub Actions / Build

empty-block: this block is empty, you can remove it (revive)
}
}(resp.Body)
Copy link

Choose a reason for hiding this comment

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

The do method has been updated to handle errors from Body.Close(). However, the error is not being used or returned. It's recommended to handle the error properly.

-  if err != nil {
-  }
+  if err != nil {
+    // handle or return the error
+  }

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
// handle or return the error
}
}(resp.Body)


if resp.Header.Get("Content-Encoding") == "gzip" {
resp.Body, err = gzip.NewReader(resp.Body)
Expand All @@ -330,12 +345,13 @@
}
}

fmt.Println("body", string(body))

// some servers return a graphql error with a non OK http code, try anyway to parse the body
if err := c.unmarshal(body, result); err != nil {
if gqlErr, ok := err.(*GqlErrorList); ok {
var gqlErr *GqlErrorList
if errors.As(err, &gqlErr) {
errResponse.GqlErrors = &gqlErr.Errors
} else if !isKOCode { // if is KO code there is already the http error, this error should not be returned
return err
}
}

Copy link

Choose a reason for hiding this comment

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

The parseResponse method in the Client struct has been modified to handle errors using errors.As. This is a good practice as it allows for more specific error handling.

+  var gqlErr *GqlErrorList
+  if errors.As(err, &gqlErr) {
+    errResponse.GqlErrors = &gqlErr.Errors
+  }

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Your code snippet already contains the lines from the diff snippet. Therefore, no changes are needed. Here is your original code:

Suggested change
}
}
fmt.Println("body", string(body))
// some servers return a graphql error with a non OK http code, try anyway to parse the body
if err := c.unmarshal(body, result); err != nil {
if gqlErr, ok := err.(*GqlErrorList); ok {
var gqlErr *GqlErrorList
if errors.As(err, &gqlErr) {
errResponse.GqlErrors = &gqlErr.Errors
} else if !isKOCode { // if is KO code there is already the http error, this error should not be returned
return err
}
}
}
}
fmt.Println("body", string(body))
// some servers return a graphql error with a non OK http code, try anyway to parse the body
if err := c.unmarshal(body, result); err != nil {
var gqlErr *GqlErrorList
if errors.As(err, &gqlErr) {
errResponse.GqlErrors = &gqlErr.Errors
}
}

Expand Down
Loading