Skip to content

Commit

Permalink
Refactor request creation to explicitly state JSON or XML usage
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiante committed Feb 2, 2024
1 parent b396ac2 commit bef6848
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion r_get_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RecordAttachment struct {
}

func (c *StoreClient) GetRecord(ctx context.Context, id uuid.UUID) (*Record, error) {
req, err := c.newRequest(ctx)
req, err := c.newRequestJSON(ctx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion r_get_record_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
//
// This returns the number of bytes written and an error if any.
func (c *StoreClient) GetRecordAttachment(ctx context.Context, writer io.Writer, recordID, attachmentID uuid.UUID) (int64, error) {
req, err := c.newRequest(ctx)
req, err := c.newRequestJSON(ctx)
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion r_get_store_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type StoreStatus struct {
}

func (c *StoreClient) GetStoreStatus(ctx context.Context) (*StoreStatus, error) {
req, err := c.newRequest(ctx)
req, err := c.newRequestJSON(ctx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion r_put_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PutStoreRequest struct {
}

func (c *ServerClient) PutStore(ctx context.Context, storeName string, request *PutStoreRequest) error {
req, err := newRequest(ctx, c.c)
req, err := newRequestJSON(ctx, c.c)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion r_store_post_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PostRecordResponse struct {
}

func (c *StoreClient) PostRecord(ctx context.Context, request *RecordRequest) (*PostRecordResponse, error) {
req, err := c.newRequest(ctx)
req, err := c.newRequestJSON(ctx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion r_store_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *StoreClient) SearchQuery(ctx context.Context, url string) (*SearchRespo
}

func (c *StoreClient) Search(ctx context.Context, request *SearchRequest) (*SearchResponse, error) {
req, err := c.newRequest(ctx)
req, err := c.newRequestJSON(ctx)
if err != nil {
return nil, err
}
Expand Down
20 changes: 16 additions & 4 deletions store_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,31 @@ func NewStoreClient(c *resty.Client) *StoreClient {
return &StoreClient{c: c}
}

func (c *StoreClient) newRequest(ctx context.Context) (*resty.Request, error) {
return newRequest(ctx, c.c)
func (c *StoreClient) newRequestJSON(ctx context.Context) (*resty.Request, error) {
return newRequestJSON(ctx, c.c)
}

func newRequest(ctx context.Context, c *resty.Client) (*resty.Request, error) {
func (c *StoreClient) newRequestXML(ctx context.Context) (*resty.Request, error) {
return newRequestXML(ctx, c.c)
}

func newRequestJSON(ctx context.Context, c *resty.Client) (*resty.Request, error) {
return newRequest(ctx, c, "application/json")
}

func newRequestXML(ctx context.Context, c *resty.Client) (*resty.Request, error) {
return newRequest(ctx, c, "application/xml")
}

func newRequest(ctx context.Context, c *resty.Client, contentType string) (*resty.Request, error) {
claims := UserClaimsFromContext(ctx)
if claims == nil {
return nil, errors.New("missing user claims in context object")
}

req := c.NewRequest()
req.SetContext(ctx)
req.SetHeader("Accept", "application/json")
req.SetHeader("Accept", contentType)
claims.SetOnHeader(req.Header)

return req, nil
Expand Down

0 comments on commit bef6848

Please sign in to comment.