Update dependencies, switch got avast retry.

This commit is contained in:
2022-12-05 12:22:12 +00:00
parent 277ef21263
commit 952749d7ff
3 changed files with 31 additions and 100 deletions
+26 -25
View File
@@ -2,15 +2,14 @@ package gql
import (
"bytes"
"context"
"crypto/md5"
"errors"
"fmt"
"io/ioutil"
"net/http"
retry "github.com/avast/retry-go"
"github.com/lukaszraczylo/pandati"
retry "github.com/sethvargo/go-retry"
)
type requestBase struct {
@@ -40,7 +39,6 @@ func (g *GraphQL) queryBuilder(queryContent string, queryVariables interface{})
}
func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHeaders map[string]interface{}) (responseContent string, err error) {
ctx := context.Background()
g.Log.Debug("Query details", map[string]interface{}{"_query": queryContent, "_variables": queryVariables})
query, err := g.queryBuilder(queryContent, queryVariables)
if err != nil {
@@ -77,31 +75,34 @@ func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHe
var httpResponse *http.Response
retry.Do(ctx, g.BackoffSetup, func(ctx context.Context) error {
httpResponse, err = g.HttpClient.Do(httpRequest)
if err != nil {
g.Log.Debug("Unable to send the query RETRY", map[string]interface{}{"_error": err.Error()})
return err
}
if httpResponse.StatusCode <= 200 && httpResponse.StatusCode >= 204 {
return retry.RetryableError(errors.New(fmt.Sprintf("%v", httpResponse.StatusCode)))
}
err = retry.Do(
func() error {
httpResponse, err = g.HttpClient.Do(httpRequest)
defer httpResponse.Body.Close()
if err != nil {
g.Log.Debug("Unable to send the query RETRY", map[string]interface{}{"_error": err.Error()})
return err
}
defer httpResponse.Body.Close()
body, err = ioutil.ReadAll(httpResponse.Body)
if err != nil {
g.Log.Debug("Unable to read the response", map[string]interface{}{"_error": err.Error()})
return retry.RetryableError(errors.New(fmt.Sprintf("RETRY: body parsing %v", httpResponse.StatusCode)))
}
if httpResponse.StatusCode <= 200 && httpResponse.StatusCode >= 204 {
return err
}
err = json.Unmarshal(body, &queryResult)
if err != nil {
g.Log.Debug("Unable to unmarshal the query", map[string]interface{}{"_error": err.Error()})
return retry.RetryableError(errors.New(fmt.Sprintf("RETRY: body unmarshal %v", httpResponse.StatusCode)))
}
return nil
})
body, err = ioutil.ReadAll(httpResponse.Body)
if err != nil {
g.Log.Debug("Unable to read the response", map[string]interface{}{"_error": err.Error()})
return err
}
err = json.Unmarshal(body, &queryResult)
if err != nil {
g.Log.Debug("Unable to unmarshal the query", map[string]interface{}{"_error": err.Error()})
return err
}
return nil
},
)
if !pandati.IsZero(queryResult.Errors) {
g.Log.Error("Query returned error", map[string]interface{}{"_query": queryContent, "_variables": queryVariables, "_error": fmt.Sprintf("%v", queryResult.Errors), "_response_code": httpResponse.StatusCode})