Skip to content

Commit

Permalink
Merge pull request #1 from Rots/master
Browse files Browse the repository at this point in the history
enable the use of API as an external package
  • Loading branch information
guitmz authored Apr 16, 2018
2 parents c0a5e5c + 3f23f7f commit 325737c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# n26
CLI to get information of your N26 account
Go API and CLI to get information of your N26 account

# Installation
- macOS: Available via Homebrew. Just run `brew install guitmz/n26/n26`
- Linux: You can manually build this project or download a binary release.

You can also install with `go get -u github.com/guitmz/n26` (make sure you have your Go env setup correctly)
You can also install with `go get -u github.com/guitmz/n26/cmd/n26` (make sure you have your Go env setup correctly)

# Usage
```
Expand Down
28 changes: 17 additions & 11 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package n26

import (
"encoding/json"
Expand Down Expand Up @@ -224,7 +224,7 @@ func (auth Auth) n26Request(endpoint string) []byte {
return body
}

func (auth Auth) getBalance(retType string) (string, *Balance) {
func (auth Auth) GetBalance(retType string) (string, *Balance) {
body := auth.n26Request("/api/accounts")
balance := &Balance{}
check(json.Unmarshal(body, &balance))
Expand All @@ -235,7 +235,7 @@ func (auth Auth) getBalance(retType string) (string, *Balance) {
return "", balance
}

func (auth Auth) getInfo(retType string) (string, *PersonalInfo) {
func (auth Auth) GetInfo(retType string) (string, *PersonalInfo) {
body := auth.n26Request("/api/me")
info := &PersonalInfo{}
check(json.Unmarshal(body, &info))
Expand All @@ -246,7 +246,7 @@ func (auth Auth) getInfo(retType string) (string, *PersonalInfo) {
return "", info
}

func (auth Auth) getStatus(retType string) (string, *Statuses) {
func (auth Auth) GetStatus(retType string) (string, *Statuses) {
body := auth.n26Request("/api/me/statuses")
status := &Statuses{}
check(json.Unmarshal(body, &status))
Expand All @@ -257,7 +257,7 @@ func (auth Auth) getStatus(retType string) (string, *Statuses) {
return "", status
}

func (auth Auth) getAddresses(retType string) (string, *Addresses) {
func (auth Auth) GetAddresses(retType string) (string, *Addresses) {
body := auth.n26Request("/api/addresses")
addresses := &Addresses{}
check(json.Unmarshal(body, &addresses))
Expand All @@ -268,7 +268,7 @@ func (auth Auth) getAddresses(retType string) (string, *Addresses) {
return "", addresses
}

func (auth Auth) getCards(retType string) (string, *Cards) {
func (auth Auth) GetCards(retType string) (string, *Cards) {
body := auth.n26Request("/api/v2/cards")
cards := &Cards{}
check(json.Unmarshal(body, &cards))
Expand All @@ -279,7 +279,7 @@ func (auth Auth) getCards(retType string) (string, *Cards) {
return "", cards
}

func (auth Auth) getLimits(retType string) (string, *Limits) {
func (auth Auth) GetLimits(retType string) (string, *Limits) {
body := auth.n26Request("/api/settings/account/limits")
limits := &Limits{}
check(json.Unmarshal(body, &limits))
Expand All @@ -290,7 +290,7 @@ func (auth Auth) getLimits(retType string) (string, *Limits) {
return "", limits
}

func (auth Auth) getContacts(retType string) (string, *Contacts) {
func (auth Auth) GetContacts(retType string) (string, *Contacts) {
body := auth.n26Request("/api/smrt/contacts")
contacts := &Contacts{}
check(json.Unmarshal(body, &contacts))
Expand All @@ -301,7 +301,7 @@ func (auth Auth) getContacts(retType string) (string, *Contacts) {
return "", contacts
}

func (auth Auth) getTransactions(retType string) (string, *Transactions) {
func (auth Auth) GetTransactions(retType string) (string, *Transactions) {
body := auth.n26Request("/api/smrt/transactions")
transactions := &Transactions{}
check(json.Unmarshal(body, &transactions))
Expand All @@ -312,7 +312,7 @@ func (auth Auth) getTransactions(retType string) (string, *Transactions) {
return "", transactions
}

func (auth Auth) getStatements(retType string) (string, *Statements) {
func (auth Auth) GetStatements(retType string) (string, *Statements) {
body := auth.n26Request("/api/statements")
statements := &Statements{}
check(json.Unmarshal(body, &statements))
Expand All @@ -323,11 +323,17 @@ func (auth Auth) getStatements(retType string) (string, *Statements) {
return "", statements
}

func (auth Auth) getStatementPDF(ID string) {
func (auth Auth) GetStatementPDF(ID string) {
body := auth.n26Request(fmt.Sprintf("%s%s", "/api/statements/", ID))
ioutil.WriteFile(
fmt.Sprintf("%s.pdf", ID),
body,
0750,
)
}

func check(e error) {
if e != nil {
panic(e)
}
}
39 changes: 26 additions & 13 deletions n26.go → cmd/n26/n26.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"regexp"

"github.com/guitmz/n26"
"github.com/howeyc/gopass"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
Expand All @@ -20,7 +21,7 @@ func check(e error) {
}
}

func authentication() *Auth {
func authentication() *n26.Auth {
username := os.Getenv("N26_USERNAME")
if username == "" {
fmt.Print("N26 username: ")
Expand All @@ -33,7 +34,7 @@ func authentication() *Auth {
check(err)
password = string(maskedPass)
}
return &Auth{username, password}
return &n26.Auth{username, password}
}

func main() {
Expand All @@ -51,7 +52,7 @@ func main() {
Usage: "your balance information",
Action: func(c *cli.Context) error {
API := authentication()
prettyJSON, balance := API.getBalance(c.Args().First())
prettyJSON, balance := API.GetBalance(c.Args().First())
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
Expand All @@ -70,7 +71,7 @@ func main() {
Usage: "personal information",
Action: func(c *cli.Context) error {
API := authentication()
prettyJSON, info := API.getInfo(c.Args().First())
prettyJSON, info := API.GetInfo(c.Args().First())
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
Expand All @@ -87,7 +88,7 @@ func main() {
Usage: "general status of your account",
Action: func(c *cli.Context) error {
API := authentication()
prettyJSON, status := API.getStatus(c.Args().First())
prettyJSON, status := API.GetStatus(c.Args().First())
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
Expand All @@ -108,7 +109,7 @@ func main() {
Usage: "addresses linked to your account",
Action: func(c *cli.Context) error {
API := authentication()
prettyJSON, addresses := API.getAddresses(c.Args().First())
prettyJSON, addresses := API.GetAddresses(c.Args().First())
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
Expand Down Expand Up @@ -144,7 +145,7 @@ func main() {
Usage: "list your cards information",
Action: func(c *cli.Context) error {
API := authentication()
prettyJSON, cards := API.getCards(c.Args().First())
prettyJSON, cards := API.GetCards(c.Args().First())
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
Expand All @@ -171,7 +172,7 @@ func main() {
Usage: "your account limits",
Action: func(c *cli.Context) error {
API := authentication()
prettyJSON, limits := API.getLimits(c.Args().First())
prettyJSON, limits := API.GetLimits(c.Args().First())
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
Expand All @@ -197,7 +198,7 @@ func main() {
Usage: "your saved contacts",
Action: func(c *cli.Context) error {
API := authentication()
prettyJSON, contacts := API.getContacts(c.Args().First())
prettyJSON, contacts := API.GetContacts(c.Args().First())
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
Expand All @@ -224,25 +225,37 @@ func main() {
Usage: "your past transactions",
Action: func(c *cli.Context) error {
API := authentication()
prettyJSON, transactions := API.getTransactions(c.Args().First())
prettyJSON, transactions := API.GetTransactions(c.Args().First())
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
data := [][]string{}
for _, transaction := range *transactions {
amount := strconv.FormatFloat(transaction.Amount, 'f', -1, 64)
var location string
if transaction.MerchantCity != "" {
location = transaction.MerchantCity
if transaction.MerchantCountry != 0 {
location += ", "
}
}
if transaction.MerchantCountry != 0 {
location += "Country Code: " + fmt.Sprint(transaction.MerchantCountry)
}
data = append(data,
[]string{
transaction.PartnerName,
transaction.PartnerIban,
transaction.PartnerBic,
transaction.MerchantName,
location,
amount,
transaction.CurrencyCode,
transaction.Type,
},
)
}
table.SetHeader([]string{"Name", "IBAN", "BIC", "Amount", "Currency", "Type"})
table.SetHeader([]string{"Name", "IBAN", "BIC", "Merchant", "Location", "Amount", "Currency", "Type"})
table.AppendBulk(data)
table.Render()
}
Expand All @@ -258,10 +271,10 @@ func main() {
argument := c.Args().First()
switch {
case dateRegex.MatchString(argument):
API.getStatementPDF(argument)
API.GetStatementPDF(argument)
fmt.Println(fmt.Sprintf("[+] PDF file %s.pdf downloaded!", argument))
default:
prettyJSON, statements := API.getStatements(argument)
prettyJSON, statements := API.GetStatements(argument)
if prettyJSON != "" {
fmt.Println(prettyJSON)
} else {
Expand Down

0 comments on commit 325737c

Please sign in to comment.