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) {
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 */
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
err = xml.Unmarshal(raw, &resp)
if err != nil {
panic(err)
}
_ = xml.Unmarshal(c.getAndRead("accounts"), &resp)
return resp.Accounts.Accountsummary
}
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
_ = 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
import "fmt"
import (
"fmt"
"strconv"
)
func main() {
// Load our environment variables
var api AllyApi
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)
}

182
types.go
View File

@ -4,96 +4,100 @@ import (
"encoding/xml"
)
type AccountSummary struct {
XMLName xml.Name `xml:"accountsummary"`
Text string `xml:",chardata"`
Account string `xml:"account"`
Accountbalance struct {
type Accountbalance struct {
XMLName xml.Name `xml:"accountbalance"`
Account string `xml:"account"`
Accountvalue float64 `xml:"accountvalue"`
Buyingpower struct {
Text string `xml:",chardata"`
Cashavailableforwithdrawal float64 `xml:"cashavailableforwithdrawal"`
Daytrading float64 `xml:"daytrading"`
Equitypercentage float64 `xml:"equitypercentage"`
Options float64 `xml:"options"`
Soddaytrading float64 `xml:"soddaytrading"`
Sodoptions float64 `xml:"sodoptions"`
Sodstock float64 `xml:"sodstock"`
Stock float64 `xml:"stock"`
} `xml:"buyingpower"`
Fedcall float64 `xml:"fedcall"`
Housecall float64 `xml:"housecall"`
Money struct {
Text string `xml:",chardata"`
Accruedinterest float64 `xml:"accruedinterest"`
Cash float64 `xml:"cash"`
Cashavailable float64 `xml:"cashavailable"`
Marginbalance float64 `xml:"marginbalance"`
Mmf float64 `xml:"mmf"`
Total float64 `xml:"total"`
Uncleareddeposits float64 `xml:"uncleareddeposits"`
Unsettledfunds float64 `xml:"unsettledfunds"`
Yield float64 `xml:"yield"`
} `xml:"money"`
Securities struct {
Text string `xml:",chardata"`
Account string `xml:"account"`
Accountvalue float64 `xml:"accountvalue"`
Buyingpower struct {
Text string `xml:",chardata"`
Cashavailableforwithdrawal float64 `xml:"cashavailableforwithdrawal"`
Daytrading float64 `xml:"daytrading"`
Equitypercentage float64 `xml:"equitypercentage"`
Options float64 `xml:"options"`
Soddaytrading float64 `xml:"soddaytrading"`
Sodoptions float64 `xml:"sodoptions"`
Sodstock float64 `xml:"sodstock"`
Stock float64 `xml:"stock"`
} `xml:"buyingpower"`
Fedcall float64 `xml:"fedcall"`
Housecall float64 `xml:"housecall"`
Money struct {
Text string `xml:",chardata"`
Accruedinterest float64 `xml:"accruedinterest"`
Cash float64 `xml:"cash"`
Cashavailable float64 `xml:"cashavailable"`
Marginbalance float64 `xml:"marginbalance"`
Mmf float64 `xml:"mmf"`
Total float64 `xml:"total"`
Uncleareddeposits float64 `xml:"uncleareddeposits"`
Unsettledfunds float64 `xml:"unsettledfunds"`
Yield float64 `xml:"yield"`
} `xml:"money"`
Securities struct {
Text string `xml:",chardata"`
Longoptions float64 `xml:"longoptions"`
Longstocks float64 `xml:"longstocks"`
Options float64 `xml:"options"`
Shortoptions float64 `xml:"shortoptions"`
Shortstocks float64 `xml:"shortstocks"`
Stocks float64 `xml:"stocks"`
Total float64 `xml:"total"`
} `xml:"securities"`
} `xml:"accountbalance"`
Accountholdings struct {
Longoptions float64 `xml:"longoptions"`
Longstocks float64 `xml:"longstocks"`
Options float64 `xml:"options"`
Shortoptions float64 `xml:"shortoptions"`
Shortstocks float64 `xml:"shortstocks"`
Stocks float64 `xml:"stocks"`
Total float64 `xml:"total"`
} `xml:"securities"`
}
type Accountholdings struct {
XMLName xml.Name `xml:"accountholdings"`
Displaydata struct {
Text string `xml:",chardata"`
Totalsecurities string `xml:"totalsecurities"`
} `xml:"displaydata"`
Holding []struct {
Text string `xml:",chardata"`
Accounttype string `xml:"accounttype"`
Costbasis string `xml:"costbasis"`
Displaydata struct {
Text string `xml:",chardata"`
Totalsecurities string `xml:"totalsecurities"`
Text string `xml:",chardata"`
Accounttype string `xml:"accounttype"`
Assetclass string `xml:"assetclass"`
Change string `xml:"change"`
Costbasis string `xml:"costbasis"`
Desc string `xml:"desc"`
Lastprice string `xml:"lastprice"`
Marketvalue string `xml:"marketvalue"`
Marketvaluechange string `xml:"marketvaluechange"`
Qty string `xml:"qty"`
Symbol string `xml:"symbol"`
} `xml:"displaydata"`
Holding []struct {
Text string `xml:",chardata"`
Accounttype string `xml:"accounttype"`
Costbasis string `xml:"costbasis"`
Displaydata struct {
Text string `xml:",chardata"`
Accounttype string `xml:"accounttype"`
Assetclass string `xml:"assetclass"`
Change string `xml:"change"`
Costbasis string `xml:"costbasis"`
Desc string `xml:"desc"`
Lastprice string `xml:"lastprice"`
Marketvalue string `xml:"marketvalue"`
Marketvaluechange string `xml:"marketvaluechange"`
Qty string `xml:"qty"`
Symbol string `xml:"symbol"`
} `xml:"displaydata"`
Gainloss float64 `xml:"gainloss"`
Instrument struct {
Text string `xml:",chardata"`
Cusip string `xml:"cusip"`
Desc string `xml:"desc"`
Factor string `xml:"factor"`
Sectyp string `xml:"sectyp"`
Sym string `xml:"sym"`
} `xml:"instrument"`
Marketvalue float64 `xml:"marketvalue"`
Marketvaluechange float64 `xml:"marketvaluechange"`
Price float64 `xml:"price"`
Purchaseprice float64 `xml:"purchaseprice"`
Qty float64 `xml:"qty"`
Quote struct {
Text string `xml:",chardata"`
Change float64 `xml:"change"`
Lastprice float64 `xml:"lastprice"`
} `xml:"quote"`
Underlying string `xml:"underlying"`
} `xml:"holding"`
Totalsecurities float64 `xml:"totalsecurities"`
} `xml:"accountholdings"`
Gainloss float64 `xml:"gainloss"`
Instrument struct {
Text string `xml:",chardata"`
Cusip string `xml:"cusip"`
Desc string `xml:"desc"`
Factor string `xml:"factor"`
Sectyp string `xml:"sectyp"`
Sym string `xml:"sym"`
} `xml:"instrument"`
Marketvalue float64 `xml:"marketvalue"`
Marketvaluechange float64 `xml:"marketvaluechange"`
Price float64 `xml:"price"`
Purchaseprice float64 `xml:"purchaseprice"`
Qty float64 `xml:"qty"`
Quote struct {
Text string `xml:",chardata"`
Change float64 `xml:"change"`
Lastprice float64 `xml:"lastprice"`
} `xml:"quote"`
Underlying string `xml:"underlying"`
} `xml:"holding"`
Totalsecurities float64 `xml:"totalsecurities"`
}
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 {
@ -118,3 +122,9 @@ type AccountBalanceResponse struct {
AccountBalances []AccountBalance `xml:"accountbalance"`
TotalBalance float64 `xml:"totalbalance>accountvalue"`
}
type AccountDetailResponse struct {
XMLName xml.Name `xml:"response"`
AccountBalance Accountbalance `xml:"accountbalance"`
AccountHoldings Accountholdings `xml:"accountholdings"`
}