diff --git a/api.go b/api.go index 253baf6..bb90bed 100644 --- a/api.go +++ b/api.go @@ -187,6 +187,27 @@ type Statements []struct { Year int `json:"year"` } +type Spaces struct { + Spaces []struct { + Balance struct { + AvailableBalance float64 `json:"availableBalance"` + OverdraftAmount interface{} `json:"overdraftAmount"` + } `json:"balance"` + Color string `json:"color"` + Goal interface{} `json:"goal"` + ID string `json:"id"` + ImageURL string `json:"imageUrl"` + IsCardAttached bool `json:"isCardAttached"` + IsPrimary bool `json:"isPrimary"` + Name string `json:"name"` + } `json:"spaces"` + TotalBalance float64 `json:"totalBalance"` + UserFeatures struct { + AvailableSpaces int `json:"availableSpaces"` + CanUpgrade bool `json:"canUpgrade"` + } `json:"userFeatures"` +} + type Client http.Client func NewClient(a Auth) (*Client, error) { @@ -365,6 +386,17 @@ func (auth *Client) UnblockCard(ID string) { fmt.Printf("\nYour card with ID: %s is ACTIVE\n\n", ID) } +func (auth *Client) GetSpaces(retType string) (string, *Spaces) { + body := auth.n26Request(http.MethodGet, "/api/spaces", nil) + spaces := &Spaces{} + check(json.Unmarshal(body, &spaces)) + identedJSON, _ := json.MarshalIndent(&spaces, "", " ") + if retType == "json" { + return string(identedJSON), spaces + } + return "", spaces +} + func check(e error) { if e != nil { panic(e) diff --git a/cmd/n26/csv.go b/cmd/n26/csv.go index fc2c74d..3bcf1b6 100644 --- a/cmd/n26/csv.go +++ b/cmd/n26/csv.go @@ -13,6 +13,7 @@ func NewCsvWriter(target io.Writer) (*csvWriter, error) { writer := csv.NewWriter(target) return &csvWriter{writer}, nil } + func (w *csvWriter) WriteData(header []string, data [][]string) error { if err := w.Write(header); err != nil { return err diff --git a/cmd/n26/json.go b/cmd/n26/json.go index beee4d6..672bd7c 100644 --- a/cmd/n26/json.go +++ b/cmd/n26/json.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "github.com/guitmz/n26" ) diff --git a/cmd/n26/n26.go b/cmd/n26/n26.go index bd3a70a..4cbb0fe 100644 --- a/cmd/n26/n26.go +++ b/cmd/n26/n26.go @@ -15,7 +15,7 @@ import ( ) const ( - appVersion = "1.4.2" + appVersion = "1.4.3" ) func check(e error) { @@ -314,6 +314,32 @@ func main() { return nil }, }, + { + Name: "spaces", + Usage: "your spaces", + Action: func(c *cli.Context) error { + API, err := authentication() + check(err) + prettyJSON, spaces := API.GetSpaces(c.Args().First()) + if prettyJSON != "" { + fmt.Println(prettyJSON) + } else { + data := [][]string{} + for _, space := range spaces.Spaces { + data = append(data, + []string{ + space.Name, + strconv.FormatFloat(space.Balance.AvailableBalance, 'f', -1, 64), + }, + ) + } + fmt.Printf("\nYour total balance is: %s\n", strconv.FormatFloat(spaces.TotalBalance, 'f', -1, 64)) + fmt.Printf("You still have %d available spaces to create and use\n\n", spaces.UserFeatures.AvailableSpaces) + NewTableWriter().WriteData([]string{"Name", "Balance"}, data) + } + return nil + }, + }, } sort.Sort(cli.CommandsByName(app.Commands)) diff --git a/cmd/n26/table.go b/cmd/n26/table.go index 532688d..44e6dc0 100644 --- a/cmd/n26/table.go +++ b/cmd/n26/table.go @@ -1,8 +1,9 @@ package main import ( - "github.com/olekukonko/tablewriter" "os" + + "github.com/olekukonko/tablewriter" ) type tblWriter struct { @@ -12,6 +13,7 @@ type tblWriter struct { func NewTableWriter() *tblWriter { return &tblWriter{tablewriter.NewWriter(os.Stdout)} } + func (table *tblWriter) WriteData(header []string, data [][]string) error { table.SetHeader(header) table.AppendBulk(data)