Skip to content

Commit

Permalink
Compress requests with zstd, handle zstd compressed responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mgorven committed May 2, 2024
1 parent dd5b264 commit 10d077b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/lightsparkdev/lightspark-crypto-uniffi/lightspark-crypto-go v0.4.1
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.17.0
github.com/DataDog/zstd v1.5.5
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M=
Expand Down
17 changes: 16 additions & 1 deletion requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strconv"
"strings"
"time"
"github.com/DataDog/zstd"

lightspark "github.com/lightsparkdev/go-sdk"
)
Expand Down Expand Up @@ -142,6 +143,11 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
return nil, errors.New("error when encoding payload")
}

compressedPayload, err := zstd.Compress(nil, encodedPayload)
if err != nil {
return nil, err
}

var serverUrl string
if r.BaseUrl == nil {
serverUrl = DEFAULT_BASE_URL
Expand All @@ -152,12 +158,14 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
return nil, err
}

request, err := http.NewRequest("POST", serverUrl, bytes.NewBuffer(encodedPayload))
request, err := http.NewRequest("POST", serverUrl, bytes.NewBuffer(compressedPayload))
if err != nil {
return nil, err
}
request.SetBasicAuth(r.ApiTokenClientId, r.ApiTokenClientSecret)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Content-Encoding", "zstd")
request.Header.Add("Accept-Encoding", "zstd")
request.Header.Add("X-GraphQL-Operation", operationName)
request.Header.Add("User-Agent", r.getUserAgent())
request.Header.Add("X-Lightspark-SDK", r.getUserAgent())
Expand Down Expand Up @@ -195,6 +203,13 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
return nil, err
}

if response.Header.Get("Content-Encoding") == "zstd" {
data, err = zstd.Decompress(nil, data)
if err != nil {
return nil, err
}
}

var result map[string]interface{}
err = json.Unmarshal(data, &result)
if err != nil {
Expand Down

0 comments on commit 10d077b

Please sign in to comment.