Skip to content

Commit

Permalink
Merge pull request #100 from tarmo-randma/feature/parameters-in-body
Browse files Browse the repository at this point in the history
Feature/parameters in body
  • Loading branch information
Dysar authored Oct 22, 2021
2 parents 2db18e6 + 1a9c6c4 commit 4fa1810
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 27 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
29 changes: 20 additions & 9 deletions internal/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/erply/api-go-wrapper/pkg/api/common"
"github.com/erply/api-go-wrapper/pkg/api/log"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/erply/api-go-wrapper/pkg/api/common"
"github.com/erply/api-go-wrapper/pkg/api/log"
)

type BulkInput struct {
Expand Down Expand Up @@ -56,22 +57,32 @@ func setParams(params url.Values, filters map[string]string) {

func (cli *Client) SendRequest(ctx context.Context, apiMethod string, filters map[string]string) (*http.Response, error) {
log.Log.Log(log.Debug, "will call %s with filters %+v", apiMethod, filters)
req, err := getHTTPRequest(cli, nil)
if err != nil {
return nil, common.NewFromError("failed to build http request", err, 0)
}
req = req.WithContext(ctx)
params := cli.headersFunc(apiMethod)
log.Log.Log(log.Debug, "extracted headers %+v", params)

params, err = cli.addSessionParams(params)
params, err := cli.addSessionParams(params)
if err != nil {
return nil, err
}

setParams(params, filters)

req.URL.RawQuery = params.Encode()
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 = req.WithContext(ctx)

resp, err := doRequest(req, cli)
if err != nil {
return nil, common.NewFromError(fmt.Sprintf("%v request failed", apiMethod), err, 0)
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 4fa1810

Please sign in to comment.