Skip to content

Commit

Permalink
Implemented a new ExecuteGraphqlWithContext function (#86)
Browse files Browse the repository at this point in the history
* Implemented a new `ExecuteGraphqlWithContext` function so that users willing to honor a given context termination can use the Lightspark go-sdk.

* Created a `WithContext` options to `LightsparkClient` that allows receiving a context for proper termination of every called operation.
  • Loading branch information
balena-zh authored May 13, 2024
1 parent ef1bffe commit 3549442
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 80 deletions.
23 changes: 16 additions & 7 deletions requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package requester

import (
"bytes"
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
Expand All @@ -16,6 +17,7 @@ import (
"strconv"
"strings"
"time"

"github.com/DataDog/zstd"

lightspark "github.com/lightsparkdev/go-sdk"
Expand All @@ -25,7 +27,7 @@ import (
// It could be due to a service outage or a network error.
// The request should be retried if RequestError is returned with server errors (500-599).
type RequestError struct {
Message string
Message string
StatusCode int
}

Expand All @@ -34,7 +36,7 @@ func (e RequestError) Error() string {
}

// GraphQLInternalError indicates there's a failure in the Lightspark API.
// It could be due to a bug on Ligthspark's side.
// It could be due to a bug on Ligthspark's side.
// The request can be retried, because the error might be transient.
type GraphQLInternalError struct {
Message string
Expand All @@ -48,11 +50,11 @@ func (e GraphQLInternalError) Error() string {
// The request should not be retried, because the error is due to the user's input.
type GraphQLError struct {
Message string
Type string
Type string
}

func (e GraphQLError) Error() string {
return e.Type + ": " + e.Message
return e.Type + ": " + e.Message
}

type Requester struct {
Expand Down Expand Up @@ -107,6 +109,12 @@ const DEFAULT_BASE_URL = "https://api.lightspark.com/graphql/server/2023-09-13"

func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{},
signingKey SigningKey,
) (map[string]interface{}, error) {
return r.ExecuteGraphqlWithContext(context.Background(), query, variables, signingKey)
}

func (r *Requester) ExecuteGraphqlWithContext(ctx context.Context, query string, variables map[string]interface{},
signingKey SigningKey,
) (map[string]interface{}, error) {
re := regexp.MustCompile(`(?i)\s*(?:query|mutation)\s+(?P<OperationName>\w+)`)
matches := re.FindStringSubmatch(query)
Expand Down Expand Up @@ -143,7 +151,7 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
return nil, errors.New("error when encoding payload")
}

body := encodedPayload;
body := encodedPayload
compressed := false
if len(encodedPayload) > 1024 {
compressed = true
Expand All @@ -163,10 +171,11 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
return nil, err
}

request, err := http.NewRequest("POST", serverUrl, bytes.NewBuffer(body))
request, err := http.NewRequestWithContext(ctx, http.MethodPost, serverUrl, bytes.NewBuffer(body))
if err != nil {
return nil, err
}

request.SetBasicAuth(r.ApiTokenClientId, r.ApiTokenClientSecret)
request.Header.Add("Content-Type", "application/json")
if compressed {
Expand Down Expand Up @@ -202,7 +211,7 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
}
defer response.Body.Close()
if response.StatusCode < 200 || response.StatusCode > 299 {
return nil, RequestError { Message: response.Status, StatusCode: response.StatusCode }
return nil, RequestError{Message: response.Status, StatusCode: response.StatusCode}
}

data, err := io.ReadAll(response.Body)
Expand Down
Loading

0 comments on commit 3549442

Please sign in to comment.