From b8167173625e699af669913d0b1466ecc9a8986a Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Sun, 19 Jan 2020 19:35:36 -0500 Subject: [PATCH] Wizardry with unmarshal and xml --- client.go | 46 ++++++++++++++++++++++++++++++---------------- main.go | 2 +- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/client.go b/client.go index 531a17b..6b5d419 100644 --- a/client.go +++ b/client.go @@ -79,26 +79,40 @@ func (c *AllyApi) AccountBalances() (balances []AccountBalance) { return resp.AccountBalances } -func (c *AllyApi) AccountDetail(accountId string) AccountDetailResponse { - var resp AccountDetailResponse - _ = xml.Unmarshal(c.getAndRead(fmt.Sprintf("accounts/%s", accountId)), &resp) - return resp +/** +Return an object representing account detail of a given string +*/ +func (c *AllyApi) AccountDetail(accountId string) (resp AccountDetailResponse) { + c.marshalInterfaceResponse(fmt.Sprintf("accounts/%s", accountId), &resp) + return } -func (c *AllyApi) AccountBalance(accountId string) AccountDetailBalanceResponse { - var resp AccountDetailBalanceResponse - _ = xml.Unmarshal(c.getAndRead(fmt.Sprintf("accounts/%s/balances", accountId)), &resp) - return resp +/** +Return an object representing account balances of a given string ID +*/ +func (c *AllyApi) AccountBalance(accountId string) (resp AccountDetailBalanceResponse) { + c.marshalInterfaceResponse(fmt.Sprintf("accounts/%s/balances", accountId), &resp) + return } -func (c *AllyApi) AccountHoldings(accountId string) AccountDetailHoldingsResponse { - var resp AccountDetailHoldingsResponse - _ = xml.Unmarshal(c.getAndRead(fmt.Sprintf("accounts/%s/holdings", accountId)), &resp) - return resp +/** +Return an object representing the account holdings of a given string. +*/ +func (c *AllyApi) AccountHoldings(accountId string) (resp AccountDetailHoldingsResponse) { + c.marshalInterfaceResponse(fmt.Sprintf("accounts/%s/holdings", accountId), &resp) + return } -func (c *AllyApi) MarketClock() MarketClockResponse { - var resp MarketClockResponse - _ = xml.Unmarshal(c.getAndRead("market/clock"), &resp) - return resp +/** +Return an object representing the market clock response. +*/ +func (c *AllyApi) MarketClock() (resp MarketClockResponse) { + c.marshalInterfaceResponse("market/clock", &resp) + return +} + +// Why does this work?? wtf?? +func (c *AllyApi) marshalInterfaceResponse(p string, i interface{}) { + resp := c.getAndRead(p) + _ = xml.Unmarshal(resp, &i) } diff --git a/main.go b/main.go index 97ba7ac..f00f976 100644 --- a/main.go +++ b/main.go @@ -9,5 +9,5 @@ func main() { var api AllyApi api.Initialize() - fmt.Printf("Market Clock: %d\n", api.MarketClock().UnixTime) + fmt.Printf("Market Clock: %d\n", api.MarketClock().CurrentStatus) }