Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for N26 Spaces (read-only for now) #10

Merged
merged 3 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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