Skip to content

Commit

Permalink
add customDo function
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamashou committed Nov 9, 2023
1 parent 0117508 commit 7fbb2ea
Showing 1 changed file with 21 additions and 5 deletions.
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 @@ import (
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
Expand Down Expand Up @@ -54,6 +55,7 @@ type Client struct {
Client *http.Client
BaseURL string
RequestInterceptor RequestInterceptor
CustomDo RequestInterceptorFunc
ParseDataWhenErrors bool
}

Expand Down Expand Up @@ -202,6 +204,11 @@ func (c *Client) Post(ctx context.Context, operationName, query string, respData

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 @@ func prepareMultipartFormBody(
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)

// form fields
for _, field := range formFields {
Expand Down Expand Up @@ -303,7 +314,11 @@ func (c *Client) do(_ context.Context, req *http.Request, _ *GQLRequestInfo, res
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)

if resp.Header.Get("Content-Encoding") == "gzip" {
resp.Body, err = gzip.NewReader(resp.Body)
Expand All @@ -330,12 +345,13 @@ func (c *Client) parseResponse(body []byte, httpCode int, result interface{}) er
}
}

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
}
}

Expand Down

0 comments on commit 7fbb2ea

Please sign in to comment.