mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
154233df14
|
||
|
|
29afbd08fd
|
||
|
|
a107ee953b
|
||
|
|
58094f85c7
|
+27
-5
@@ -2,6 +2,7 @@ package gql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -18,7 +19,11 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
}
|
||||
|
||||
for key, value := range qe.Headers {
|
||||
httpRequest.Header.Set(key, value.(string))
|
||||
strValue, ok := value.(string)
|
||||
if !ok {
|
||||
strValue = fmt.Sprintf("%v", value)
|
||||
}
|
||||
httpRequest.Header.Set(key, strValue)
|
||||
}
|
||||
|
||||
var retries_max int
|
||||
@@ -41,14 +46,32 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
qe.Logger.Debug("Error while executing http request", map[string]interface{}{"error": err.Error()})
|
||||
return err
|
||||
}
|
||||
defer io.Copy(io.Discard, httpResponse.Body)
|
||||
defer httpResponse.Body.Close()
|
||||
defer func() {
|
||||
_, err := io.Copy(io.Discard, httpResponse.Body)
|
||||
if err != nil {
|
||||
qe.Logger.Debug("Error while discarding http response body;", map[string]interface{}{"error": err.Error()})
|
||||
}
|
||||
httpResponse.Body.Close()
|
||||
}()
|
||||
|
||||
if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 204 {
|
||||
return fmt.Errorf("HTTP error - unacceptable status code: \"%s\" for \"%s\"", httpResponse.Status, httpRequest.URL)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(httpResponse.Body)
|
||||
var reader io.ReadCloser
|
||||
encoding := httpResponse.Header.Get("Content-Encoding")
|
||||
if encoding == "gzip" {
|
||||
reader, err = gzip.NewReader(httpResponse.Body)
|
||||
if err != nil {
|
||||
qe.Logger.Debug("Error while creating gzip reader;", map[string]interface{}{"error": err.Error()})
|
||||
return fmt.Errorf("Error while creating gzip reader: %s", err.Error())
|
||||
}
|
||||
defer reader.Close()
|
||||
} else {
|
||||
reader = httpResponse.Body
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
qe.Logger.Debug("Error while reading http response;", map[string]interface{}{"error": err.Error()})
|
||||
return fmt.Errorf("Error while reading http response: %s", err.Error())
|
||||
@@ -59,7 +82,6 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
qe.Logger.Debug("Error while unmarshalling http response;", map[string]interface{}{"error": err.Error()})
|
||||
return fmt.Errorf("Error while unmarshalling http response: %s", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
retry.OnRetry(func(n uint, err error) {
|
||||
|
||||
@@ -11,14 +11,18 @@ import (
|
||||
|
||||
func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
||||
httpTransport := &http.Transport{
|
||||
MaxIdleConns: 10,
|
||||
MaxIdleConns: 512,
|
||||
MaxConnsPerHost: 512,
|
||||
MaxIdleConnsPerHost: 512,
|
||||
IdleConnTimeout: 15 * time.Second,
|
||||
ResponseHeaderTimeout: 15 * time.Second,
|
||||
DisableKeepAlives: false,
|
||||
DisableCompression: false,
|
||||
}
|
||||
|
||||
if strings.HasPrefix(b.endpoint, "http://") {
|
||||
http_client = &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
Transport: httpTransport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
func NewConnection() (b *BaseClient) {
|
||||
b = &BaseClient{
|
||||
endpoint: envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql"),
|
||||
responseType: envutil.Getenv("GRAPHQL_RESPONSE_TYPE", "string"),
|
||||
responseType: envutil.Getenv("GRAPHQL_OUTPUT", "string"),
|
||||
Logger: logging.NewLogger(),
|
||||
cache: cache.New(time.Duration(envutil.GetInt("GRAPHQL_CACHE_TTL", 5)) * time.Second),
|
||||
cache_global: envutil.GetBool("GRAPHQL_CACHE_ENABLED", false),
|
||||
|
||||
Reference in New Issue
Block a user