Covered account detail response

This commit is contained in:
Jacob Windle 2019-11-27 08:29:14 -05:00
parent c6eb542a2c
commit e2479704f2
3 changed files with 130 additions and 112 deletions

View File

@ -46,38 +46,41 @@ func (c *AllyApi) Initialize() {
func (c *AllyApi) get(path string) (resp *http.Response, err error) { func (c *AllyApi) get(path string) (resp *http.Response, err error) {
resp, err = c.Client.Get(fmt.Sprintf("%s\\%s", endpoint, path)) resp, err = c.Client.Get(fmt.Sprintf("%s\\%s", endpoint, path))
return return resp, err
}
//GetAndMarshal call GET on the path, and marshal the resopnse to interface
// type
func (c *AllyApi) getAndRead(path string) []byte {
resp, err := c.get(path)
if err != nil {
log.Fatal("Could not make request")
}
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Could not read body!")
return nil
}
return raw
} }
/* The /accounts endpoint of Ally */ /* The /accounts endpoint of Ally */
func (c *AllyApi) Accounts() []AccountSummary { func (c *AllyApi) Accounts() []AccountSummary {
b, err := c.get("accounts")
if err != nil {
}
defer b.Body.Close()
raw, err := ioutil.ReadAll(b.Body)
fmt.Printf("%s\n", raw)
if err != nil {
}
var resp AccountResponse var resp AccountResponse
err = xml.Unmarshal(raw, &resp) _ = xml.Unmarshal(c.getAndRead("accounts"), &resp)
if err != nil {
panic(err)
}
return resp.Accounts.Accountsummary return resp.Accounts.Accountsummary
} }
func (c *AllyApi) AccountBalances() (balances []AccountBalance) { func (c *AllyApi) AccountBalances() (balances []AccountBalance) {
b, err := c.get("accounts/balances")
if err != nil {
log.Fatal("Could not get account balances")
return
}
defer b.Body.Close()
var resp AccountBalanceResponse var resp AccountBalanceResponse
_ = xml.Unmarshal(c.getAndRead("accounts/balances"), &resp)
return resp.AccountBalances
}
func (c *AllyApi) AccountDetail(accountId int) AccountDetailResponse {
var resp AccountDetailResponse
_ = xml.Unmarshal(c.getAndRead(fmt.Sprintf("accounts/%d", accountId)), &resp)
return resp
} }

View File

@ -1,11 +1,16 @@
package main package main
import "fmt" import (
"fmt"
"strconv"
)
func main() { func main() {
// Load our environment variables // Load our environment variables
var api AllyApi var api AllyApi
api.Initialize() api.Initialize()
fmt.Printf("%s\n", api.Accounts()[0].Accountholdings.Displaydata.Totalsecurities) acctId, _ := strconv.Atoi(api.Accounts()[0].Account)
fmt.Printf("AccountDetail: %s\n", api.AccountDetail(acctId).AccountHoldings.Holding[0].Displaydata.Change)
} }

View File

@ -4,12 +4,8 @@ import (
"encoding/xml" "encoding/xml"
) )
type AccountSummary struct { type Accountbalance struct {
XMLName xml.Name `xml:"accountsummary"` XMLName xml.Name `xml:"accountbalance"`
Text string `xml:",chardata"`
Account string `xml:"account"`
Accountbalance struct {
Text string `xml:",chardata"`
Account string `xml:"account"` Account string `xml:"account"`
Accountvalue float64 `xml:"accountvalue"` Accountvalue float64 `xml:"accountvalue"`
Buyingpower struct { Buyingpower struct {
@ -47,9 +43,10 @@ type AccountSummary struct {
Stocks float64 `xml:"stocks"` Stocks float64 `xml:"stocks"`
Total float64 `xml:"total"` Total float64 `xml:"total"`
} `xml:"securities"` } `xml:"securities"`
} `xml:"accountbalance"` }
Accountholdings struct {
Text string `xml:",chardata"` type Accountholdings struct {
XMLName xml.Name `xml:"accountholdings"`
Displaydata struct { Displaydata struct {
Text string `xml:",chardata"` Text string `xml:",chardata"`
Totalsecurities string `xml:"totalsecurities"` Totalsecurities string `xml:"totalsecurities"`
@ -93,7 +90,14 @@ type AccountSummary struct {
Underlying string `xml:"underlying"` Underlying string `xml:"underlying"`
} `xml:"holding"` } `xml:"holding"`
Totalsecurities float64 `xml:"totalsecurities"` Totalsecurities float64 `xml:"totalsecurities"`
} `xml:"accountholdings"` }
type AccountSummary struct {
XMLName xml.Name `xml:"accountsummary"`
Text string `xml:",chardata"`
Account string `xml:"account"`
Accountbalance Accountbalance `xml:"accountbalance"`
Accountholdings Accountholdings `xml:"accountholdings"`
} }
type AccountResponse struct { type AccountResponse struct {
@ -118,3 +122,9 @@ type AccountBalanceResponse struct {
AccountBalances []AccountBalance `xml:"accountbalance"` AccountBalances []AccountBalance `xml:"accountbalance"`
TotalBalance float64 `xml:"totalbalance>accountvalue"` TotalBalance float64 `xml:"totalbalance>accountvalue"`
} }
type AccountDetailResponse struct {
XMLName xml.Name `xml:"response"`
AccountBalance Accountbalance `xml:"accountbalance"`
AccountHoldings Accountholdings `xml:"accountholdings"`
}