Skip to content

Commit

Permalink
Set query param instead of header
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Jul 2, 2024
1 parent 592dfdb commit 87574dc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
7 changes: 2 additions & 5 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/event"
"github.com/sourcenetwork/defradb/internal/encryption"
)

var _ client.DB = (*Client)(nil)
Expand Down Expand Up @@ -350,17 +349,15 @@ func (c *Client) ExecRequest(
result.GQL.Errors = []error{err}
return result
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, methodURL.String(), bytes.NewBuffer(body))
if err != nil {
result.GQL.Errors = []error{err}
return result
}
err = c.http.setDefaultHeaders(req)

encConf := encryption.GetContextConfig(ctx)
if encConf.HasValue() && encConf.Value().IsEncrypted {
req.Header.Set(DocEncryptionHeader, "1")
}
setDocEncryptionFlagIfNeeded(ctx, req)

if err != nil {
result.GQL.Errors = []error{err}
Expand Down
10 changes: 6 additions & 4 deletions http/client_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *Collection) Create(
return err
}

setDocEncryptionHeaderIfNeeded(ctx, req)
setDocEncryptionFlagIfNeeded(ctx, req)

_, err = c.http.request(req)
if err != nil {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (c *Collection) CreateMany(
return err
}

setDocEncryptionHeaderIfNeeded(ctx, req)
setDocEncryptionFlagIfNeeded(ctx, req)

_, err = c.http.request(req)
if err != nil {
Expand All @@ -130,10 +130,12 @@ func (c *Collection) CreateMany(
return nil
}

func setDocEncryptionHeaderIfNeeded(ctx context.Context, req *http.Request) {
func setDocEncryptionFlagIfNeeded(ctx context.Context, req *http.Request) {
encConf := encryption.GetContextConfig(ctx)
if encConf.HasValue() && encConf.Value().IsEncrypted {
req.Header.Set(DocEncryptionHeader, "1")
q := req.URL.Query()
q.Set(docEncryptParam, "true")
req.URL.RawQuery = q.Encode()
}
}

Expand Down
4 changes: 2 additions & 2 deletions http/handler_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/sourcenetwork/defradb/internal/encryption"
)

const DocEncryptionHeader = "EncryptDoc"
const docEncryptParam = "encrypt"

type collectionHandler struct{}

Expand All @@ -47,7 +47,7 @@ func (s *collectionHandler) Create(rw http.ResponseWriter, req *http.Request) {
}

ctx := req.Context()
if req.Header.Get(DocEncryptionHeader) == "1" {
if req.URL.Query().Get(docEncryptParam) == "true" {
ctx = encryption.SetContextConfig(ctx, encryption.DocEncConfig{IsEncrypted: true})
}

Expand Down

0 comments on commit 87574dc

Please sign in to comment.