Realizing I can use online tools to convert API responses into XML

This commit is contained in:
Jacob Windle 2019-11-15 13:01:19 -05:00
parent 13b5665109
commit d824d3c635
3 changed files with 103 additions and 133 deletions

View File

@ -49,22 +49,21 @@ func (c *AllyApi) get(path string) (resp *http.Response, err error) {
} }
/* The /accounts endpoint of Ally */ /* The /accounts endpoint of Ally */
func (c *AllyApi) Accounts() (resp AllyResponse, err error) { func (c *AllyApi) Accounts() []AccountSummary {
b, err := c.get("accounts") b, err := c.get("accounts")
if err != nil { if err != nil {
return
} }
defer b.Body.Close() defer b.Body.Close()
raw, err := ioutil.ReadAll(b.Body) raw, err := ioutil.ReadAll(b.Body)
fmt.Printf("%s\n", raw)
if err != nil { if err != nil {
return
} }
var resp AccountResponse
err = xml.Unmarshal(raw, &resp) err = xml.Unmarshal(raw, &resp)
if err != nil { if err != nil {
return
} }
return return resp.Accounts.Accountsummary
} }

View File

@ -1,15 +1,11 @@
package main package main
import ( import "fmt"
"fmt"
)
func main() { func main() {
// Load our environment variables // Load our environment variables
var api AllyApi var api AllyApi
api.Initialize() api.Initialize()
acctSummary, _ := api.Accounts() fmt.Printf("%f\n", api.Accounts()[0].Accountbalance.Money.Cash)
fmt.Printf("%f\n", acctSummary.Accounts[0].AccountHoldingsInfo.TotalSecurities)
} }

219
types.go
View File

