Skip to content

Commit

Permalink
Request body used only based on flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmo Randma committed Oct 22, 2021
1 parent bbd7e38 commit 1a9c6c4
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 23 deletions.
20 changes: 14 additions & 6 deletions internal/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,20 @@ func (dsp *DefaultSessionProvider) Invalidate() {
}

type Client struct {
Url string
httpClient *http.Client
clientCode string
partnerKey string
headersFunc AuthFunc
sessionProvider SessionProvider
Url string
httpClient *http.Client
clientCode string
partnerKey string
headersFunc AuthFunc
sessionProvider SessionProvider
sendParametersInRequestBody bool
}

//SendParametersInRequestBody indicates to the client that the request should add the data payload in the
//request body instead of using the query parameters. Using the request body eliminates the query size
//limitations imposed by the maximum URL length
func (cli *Client) SendParametersInRequestBody() {
cli.sendParametersInRequestBody = true
}

func (cli *Client) Close() {
Expand Down
18 changes: 13 additions & 5 deletions internal/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,20 @@ func (cli *Client) SendRequest(ctx context.Context, apiMethod string, filters ma

setParams(params, filters)

req, err := getHTTPRequest(cli, strings.NewReader(params.Encode()))
if err != nil {
return nil, common.NewFromError("failed to build http request", err, 0)
var req *http.Request
if cli.sendParametersInRequestBody {
req, err = getHTTPRequest(cli, strings.NewReader(params.Encode()))
if err != nil {
return nil, common.NewFromError("failed to build http request", err, 0)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else {
req, err = getHTTPRequest(cli, nil)
if err != nil {
return nil, common.NewFromError("failed to build http request", err, 0)
}
req.URL.RawQuery = params.Encode()
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req = req.WithContext(ctx)

resp, err := doRequest(req, cli)
Expand Down
82 changes: 81 additions & 1 deletion internal/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,93 @@ package common

import (
"context"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestSendRequestInBody(t *testing.T) {
calledTimes := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calledTimes++
assert.Equal(t, "", r.URL.Query().Get("clientCode"))
assert.Equal(t, "", r.URL.Query().Get("sessionKey"))
assert.Equal(t, "", r.URL.Query().Get("someKey"))
AssertFormValues(t, r, map[string]interface{}{
"clientCode": "someclient",
"sessionKey": "somesess",
"someKey": "someValue",
})
}))
defer srv.Close()

cli := NewClientWithURL(
"somesess",
"someclient",
"",
srv.URL,
&http.Client{
Timeout: 5 * time.Second,
},
nil,
)
cli.SendParametersInRequestBody()

resp, err := cli.SendRequest(
context.Background(),
"getSuppliers",
map[string]string{"someKey": "someValue"},
)
assert.NoError(t, err)
if err != nil {
return
}
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, 1, calledTimes)
}

func TestSendRequestInQuery(t *testing.T) {
calledTimes := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calledTimes++
assert.Equal(t, "someclient", r.URL.Query().Get("clientCode"))
assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey"))
assert.Equal(t, "someValue", r.URL.Query().Get("someKey"))
AssertFormValues(t, r, map[string]interface{}{
"clientCode": "someclient",
"sessionKey": "somesess",
"someKey": "someValue",
})
}))
defer srv.Close()

cli := NewClientWithURL(
"somesess",
"someclient",
"",
srv.URL,
&http.Client{
Timeout: 5 * time.Second,
},
nil,
)

resp, err := cli.SendRequest(
context.Background(),
"getSuppliers",
map[string]string{"someKey": "someValue"},
)
assert.NoError(t, err)
if err != nil {
return
}
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, 1, calledTimes)
}

func TestSendRequestBulk(t *testing.T) {
calledTimes := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
13 changes: 7 additions & 6 deletions pkg/api/addresses/addressRequests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package addresses
import (
"context"
"encoding/json"
"github.com/erply/api-go-wrapper/internal/common"
sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"

"github.com/erply/api-go-wrapper/internal/common"
sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common"
"github.com/stretchr/testify/assert"
)

func TestGetAddresses(t *testing.T) {
Expand Down Expand Up @@ -279,12 +280,12 @@ func TestGetAddressesBulkCustomUnmarshal(t *testing.T) {
{
AddressID: 123,
Address: "Some Address 123",
TypeID: "3",
TypeID: 3,
},
{
AddressID: 124,
Address: "Some Address 124",
TypeID: "4",
TypeID: 4,
},
}, suppliersBulk.BulkItems[0].Addresses)

Expand All @@ -294,7 +295,7 @@ func TestGetAddressesBulkCustomUnmarshal(t *testing.T) {
{
AddressID: 125,
Address: "Some Address 125",
TypeID: "5",
TypeID: 5,
},
}, suppliersBulk.BulkItems[1].Addresses)
assert.Equal(t, expectedStatus, suppliersBulk.BulkItems[1].Status)
Expand Down
18 changes: 13 additions & 5 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"sync"
"time"

"github.com/erply/api-go-wrapper/internal/common"
"github.com/erply/api-go-wrapper/pkg/api/addresses"
"github.com/erply/api-go-wrapper/pkg/api/auth"
Expand All @@ -18,11 +24,6 @@ import (
"github.com/erply/api-go-wrapper/pkg/api/sales"
"github.com/erply/api-go-wrapper/pkg/api/servicediscovery"
"github.com/erply/api-go-wrapper/pkg/api/warehouse"
"net/http"
"net/url"
"strconv"
"sync"
"time"
)

type Client struct {
Expand Down Expand Up @@ -59,6 +60,13 @@ func (cl *Client) GetSession() (sessionKey string, err error) {
return cl.commonClient.GetSession()
}

//SendParametersInRequestBody indicates to the client that the request should add the data payload in the
//request body instead of using the query parameters. Using the request body eliminates the query size
//limitations imposed by the maximum URL length
func (cl *Client) SendParametersInRequestBody() {
cl.commonClient.SendParametersInRequestBody()
}

//NewUnvalidatedClient returns a new Client without validating any of the incoming parameters giving the
//developer more flexibility
func NewUnvalidatedClient(sk, cc, partnerKey string, httpCli *http.Client) *Client {
Expand Down

0 comments on commit 1a9c6c4

Please sign in to comment.