From 13b5665109a5f53f9d180ceb15a34bc07f73d542 Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 15 Nov 2019 11:32:00 -0500 Subject: [PATCH] Implemented Accounts endpoint on client --- README.md | 20 +++++++++++++++++++- client.go | 25 ++++++++++++++++++++++++- main.go | 17 +---------------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 73560a0..e6ab57c 100644 --- a/README.md +++ b/README.md @@ -1 +1,19 @@ -# investmentally +# Ally Invest API in Go + +This project is intended to be an API wrapper for the Ally Invest API written in Golang + +## Endpoints + +The different endpoints that are currently covered by this API + +### /accounts + +Get all account information for a user. + +## Running This Project + +For now, just build this directory and run the executable. + +### Requirements + +You must within the .env file define your Ally Invest credentials diff --git a/client.go b/client.go index 2f8c3c0..af30e8d 100644 --- a/client.go +++ b/client.go @@ -1,9 +1,11 @@ package main import ( + "encoding/xml" "fmt" "github.com/joho/godotenv" "github.com/mrjones/oauth" + "io/ioutil" "net/http" "os" ) @@ -41,7 +43,28 @@ 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)) return } + +/* The /accounts endpoint of Ally */ +func (c *AllyApi) Accounts() (resp AllyResponse, err error) { + b, err := c.get("accounts") + if err != nil { + return + } + + defer b.Body.Close() + raw, err := ioutil.ReadAll(b.Body) + if err != nil { + return + } + + err = xml.Unmarshal(raw, &resp) + if err != nil { + return + } + + return +} diff --git a/main.go b/main.go index 2fcb7a1..8e15205 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,7 @@ package main import ( - "encoding/xml" "fmt" - "io/ioutil" ) func main() { @@ -11,20 +9,7 @@ func main() { var api AllyApi api.Initialize() - // Set up our HTTP client + acctSummary, _ := api.Accounts() - resp, err := api.Get("accounts") - - defer resp.Body.Close() - b, _ := ioutil.ReadAll(resp.Body) - - var acctSummary AllyResponse - err = xml.Unmarshal(b, &acctSummary) - - if err != nil { - panic(err) - } - - fmt.Printf("%s\n", b) fmt.Printf("%f\n", acctSummary.Accounts[0].AccountHoldingsInfo.TotalSecurities) }