@ -2,129 +2,104 @@ package main
import "encoding/xml" import "encoding/xml"
/**
Response structure from Ally
*/
type AllyResponse struct {
XMLName xml.Name `xml:"response"`
Error string `xml:"error"`
ResponseId string `xml:"id,attr"`
ElapsedTime int `xml:"elapsedtime"`
Accounts []AccountSummary `xml:"accounts>accountsummary"`
}
/**
TODO: We need to work on AccountHoldings next.
*/
type AccountHoldings struct {
XMLName xml.Name `xml:"accountholdings"`
Holdings []Holding `xml:"holding"`
TotalSecurities float64 `xml:"totalsecurities"`
}
type HoldingDisplayData struct {
XMLName xml.Name `xml:"displaydata"`
}
type InstrumentData struct {
XMLName xml.Name `xml:"instrument"`
Cfi string `xml:"cfi"`
Cusip string `xml:"cusip"`
Desc string `xml:"desc"`
Factor int `xml:"factor"`
Matdt string `xml:"matdt"`
Mult int `xml:"mult"`
PutCall int `xml:"putcall"`
SecType string `xml:"sectyp"`
StrikePrice float64 `xml:"strkpx"`
Symbol string `xml:"sym"`
}
type QuoteData struct {
XMLName xml.Name `xml:"quote"`
Change float64 `xml:"change"`
LastPrice float64 `xml:"lastprice"`
}
type Holding struct {
XMLName xml.Name `xml:"holding"`
AccountType int `xml:"accounttype"`
CostBasis float64 `xml:"costbasis"`
DisplayData HoldingDisplayData `xml:"displaydata"`
GainLoss float64 `xml:"gainloss"`
Instrument InstrumentData `xml:"instrument"`
MarketValue float64 `xml:"marketvalue"`
MarketValueChange float64 `xml:"marketvaluechange"`
Price float64 `xml:"price"`
PurchasePrice float64 `xml:"purchaseprice"`
Quantity int `xml:"qty"`
Quote QuoteData `xml:"quote"`
}
/**
Account summary information
*/
type AccountSummary struct { type AccountSummary struct {
XMLName xml.Name `xml:"accountsummary"` XMLName xml.Name `xml:"accountsummary"`
Account int `xml:"account"` Text string `xml:",chardata"`
AccountName string `xml:"accountname"` Account string `xml:"account"`
AccountBalanceInfo AccountBalance `xml:"accountbalance"` Accountbalance struct {
AccountHoldingsInfo AccountHoldings `xml:"accountholdings"` Text string `xml:",chardata"`
Account string `xml:"account"`
Accountvalue string `xml:"accountvalue"`
Buyingpower struct {
Text string `xml:",chardata"`
Cashavailableforwithdrawal string `xml:"cashavailableforwithdrawal"`
Daytrading string `xml:"daytrading"`
Equitypercentage string `xml:"equitypercentage"`
Options string `xml:"options"`
Soddaytrading string `xml:"soddaytrading"`
Sodoptions string `xml:"sodoptions"`
Sodstock string `xml:"sodstock"`
Stock string `xml:"stock"`
} `xml:"buyingpower"`
Fedcall string `xml:"fedcall"`
Housecall string `xml:"housecall"`
Money struct {
Text string `xml:",chardata"`
Accruedinterest string `xml:"accruedinterest"`
Cash string `xml:"cash"`
Cashavailable string `xml:"cashavailable"`
Marginbalance string `xml:"marginbalance"`
Mmf string `xml:"mmf"`
Total string `xml:"total"`
Uncleareddeposits string `xml:"uncleareddeposits"`
Unsettledfunds string `xml:"unsettledfunds"`
Yield string `xml:"yield"`
} `xml:"money"`
Securities struct {
Text string `xml:",chardata"`
Longoptions string `xml:"longoptions"`
Longstocks string `xml:"longstocks"`
Options string `xml:"options"`
Shortoptions string `xml:"shortoptions"`
Shortstocks string `xml:"shortstocks"`
Stocks string `xml:"stocks"`
Total string `xml:"total"`
} `xml:"securities"`
} `xml:"accountbalance"`
Accountholdings struct {
Text string `xml:",chardata"`
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"`
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 string `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 string `xml:"marketvalue"`
Marketvaluechange string `xml:"marketvaluechange"`
Price string `xml:"price"`
Purchaseprice string `xml:"purchaseprice"`
Qty string `xml:"qty"`
Quote struct {
Text string `xml:",chardata"`
Change string `xml:"change"`
Lastprice string `xml:"lastprice"`
} `xml:"quote"`
Underlying string `xml:"underlying"`
} `xml:"holding"`
Totalsecurities string `xml:"totalsecurities"`
} `xml:"accountholdings"`
} }
/** type AccountResponse struct {
Different securites currently held with an account XMLName xml.Name `xml:"response"`
*/ Text string `xml:",chardata"`
type Securities struct { ID string `xml:"id,attr"`
XMLName xml.Name `xml:"securities"` Accounts struct {
LongOptions float64 `xml:"longoptions"` Text string `xml:",chardata"`
LongStocks float64 `xml:"longstocks"` Accountsummary []AccountSummary `xml:"accountsummary"`
Options float64 `xml:"options"` } `xml:"accounts"`
ShortOptions float64 `xml:"shortoptions"`
ShortStocks float64 `xml:"shortstocks"`
}
/**
Various values representing account balance
*/
type AccountBalance struct {
XMLName xml.Name `xml:"accountbalance"`
Account int `xml:"account"`
AccountValue float64 `xml:"accountvalue"`
BuyingPower BuyingPower `xml:"buyingpower"`
FedCall int `xml:"fedcall"`
HouseCall int `xml:"housecall"`
Money Money `xml:"money"`
Securities Securities `xml:"securities"`
}
/**
Various values representing buying power of this account
*/
type BuyingPower struct {
XMLName xml.Name `xml:"buyingpower"`
CashAvailableForWithdrawal float64 `xml:"cashavailableforwithdrawal"`
DayTrading int `xml:"daytrading"`
EquityPercentage int `xml:"equitypercentage"`
Options int `xml:"options"`
SodDayTrading int `xml:"soddaytrading"`
SodOptions int `xml:"sodoptions"`
SodStock int `xml:"sodstock"`
Stock int `xml:"stock"`
}
/**
Various values representing money in this account
*/
type Money struct {
XMLName xml.Name `xml:"money"`
AccruedInterest float64 `xml:"accruedinterest"`
Cash float64 `xml:"cash"`
CashAvailable float64 `xml:"cashavailable"`
MarginBalance float64 `xml:"marginbalance"`
Mmf float64 `xml:"mnf"`
Total float64 `xml:"total"`
UnclearedDeposits float64 `xml:"uncleareddeposits"`
UnsettledFunds float64 `xml:"unsettledfunds"`
Yield float64 `xml:"yield"`
} }