mirror of
				https://github.com/aclindsa/ofxgo.git
				synced 2025-10-31 09:53:26 -04:00 
			
		
		
		
	This removes the many decodeXXXMessageSet() functions and replaces them with a large map and a single generic decodeMessageSet() function. Also change Responses to satisfy the Message interface as pointer types (instead of the raw types), add the full set of top-level message sets (though most of them still lack any message-parsing ability), adjust the message set names to more closely mirror their OFX names, and fixup tests and the command-line client to match the above changes.
		
			
				
	
	
		
			75 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"flag"
 | |
| 	"fmt"
 | |
| 	"github.com/aclindsa/ofxgo"
 | |
| 	"io"
 | |
| 	"os"
 | |
| )
 | |
| 
 | |
| var ccDownloadCommand = Command{
 | |
| 	Name:        "download-cc",
 | |
| 	Description: "Download a credit card account statement to a file",
 | |
| 	Flags:       flag.NewFlagSet("download-cc", flag.ExitOnError),
 | |
| 	CheckFlags:  ccDownloadCheckFlags,
 | |
| 	Do:          ccDownload,
 | |
| }
 | |
| 
 | |
| func init() {
 | |
| 	defineServerFlags(ccDownloadCommand.Flags)
 | |
| 	ccDownloadCommand.Flags.StringVar(&filename, "filename", "./download.ofx", "The file to save to")
 | |
| 	ccDownloadCommand.Flags.StringVar(&acctId, "acctid", "", "AcctId (from `get-accounts` subcommand)")
 | |
| }
 | |
| 
 | |
| func ccDownloadCheckFlags() bool {
 | |
| 	ret := checkServerFlags()
 | |
| 
 | |
| 	if len(filename) == 0 {
 | |
| 		fmt.Println("Error: Filename empty")
 | |
| 		return false
 | |
| 	}
 | |
| 
 | |
| 	return ret
 | |
| }
 | |
| 
 | |
| func ccDownload() {
 | |
| 	client, query := NewRequest()
 | |
| 
 | |
| 	uid, err := ofxgo.RandomUID()
 | |
| 	if err != nil {
 | |
| 		fmt.Println("Error creating uid for transaction:", err)
 | |
| 		os.Exit(1)
 | |
| 	}
 | |
| 
 | |
| 	statementRequest := ofxgo.CCStatementRequest{
 | |
| 		TrnUID: *uid,
 | |
| 		CCAcctFrom: ofxgo.CCAcct{
 | |
| 			AcctId: ofxgo.String(acctId),
 | |
| 		},
 | |
| 		Include: true,
 | |
| 	}
 | |
| 	query.CreditCard = append(query.CreditCard, &statementRequest)
 | |
| 
 | |
| 	response, err := client.RequestNoParse(query)
 | |
| 
 | |
| 	if err != nil {
 | |
| 		fmt.Println("Error requesting account statement:", err)
 | |
| 		os.Exit(1)
 | |
| 	}
 | |
| 	defer response.Body.Close()
 | |
| 
 | |
| 	file, err := os.Create(filename)
 | |
| 	if err != nil {
 | |
| 		fmt.Println("Error creating file to write to:", err)
 | |
| 		os.Exit(1)
 | |
| 	}
 | |
| 	defer file.Close()
 | |
| 
 | |
| 	_, err = io.Copy(file, response.Body)
 | |
| 	if err != nil {
 | |
| 		fmt.Println("Error writing response to file:", err)
 | |
| 		os.Exit(1)
 | |
| 	}
 | |
| }
 |