Skip to content

Commit

Permalink
Merge pull request #10 from guitmz/add-spaces
Browse files Browse the repository at this point in the history
Add initial support for N26 Spaces (read-only for now)
  • Loading branch information
guitmz authored Aug 29, 2018
2 parents 2a6dc4b + 117103c commit 88e278a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
32 changes: 32 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/n26/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/n26/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"

"github.com/guitmz/n26"
)

Expand Down
28 changes: 27 additions & 1 deletion cmd/n26/n26.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
appVersion = "1.4.2"
appVersion = "1.4.3"
)

func check(e error) {
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion cmd/n26/table.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/olekukonko/tablewriter"
"os"

"github.com/olekukonko/tablewriter"
)

type tblWriter struct {
Expand All @@ -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)
Expand Down

0 comments on commit 88e278a

Please sign in to comment.