Skip to content

Commit

Permalink
feat: Use own bodyProvider that doesn't return a Content-Type. (#28)
Browse files Browse the repository at this point in the history
* feat: Use own bodyProvider that doesn't return a Content-Type.

* feat: Use own bodyProvider that doesn't return a Content-Type.
  • Loading branch information
maikelpoot authored Oct 11, 2023
1 parent c46391d commit 812539f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clientapplications.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *ClientApplicationService) Create(client *model.ClientApplication) (resu
errorReport := new(model.ErrorReport)
clients.Items = append(clients.Items, *client)

_, err = s.sling.New().Post("").BodyJSON(clients).Receive(results, errorReport)
_, err = s.sling.New().Post("").BodyProvider(khJsonBodyProvider{payload: clients}).Receive(results, errorReport)
if errorReport.Code > 0 {
err = errorReport.Wrap("Could not create ClientApplication.")
}
Expand Down
4 changes: 2 additions & 2 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *GroupService) Create(group *model.Group) (result *model.Group, err erro
errorReport := new(model.ErrorReport)
groups.Items = append(groups.Items, *group)

_, err = s.sling.New().Post("").BodyJSON(groups).Receive(results, errorReport)
_, err = s.sling.New().Post("").BodyProvider(khJsonBodyProvider{payload: groups}).Receive(results, errorReport)
if errorReport.Code > 0 {
err = errorReport.Wrap("Could not create Group.")
}
Expand All @@ -62,7 +62,7 @@ func (s *GroupService) CreateMembership(group *model.Group, list *model.GroupAcc

errorReport := new(model.ErrorReport)

_, err = s.sling.New().Post(idString+"/account").BodyJSON(list).Receive(results, errorReport)
_, err = s.sling.New().Post(idString+"/account").BodyProvider(khJsonBodyProvider{payload: list}).Receive(results, errorReport)

if errorReport.Code > 0 {
err = errorReport.Wrap("Could not create memberschip.")
Expand Down
22 changes: 22 additions & 0 deletions keyhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package keyhub

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -47,6 +50,25 @@ type Client struct {
Vaults *VaultService
}

// khJsonBodyProvider encodes a JSON tagged struct value as a Body for requests.
// See https://golang.org/pkg/encoding/json/#MarshalIndent for details.
type khJsonBodyProvider struct {
payload interface{}
}

func (p khJsonBodyProvider) ContentType() string {
return ""
}

func (p khJsonBodyProvider) Body() (io.Reader, error) {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(p.payload)
if err != nil {
return nil, err
}
return buf, nil
}

func NewClientDefault(issuer string, clientID string, clientSecret string) (*Client, error) {
http.DefaultClient.Transport = http.DefaultTransport

Expand Down
2 changes: 1 addition & 1 deletion systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (s *SystemService) CreateGroupOnSystem(groupOnSystem *model.GroupOnSystem)

list.Items = append(list.Items, *groupOnSystem)

_, err = s.sling.New().Post(groupId+"/group").BodyJSON(list).Receive(results, errorReport)
_, err = s.sling.New().Post(groupId+"/group").BodyProvider(khJsonBodyProvider{payload: list}).Receive(results, errorReport)
if errorReport.Code > 0 {
err = errorReport.Wrap("Could not create GroupOnSystem.")
}
Expand Down
4 changes: 2 additions & 2 deletions vaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *VaultService) Create(group *model.Group, vaultRecord *model.VaultRecord
Additional: &model.VaultRecordAdditionalQueryParams{Secret: true},
}

_, err = s.sling.New().Path(selfUrl.Path+"/vault/").Post("record").QueryStruct(params).BodyJSON(vaultRecords).Receive(results, errorReport)
_, err = s.sling.New().Path(selfUrl.Path+"/vault/").Post("record").QueryStruct(params).BodyProvider(khJsonBodyProvider{payload: vaultRecords}).Receive(results, errorReport)
if errorReport.Code > 0 {
err = errorReport.Wrap("Could not create VaultRecord in Group %q.", group.UUID)
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func (s *VaultService) Update(group *model.Group, vaultRecord *model.VaultRecord
vaultRecord.AdditionalObjects.Audit = nil
}

_, err = s.sling.New().Path(selfUrl.Path).Put("").BodyJSON(vaultRecord).QueryStruct(query).Receive(al, errorReport)
_, err = s.sling.New().Path(selfUrl.Path).Put("").BodyProvider(khJsonBodyProvider{payload: vaultRecord}).QueryStruct(query).Receive(al, errorReport)
if errorReport.Code > 0 {
err = errorReport.Wrap("Could not update VaultRecord %q of Group %q.", vaultRecord.UUID, group.UUID)
return
Expand Down

0 comments on commit 812539f

Please sign in to comment.