From 2640bb007707adf827c664abc56dd24b0ef74db4 Mon Sep 17 00:00:00 2001 From: Rauno Date: Wed, 24 Oct 2018 19:48:10 +0200 Subject: [PATCH] align smartcsv output with other formats make api call universal, so CSV can be saved anywhere by the client make CLI print the smart CSV to stdout as with other formats --- api.go | 13 ++----------- cmd/n26/n26.go | 7 +++++-- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/api.go b/api.go index 0b9b716..c41a7cf 100644 --- a/api.go +++ b/api.go @@ -9,7 +9,6 @@ import ( "io/ioutil" "net/http" "net/url" - "os" "golang.org/x/oauth2" ) @@ -371,20 +370,12 @@ func (auth *Client) GetTransactions(from, to TimeStamp, limit string) (*Transact } // Get transactions for the given time window as N26 CSV file. Stored as 'smrt_statement.csv' -func (auth *Client) GetSmartStatementCsv(from, to TimeStamp) error { +func (auth *Client) GetSmartStatementCsv(from, to TimeStamp, reader func (io.Reader) error) error { //Filter is applied only if both values are set if from.IsZero() || to.IsZero() { return errors.New("Start and end time must be set") } - return auth.n26RawRequest(http.MethodGet, fmt.Sprintf("/api/smrt/reports/%v/%v/statements", from.AsMillis(), to.AsMillis()), nil, func(r io.Reader) error { - file, err := os.Create("smrt_statement.csv") - if err != nil { - return err - } - defer file.Close() - _, err = io.Copy(file, r) - return err - }) + return auth.n26RawRequest(http.MethodGet, fmt.Sprintf("/api/smrt/reports/%v/%v/statements", from.AsMillis(), to.AsMillis()), nil, reader) } func (auth *Client) GetStatements(retType string) (string, *Statements) { diff --git a/cmd/n26/n26.go b/cmd/n26/n26.go index b2e8ef7..64baa60 100644 --- a/cmd/n26/n26.go +++ b/cmd/n26/n26.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "os" "sort" "strconv" @@ -256,8 +257,10 @@ func main() { fmt.Println("Start and end time must be set for smart CSV!") return nil } - err = API.GetSmartStatementCsv(from, to) - fmt.Println("Report saved as smrt_statement.csv.") + err = API.GetSmartStatementCsv(from, to, func(r io.Reader) error { + _, err := io.Copy(os.Stdout, r) + return err + }) return } writer, err := getTransactionWriter(c.Args().First())