Skip to content

Commit

Permalink
feat(payments): automatically set content type in httpwrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
laouji committed Sep 17, 2024
1 parent b5e3a4a commit 2b28ddd
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Client interface {
}

type client struct {
httpClient *http.Client
httpClient *http.Client
contentType ContentType

httpErrorCheckerFn func(statusCode int) error
}
Expand All @@ -48,6 +49,7 @@ func NewClient(config *Config) (Client, error) {
}

return &client{
contentType: config.ContentType,
httpErrorCheckerFn: config.HttpErrorCheckerFn,
httpClient: &http.Client{
Timeout: config.Timeout,
Expand All @@ -57,6 +59,10 @@ func NewClient(config *Config) (Client, error) {
}

func (c *client) Do(req *http.Request, expectedBody, errorBody any) (int, error) {
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", c.contentType.String())
}

resp, err := c.httpClient.Do(req)
if err != nil {
return 0, fmt.Errorf("failed to make request: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var _ = Describe("ClientWrapper", func() {
client, err = httpwrapper.NewClient(config)
Expect(err).To(BeNil())
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expect(r.Header.Get("Content-Type")).To(Equal(httpwrapper.CONTENT_TYPE_JSON.String()))
params, err := url.ParseQuery(r.URL.RawQuery)
Expect(err).To(BeNil())

Expand Down
14 changes: 14 additions & 0 deletions components/payments/internal/connectors/httpwrapper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ import (
"time"
)

type ContentType int

const (
CONTENT_TYPE_JSON ContentType = iota
)

func (c ContentType) String() string {
switch c {
default:
return "application/json"
}
}

type Config struct {
ContentType ContentType
HttpErrorCheckerFn func(code int) error

Timeout time.Duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ func (c *Client) GetAccounts(ctx context.Context, page int, pageSize int) ([]*Ac
return nil, fmt.Errorf("failed to create accounts request: %w", err)
}

// TODO generic headers can be set in wrapper
req.Header.Set("Content-Type", "application/json")

q := req.URL.Query()
q.Add("page[size]", strconv.Itoa(pageSize))
q.Add("page[number]", fmt.Sprint(page))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (c *Client) GetAccountBalances(ctx context.Context, accountID string) ([]*B
if err != nil {
return nil, fmt.Errorf("failed to create login request: %w", err)
}
req.Header.Set("Content-Type", "application/json")

balances := balancesResponse{Balances: make([]*Balance, 0)}
var errRes moneycorpError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func (c *Client) GetRecipients(ctx context.Context, accountID string, page int,
return nil, fmt.Errorf("failed to create recipients request: %w", err)
}

req.Header.Set("Content-Type", "application/json")

q := req.URL.Query()
q.Add("page[size]", strconv.Itoa(pageSize))
q.Add("page[number]", fmt.Sprint(page))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ func (c *Client) GetTransactions(ctx context.Context, accountID string, page, pa
return nil, fmt.Errorf("failed to create transactions request: %w", err)
}

req.Header.Set("Content-Type", "application/json")

q := req.URL.Query()
q.Add("page[size]", strconv.Itoa(pageSize))
q.Add("page[number]", fmt.Sprint(page))
Expand Down

0 comments on commit 2b28ddd

Please sign in to comment.