From 06de7e2af64b7c455606324f0c6d2ed7a3d8054f Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sat, 18 Nov 2017 19:43:41 -0500 Subject: [PATCH] Add example usage to README --- README.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/README.md b/README.md index 1451444..948eadd 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,62 @@ repository. Documentation can be found with the `go doc` tool, or at https://godoc.org/github.com/aclindsa/ofxgo +## Example Usage + +The following code snippet demonstrates how to use OFXGo to query and parse +OFX code from a checking account, printing the balance and returned transactions: + +```go +client := ofxgo.Client{} # Accept the default Client settings + +# These values are specific to your bank +var query ofxgo.Request +query.URL = "https://secu.example.com/ofx" +query.Signon.Org = ofxgo.String("SECU") +query.Signon.Fid = ofxgo.String("1234") + +# Set your username/password +query.Signon.UserID = ofxgo.String("username") +query.Signon.UserPass = ofxgo.String("hunter2") + +uid, _ := ofxgo.RandomUID() # Handle error in real code +query.Bank = append(query.bank, &ofxgo.StatementRequest{ + TrnUID: *uid, + BankAcctFrom: ofxgo.BankAcct{ + BankID: ofxgo.String("123456789"), # Possibly your routing number + AcctID: ofxgo.String("00011122233"), # Possibly your account number + AcctType: ofxgo.AcctTypeChecking, + }, + Include: true, # Include transactions (instead of only balance information) +}) + +response, _ := client.Request(query) # Handle error in real code + +# Was there an OFX error while processing our request? +if response.Signon.Status.Code != 0 { + meaning, _ := response.Signon.Status.CodeMeaning() + fmt.Printf("Nonzero signon status (%d: %s) with message: %s\n", response.Signon.Status.Code, meaning, response.Signon.Status.Message) + os.Exit(1) +} + +if len(response.Bank) < 1 { + fmt.Println("No banking messages received") + os.Exit(1) +} + +if stmt, ok := response.Bank[0].(*ofxgo.StatementResponse); ok { + fmt.Printf("Balance: %s %s (as of %s)\n", stmt.BalAmt, stmt.CurDef, stmt.DtAsOf) + fmt.Println("Transactions:") + for _, tran := range stmt.BankTranList.Transactions { + currency := defCurrency + if ok, _ := tran.Currency.Valid(); ok { + currency = tran.Currency.CurSym + } + fmt.Printf("%s %-15s %-11s %s%s%s\n", tran.DtPosted, tran.TrnAmt.String()+" "+currency.String(), tran.TrnType, tran.Name, tran.Payee.Name, tran.Memo) + } +} +``` + ## Requirements OFXGo requires go >= 1.9