diff options
Diffstat (limited to 'users/wpcarro/tools/monzo_ynab')
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/.envrc | 2 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/.skip-subtree | 2 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/job.nix | 15 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/main.go | 10 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/monzo/client.go | 4 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/monzo/serde.go | 12 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/shell.nix | 9 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/tokens.go | 36 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/tokens.nix | 26 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/ynab/client.go | 25 | ||||
-rw-r--r-- | users/wpcarro/tools/monzo_ynab/ynab/serde.go | 10 |
11 files changed, 33 insertions, 118 deletions
diff --git a/users/wpcarro/tools/monzo_ynab/.envrc b/users/wpcarro/tools/monzo_ynab/.envrc index 2e3b53cd616f..6560926eaefd 100644 --- a/users/wpcarro/tools/monzo_ynab/.envrc +++ b/users/wpcarro/tools/monzo_ynab/.envrc @@ -1,5 +1,5 @@ source_up -use_nix + # TODO(wpcarro): Prefer age-nix solution if possible. export monzo_client_id="$(jq -j '.monzo | .clientId' < $WPCARRO/secrets.json)" export monzo_client_secret="$(jq -j '.monzo | .clientSecret' < $WPCARRO/secrets.json)" diff --git a/users/wpcarro/tools/monzo_ynab/.skip-subtree b/users/wpcarro/tools/monzo_ynab/.skip-subtree deleted file mode 100644 index 8db1f814f653..000000000000 --- a/users/wpcarro/tools/monzo_ynab/.skip-subtree +++ /dev/null @@ -1,2 +0,0 @@ -Subdirectories of this folder should not be imported since they are -internal to buildGo.nix and incompatible with readTree. diff --git a/users/wpcarro/tools/monzo_ynab/job.nix b/users/wpcarro/tools/monzo_ynab/job.nix deleted file mode 100644 index f710b73cefdb..000000000000 --- a/users/wpcarro/tools/monzo_ynab/job.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ depot, ... }: - -let - inherit (depot.users.wpcarro) gopkgs; -in -depot.nix.buildGo.program { - name = "job"; - srcs = [ - ./main.go - ]; - deps = with gopkgs; [ - kv - utils - ]; -} diff --git a/users/wpcarro/tools/monzo_ynab/main.go b/users/wpcarro/tools/monzo_ynab/main.go index bf3707138148..900deac0cbdb 100644 --- a/users/wpcarro/tools/monzo_ynab/main.go +++ b/users/wpcarro/tools/monzo_ynab/main.go @@ -10,8 +10,11 @@ package main import ( + "monzoClient" "monzoSerde" "os" + "ynabClient" + "ynabSerde" ) var ( @@ -34,11 +37,12 @@ func toYnab(tx monzoSerde.Transaction) ynabSerde.Transaction { } func main() { + monzo := monzoClient.Create() txs := monzo.TransactionsLast24Hours() var ynabTxs []ynabSerde.Transaction - for tx := range txs { - append(ynabTxs, toYnab(tx)) + for _, tx := range txs { + ynabTxs = append(ynabTxs, toYnab(tx)) } - ynab.PostTransactions(ynabTxs) + ynabClient.PostTransactions(ynabTxs) os.Exit(0) } diff --git a/users/wpcarro/tools/monzo_ynab/monzo/client.go b/users/wpcarro/tools/monzo_ynab/monzo/client.go index 8c6c41e29f40..9621ffc5ad51 100644 --- a/users/wpcarro/tools/monzo_ynab/monzo/client.go +++ b/users/wpcarro/tools/monzo_ynab/monzo/client.go @@ -27,8 +27,8 @@ func Create() *Client { } // Returns a slice of transactions from the last 24 hours. -func (c *Client) Transactions24Hours() []monzoSerde.Transaction { - token := tokens.AccessToken() +func (c *Client) TransactionsLast24Hours() []monzoSerde.Transaction { + token := tokens.GetState().AccessToken form := url.Values{"account_id": {accountID}} client := http.Client{} req, _ := http.NewRequest("POST", "https://api.monzo.com/transactions", diff --git a/users/wpcarro/tools/monzo_ynab/monzo/serde.go b/users/wpcarro/tools/monzo_ynab/monzo/serde.go index a38585eca632..e2f55dad4597 100644 --- a/users/wpcarro/tools/monzo_ynab/monzo/serde.go +++ b/users/wpcarro/tools/monzo_ynab/monzo/serde.go @@ -1,11 +1,9 @@ // This package hosts the serialization and deserialization logic for all of the // data types with which our application interacts from the Monzo API. -package main +package monzoSerde import ( "encoding/json" - "fmt" - "io/ioutil" "time" ) @@ -72,11 +70,3 @@ func deserializeTx(x string) (*Transaction, error) { err := json.Unmarshal([]byte(x), target) return target, err } - -func main() { - b, _ := ioutil.ReadFile("./fixture.json") - tx := string(b) - target, _ := deserializeTx(tx) - out, _ := serializeTx(target) - fmt.Println(out) -} diff --git a/users/wpcarro/tools/monzo_ynab/shell.nix b/users/wpcarro/tools/monzo_ynab/shell.nix deleted file mode 100644 index f777c13fefae..000000000000 --- a/users/wpcarro/tools/monzo_ynab/shell.nix +++ /dev/null @@ -1,9 +0,0 @@ -{ pkgs, ... }: - -pkgs.mkShell { - buildInputs = with pkgs; [ - go - goimports - godef - ]; -} diff --git a/users/wpcarro/tools/monzo_ynab/tokens.go b/users/wpcarro/tools/monzo_ynab/tokens.go index 4be967ccb803..01b57d3daafd 100644 --- a/users/wpcarro/tools/monzo_ynab/tokens.go +++ b/users/wpcarro/tools/monzo_ynab/tokens.go @@ -1,7 +1,7 @@ // Creating a Tokens server to manage my access and refresh tokens. Keeping this // as a separate server allows me to develop and use the access tokens without // going through client authorization. -package main +package tokens //////////////////////////////////////////////////////////////////////////////// // Dependencies @@ -46,7 +46,7 @@ type setTokensRequest struct { // This is our application state. type state struct { - accessToken string `json:"access_token"` + AccessToken string `json:"access_token"` refreshToken string `json:"refresh_token"` } @@ -90,7 +90,7 @@ func logTokens(access string, refresh string) { } func (state *state) String() string { - return fmt.Sprintf("state{\n\taccessToken: \"%s\",\n\trefreshToken: \"%s\"\n}\n", state.accessToken, state.refreshToken) + return fmt.Sprintf("state{\n\tAccessToken: \"%s\",\n\trefreshToken: \"%s\"\n}\n", state.AccessToken, state.refreshToken) } // Schedule a token refresh for `expiresIn` seconds using the provided @@ -104,10 +104,10 @@ func scheduleTokenRefresh(expiresIn int, refreshToken string) { log.Printf("Scheduling token refresh for %v\n", timestamp) time.Sleep(duration) log.Println("Refreshing tokens now...") - accessToken, refreshToken := refreshTokens(refreshToken) + AccessToken, refreshToken := refreshTokens(refreshToken) log.Println("Successfully refreshed tokens.") - logTokens(accessToken, refreshToken) - setState(accessToken, refreshToken) + logTokens(AccessToken, refreshToken) + setState(AccessToken, refreshToken) } // Exchange existing credentials for a new access token and `refreshToken`. Also @@ -169,8 +169,8 @@ func handleInterrupts() { go func() { sig := <-sigs log.Printf("Received signal to shutdown. %v\n", sig) - state := getState() - persistTokens(state.accessToken, state.refreshToken) + state := GetState() + persistTokens(state.AccessToken, state.refreshToken) done <- true }() @@ -179,25 +179,21 @@ func handleInterrupts() { os.Exit(0) } -// Set `accessToken` and `refreshToken` on application state. -func setState(accessToken string, refreshToken string) { - msg := writeMsg{state{accessToken, refreshToken}, make(chan bool)} +// Set `AccessToken` and `refreshToken` on application state. +func setState(AccessToken string, refreshToken string) { + msg := writeMsg{state{AccessToken, refreshToken}, make(chan bool)} chans.writes <- msg <-msg.sender } // Return our application state. -func getState() state { +func GetState() state { msg := readMsg{make(chan state)} chans.reads <- msg return <-msg.sender } -//////////////////////////////////////////////////////////////////////////////// -// Main -//////////////////////////////////////////////////////////////////////////////// - -func main() { +func StartServer() { // Manage application state. go func() { state := &state{} @@ -215,7 +211,7 @@ func main() { // As an attempt to maintain consistency between application // state and persisted state, everytime we write to the // application state, we will write to the store. - persistTokens(state.accessToken, state.refreshToken) + persistTokens(state.AccessToken, state.refreshToken) msg.sender <- true } } @@ -251,7 +247,7 @@ func main() { log.Fatal(http.ListenAndServe(":4242", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if req.URL.Path == "/refresh-tokens" && req.Method == "POST" { - state := getState() + state := GetState() go scheduleTokenRefresh(0, state.refreshToken) fmt.Fprintf(w, "Done.") } else if req.URL.Path == "/set-tokens" && req.Method == "POST" { @@ -273,7 +269,7 @@ func main() { } else if req.URL.Path == "/state" && req.Method == "GET" { // TODO(wpcarro): Ensure that this returns serialized state. w.Header().Set("Content-type", "application/json") - state := getState() + state := GetState() payload, _ := json.Marshal(state) io.WriteString(w, string(payload)) } else { diff --git a/users/wpcarro/tools/monzo_ynab/tokens.nix b/users/wpcarro/tools/monzo_ynab/tokens.nix deleted file mode 100644 index 4e2761bc7882..000000000000 --- a/users/wpcarro/tools/monzo_ynab/tokens.nix +++ /dev/null @@ -1,26 +0,0 @@ -{ depot, ... }: - -let - inherit (depot.users.wpcarro) gopkgs; - - auth = depot.nix.buildGo.package { - name = "auth"; - srcs = [ - ./auth.go - ]; - deps = with gopkgs; [ - utils - ]; - }; -in -depot.nix.buildGo.program { - name = "token-server"; - srcs = [ - ./tokens.go - ]; - deps = with gopkgs; [ - kv - utils - auth - ]; -} diff --git a/users/wpcarro/tools/monzo_ynab/ynab/client.go b/users/wpcarro/tools/monzo_ynab/ynab/client.go index b3e9930f621d..e63010b28189 100644 --- a/users/wpcarro/tools/monzo_ynab/ynab/client.go +++ b/users/wpcarro/tools/monzo_ynab/ynab/client.go @@ -1,24 +1,9 @@ -package client +package ynabClient import ( - "serde" + "ynabSerde" ) -// // See requests.txt for more details. -// func PostTransactions(accountID string, txs []serde.Transaction{}) error { -// return map[string]string{ -// "transactions": [ -// { -// "account_id": accountID, -// "date": "2019-12-30", -// "amount": 10000, -// "payee_name": "Richard Stallman", -// "memo": "Not so free software after all...", -// "cleared": "cleared", -// "approved": true, -// "flag_color": "red", -// "import_id": "xyz-123" -// } -// ] -// } -// } +// See requests.txt for more details. +func PostTransactions(txs []ynabSerde.Transaction) { +} diff --git a/users/wpcarro/tools/monzo_ynab/ynab/serde.go b/users/wpcarro/tools/monzo_ynab/ynab/serde.go index 53dd33e83637..45dd921b2403 100644 --- a/users/wpcarro/tools/monzo_ynab/ynab/serde.go +++ b/users/wpcarro/tools/monzo_ynab/ynab/serde.go @@ -1,10 +1,9 @@ // This package hosts the serialization and deserialization logic for all of the // data types with which our application interacts from the YNAB API. -package main +package ynabSerde import ( "encoding/json" - "fmt" "time" ) @@ -43,10 +42,3 @@ func deserializeTx(x string) (*Transaction, error) { err := json.Unmarshal([]byte(x), target) return target, err } - -func main() { - target, _ := deserializeTx(tx) - out, _ := serializeTx(target) - fmt.Println(out) - fmt.Println(ynabOut) -} |