Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change response arguments to []byte #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type (

/********** METHODS **********/

func (c *Client) do(method, url, data string, queryParams []string) (map[string]interface{}, error) {
func (c *Client) do(method, url, data string, queryParams []string) ([]byte, error) {
var body []byte
var err error

Expand All @@ -114,7 +114,7 @@ func (c *Client) do(method, url, data string, queryParams []string) (map[string]
body, err = c.request.Delete(url)
}

return readStream(body), err
return body, err
}

/********** CLIENT **********/
Expand Down Expand Up @@ -151,7 +151,7 @@ func New(clientID, clientSecret, fingerprint, ipAddress string, modes ...bool) *
/********** AUTHENTICATION **********/

// GetPublicKey returns a public key as a token representing client credentials
func (c *Client) GetPublicKey(scope ...string) (map[string]interface{}, error) {
func (c *Client) GetPublicKey(scope ...string) ([]byte, error) {
log.info("========== GET PUBLIC KEY ==========")
url := buildURL(path["client"])
defaultScope := "OAUTH|POST,USERS|POST,USERS|GET,USER|GET,USER|PATCH,SUBSCRIPTIONS|GET,SUBSCRIPTIONS|POST,SUBSCRIPTION|GET,SUBSCRIPTION|PATCH,CLIENT|REPORTS,CLIENT|CONTROLS"
Expand All @@ -169,15 +169,15 @@ func (c *Client) GetPublicKey(scope ...string) (map[string]interface{}, error) {
/********** NODE **********/

// GetNodes returns all of the nodes
func (c *Client) GetNodes(queryParams ...string) (map[string]interface{}, error) {
func (c *Client) GetNodes(queryParams ...string) ([]byte, error) {
log.info("========== GET CLIENT NODES ==========")
url := buildURL(path["nodes"])

return c.do("GET", url, "", queryParams)
}

// GetTradeMarketData returns data on a stock based on its ticker symbol
func (c *Client) GetTradeMarketData(tickerSymbol string) (map[string]interface{}, error) {
func (c *Client) GetTradeMarketData(tickerSymbol string) ([]byte, error) {
log.info("========== GET TRADE MARKET DATA ==========")
url := buildURL(path["nodes"], "trade-market-watch")

Expand All @@ -189,47 +189,47 @@ func (c *Client) GetTradeMarketData(tickerSymbol string) (map[string]interface{}
/********** OTHER **********/

// GetCryptoMarketData returns market data for cryptocurrencies
func (c *Client) GetCryptoMarketData() (map[string]interface{}, error) {
func (c *Client) GetCryptoMarketData() ([]byte, error) {
log.info("========== GET CRYPTO MARKET DATA ==========")
url := buildURL(path["nodes"], "crypto-market-watch")

return c.do("GET", url, "", nil)
}

// GetCryptoQuotes returns all of the quotes for crypto currencies
func (c *Client) GetCryptoQuotes(queryParams ...string) (map[string]interface{}, error) {
func (c *Client) GetCryptoQuotes(queryParams ...string) ([]byte, error) {
log.info("========== GET CRYPTO QUOTES ==========")
url := buildURL(path["nodes"], "crypto-quotes")

return c.do("GET", url, "", queryParams)
}

// GetInstitutions returns a list of all available banking institutions
func (c *Client) GetInstitutions() (map[string]interface{}, error) {
func (c *Client) GetInstitutions() ([]byte, error) {
log.info("========== GET INSTITUTIONS ==========")
url := buildURL(path["institutions"])

return c.do("GET", url, "", nil)
}

// LocateATMs returns a list of nearby ATMs
func (c *Client) LocateATMs(queryParams ...string) (map[string]interface{}, error) {
func (c *Client) LocateATMs(queryParams ...string) ([]byte, error) {
log.info("========== LOCATE ATMS ==========")
url := buildURL(path["nodes"], "atms")

return c.do("GET", url, "", queryParams)
}

// VerifyAddress checks if an address if valid
func (c *Client) VerifyAddress(data string) (map[string]interface{}, error) {
func (c *Client) VerifyAddress(data string) ([]byte, error) {
log.info("========== VERIFY ADDRESS ==========")
url := buildURL("address-verification")

return c.do("POST", url, data, nil)
}

// VerifyRoutingNumber checks and returns the bank details of a routing number
func (c *Client) VerifyRoutingNumber(data string) (map[string]interface{}, error) {
func (c *Client) VerifyRoutingNumber(data string) ([]byte, error) {
log.info("========== VERIFY ROUTING NUMBER ==========")
url := buildURL("routing-number-verification")

Expand All @@ -239,39 +239,39 @@ func (c *Client) VerifyRoutingNumber(data string) (map[string]interface{}, error
/********** SUBSCRIPTION **********/

// GetSubscriptions returns all of the nodes associated with a user
func (c *Client) GetSubscriptions(queryParams ...string) (map[string]interface{}, error) {
func (c *Client) GetSubscriptions(queryParams ...string) ([]byte, error) {
log.info("========== GET SUBSCRIPTIONS ==========")
url := buildURL(path["subscriptions"])

return c.do("GET", url, "", queryParams)
}

// GetSubscription returns a single subscription
func (c *Client) GetSubscription(subscriptionID string) (map[string]interface{}, error) {
func (c *Client) GetSubscription(subscriptionID string) ([]byte, error) {
log.info("========== GET SUBSCRIPTION ==========")
url := buildURL(path["subscriptions"], subscriptionID)

return c.do("GET", url, "", nil)
}

// CreateSubscription creates a subscription and returns the subscription data
func (c *Client) CreateSubscription(data string, idempotencyKey ...string) (map[string]interface{}, error) {
func (c *Client) CreateSubscription(data string, idempotencyKey ...string) ([]byte, error) {
log.info("========== CREATE SUBSCRIPTION ==========")
url := buildURL(path["subscriptions"])

return c.do("POST", url, data, idempotencyKey)
}

// UpdateSubscription updates an existing subscription
func (c *Client) UpdateSubscription(subscriptionID string, data string) (map[string]interface{}, error) {
func (c *Client) UpdateSubscription(subscriptionID string, data string) ([]byte, error) {
log.info("========== UPDATE SUBSCRIPTION ==========")
url := buildURL(path["subscriptions"], subscriptionID)

return c.do("PATCH", url, data, nil)
}

// GetWebhookLogs returns all of the webhooks sent to a specific client
func (c *Client) GetWebhookLogs() (map[string]interface{}, error) {
func (c *Client) GetWebhookLogs() ([]byte, error) {
log.info("========== GET WEBHOOK LOGS ==========")
url := buildURL(path["subscriptions"], "logs")

Expand All @@ -281,7 +281,7 @@ func (c *Client) GetWebhookLogs() (map[string]interface{}, error) {
/********** TRANSACTION **********/

// GetTransactions returns all client transactions
func (c *Client) GetTransactions(queryParams ...string) (map[string]interface{}, error) {
func (c *Client) GetTransactions(queryParams ...string) ([]byte, error) {
log.info("========== GET CLIENT TRANSACTIONS ==========")
url := buildURL(path["transactions"])

Expand All @@ -291,7 +291,7 @@ func (c *Client) GetTransactions(queryParams ...string) (map[string]interface{},
/********** USER **********/

// GetUsers returns a list of users
func (c *Client) GetUsers(queryParams ...string) (map[string]interface{}, error) {
func (c *Client) GetUsers(queryParams ...string) ([]byte, error) {
log.info("========== GET CLIENT USERS ==========")
url := buildURL(path["users"])

Expand All @@ -303,10 +303,15 @@ func (c *Client) GetUser(userID, fingerprint, ipAddress string, queryParams ...s
log.info("========== GET USER ==========")
url := buildURL(path["users"], userID)
res, err := c.do("GET", url, "", queryParams)
response, resErr := readStream(res)

if resErr != nil {
return nil, resErr
}

var user User
mapstructure.Decode(res, &user)
user.Response = res
mapstructure.Decode(response, &user)
user.Response = response
request := Request{
clientID: c.ClientID,
clientSecret: c.ClientSecret,
Expand All @@ -331,8 +336,14 @@ func (c *Client) CreateUser(data, fingerprint, ipAddress string, idempotencyKey

url := buildURL(path["users"])
res, err := user.do("POST", url, data, idempotencyKey)
mapstructure.Decode(res, &user)
user.Response = res
response, resErr := readStream(res)

if resErr != nil {
return nil, resErr
}

mapstructure.Decode(response, &user)
user.Response = response

return &user, err
}
32 changes: 16 additions & 16 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Test_GetPublicKey(t *testing.T) {
testRes, err := testClient.GetPublicKey()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

/********** NODE **********/
Expand All @@ -77,7 +77,7 @@ func Test_GetClientNodes(t *testing.T) {
testRes, err := testClient.GetNodes()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_GetTradeMarketData(t *testing.T) {
Expand All @@ -88,7 +88,7 @@ func Test_GetTradeMarketData(t *testing.T) {
testRes, err := testClient.GetTradeMarketData("")

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

/********** OTHER **********/
Expand All @@ -101,7 +101,7 @@ func Test_GetCryptoMarketData(t *testing.T) {
testRes, err := testClient.GetCryptoMarketData()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_GetCryptoQuotes(t *testing.T) {
Expand All @@ -112,7 +112,7 @@ func Test_GetCryptoQuotes(t *testing.T) {
testRes, err := testClient.GetCryptoQuotes()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_GetInstitutions(t *testing.T) {
Expand All @@ -123,7 +123,7 @@ func Test_GetInstitutions(t *testing.T) {
testRes, err := testClient.GetInstitutions()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_LocateATMs(t *testing.T) {
Expand All @@ -134,7 +134,7 @@ func Test_LocateATMs(t *testing.T) {
testRes, err := testClient.LocateATMs()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_VerifyAddress(t *testing.T) {
Expand All @@ -144,7 +144,7 @@ func Test_VerifyAddress(t *testing.T) {
testRes, err := testClient.VerifyAddress("")

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_VerifyRoutingNumber(t *testing.T) {
Expand All @@ -154,7 +154,7 @@ func Test_VerifyRoutingNumber(t *testing.T) {
testRes, err := testClient.VerifyRoutingNumber("")

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

/********** SUBSCRIPTION **********/
Expand All @@ -167,7 +167,7 @@ func Test_GetSubscriptions(t *testing.T) {
testRes, err := testClient.GetSubscriptions()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_GetSubscription(t *testing.T) {
Expand All @@ -178,7 +178,7 @@ func Test_GetSubscription(t *testing.T) {
testRes, err := testClient.GetSubscription("")

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_CreateSubscription(t *testing.T) {
Expand All @@ -189,7 +189,7 @@ func Test_CreateSubscription(t *testing.T) {
testRes, err := testClient.CreateSubscription("")

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_UpdateSubscription(t *testing.T) {
Expand All @@ -200,7 +200,7 @@ func Test_UpdateSubscription(t *testing.T) {
testRes, err := testClient.CreateSubscription("")

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_GetWebhookLogs(t *testing.T) {
Expand All @@ -211,7 +211,7 @@ func Test_GetWebhookLogs(t *testing.T) {
testRes, err := testClient.GetWebhookLogs()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

/********** TRANSACTION **********/
Expand All @@ -223,7 +223,7 @@ func Test_GetClientTransactions(t *testing.T) {
testRes, err := testClient.GetTransactions()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

/********** USER **********/
Expand All @@ -236,7 +236,7 @@ func Test_GetUsers(t *testing.T) {
testRes, err := testClient.GetUsers()

assert.NoError(err)
assert.Equal(testRes, mockClientResponse)
assert.NotNil(testRes)
}

func Test_GetUser(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ func handleAPIError(errorCode, httpCode, message string) error {
}

func handleHTTPError(d []byte) error {
data := readStream(d)
data, err := readStream(d)

if err != nil {
return err
}

errCode := data["error_code"].(string)
httpCode := data["http_code"].(string)
Expand Down
2 changes: 1 addition & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Test_HandleHTTPError(t *testing.T) {
httpCode := errData["http_code"].(string)
errCode := errData["error_code"].(string)
msg := errData["error"].(map[string]interface{})["en"].(string)
responseMsg := "HTTP_CODE " + httpCode + " ERROR_CODE " + errCode + "\n" + msg
responseMsg := "http_code " + httpCode + " error_code " + errCode + " " + msg

// error message should be an error and print error code plus original API message
assert.EqualError(testErr, responseMsg)
Expand Down
6 changes: 3 additions & 3 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"encoding/json"
)

func readStream(data []byte) map[string]interface{} {
func readStream(data []byte) (map[string]interface{}, error) {
d := make(map[string]interface{})
err := json.Unmarshal(data, &d)

// if data is an empty stream this will cause an unmarshal error
if err != nil {
panic(err)
return nil, err
}

return d
return d, nil
}
6 changes: 5 additions & 1 deletion read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func Test_Read(t *testing.T) {
panic(err)
}

testRes := readStream(td)
testRes, testResErr := readStream(td)

if testResErr != nil {
panic(testResErr)
}

assert.Equal(t, testData, testRes)
}
Loading