mirror of
https://github.com/bestbytes/datatrans.git
synced 2025-10-16 12:05:36 +00:00
Add merchant specific opaque data container
does not get used in requests towards datatrans but Data map can contain merchant specific configurations.
This commit is contained in:
parent
d27234f7e6
commit
c0f532aa74
55
client.go
55
client.go
@ -11,6 +11,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -50,6 +51,9 @@ type OptionMerchant struct {
|
||||
DisableRawJSONBody bool
|
||||
MerchantID string // basic auth user
|
||||
Password string // basic auth pw
|
||||
// Data contains merchant specific other IDs or configurations. Keys/Values
|
||||
// from this map are not getting used in requests towards datatrans.
|
||||
Data map[string]interface{}
|
||||
}
|
||||
|
||||
func (m OptionMerchant) apply(c *Client) error {
|
||||
@ -536,3 +540,54 @@ func (c *Client) ReconciliationsSalesBulk(ctx context.Context, sales RequestReco
|
||||
}
|
||||
return &rrs, nil
|
||||
}
|
||||
|
||||
// GetDataInt returns the int value from the data map or false if not found or failed to convert.
|
||||
func (c *Client) GetDataInt(key string) (int, bool) {
|
||||
internalID := c.currentInternalID
|
||||
if !c.internalIDFound {
|
||||
return 0, false
|
||||
}
|
||||
raw, ok := c.merchants[internalID].Data[key]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
switch t := raw.(type) {
|
||||
case string:
|
||||
i, err := strconv.Atoi(t)
|
||||
return i, err == nil
|
||||
case int:
|
||||
return t, true
|
||||
case int64:
|
||||
return int(t), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetDataString returns the string value from the data map or false if not found or failed to convert.
|
||||
func (c *Client) GetDataString(key string) (string, bool) {
|
||||
internalID := c.currentInternalID
|
||||
if !c.internalIDFound {
|
||||
return "", false
|
||||
}
|
||||
raw, ok := c.merchants[internalID].Data[key]
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
switch t := raw.(type) {
|
||||
case string:
|
||||
return t, true
|
||||
case []byte:
|
||||
return string(t), true
|
||||
default:
|
||||
return fmt.Sprintf("%v", t), true
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetDataRaw(key string) (interface{}, bool) {
|
||||
internalID := c.currentInternalID
|
||||
if !c.internalIDFound {
|
||||
return nil, false
|
||||
}
|
||||
raw, ok := c.merchants[internalID].Data[key]
|
||||
return raw, ok
|
||||
}
|
||||
|
||||
@ -193,3 +193,37 @@ func TestClient_AliasDelete_Error(t *testing.T) {
|
||||
t.Error("errors not equal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_GetData(t *testing.T) {
|
||||
c, err := datatrans.MakeClient(
|
||||
datatrans.OptionMerchant{
|
||||
InternalID: "", // default
|
||||
MerchantID: "A",
|
||||
Data: map[string]interface{}{
|
||||
"k1": "1",
|
||||
},
|
||||
},
|
||||
datatrans.OptionMerchant{
|
||||
InternalID: "B",
|
||||
MerchantID: "B",
|
||||
Data: map[string]interface{}{
|
||||
"k2": "2",
|
||||
},
|
||||
},
|
||||
)
|
||||
must(t, err)
|
||||
if v, ok := c.GetDataString("k1"); !ok || v != "1" {
|
||||
t.Errorf("wrong value for k1: %#v", v)
|
||||
}
|
||||
if v, ok := c.GetDataInt("k1"); !ok || v != 1 {
|
||||
t.Errorf("wrong value for k1: %#v", v)
|
||||
}
|
||||
|
||||
c2 := c.WithMerchant("B")
|
||||
if v, ok := c2.GetDataString("k2"); !ok || v != "2" {
|
||||
t.Errorf("wrong value for k2: %#v", v)
|
||||
}
|
||||
if v, ok := c2.GetDataInt("k2"); !ok || v != 2 {
|
||||
t.Errorf("wrong value for k2: %#v", v)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user