Created
November 8, 2017 19:45
-
-
Save wilk/f0068258d27725a039194cd1af5021de to your computer and use it in GitHub Desktop.
Goxfer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"fmt" | |
"errors" | |
"github.com/parnurzeal/gorequest" | |
) | |
var ( | |
EXPENSE_ACCOUNT_BUXFER = os.Getenv("EXPENSE_ACCOUNT_BUXFER") | |
EXPENSE_ACCOUNT_ID int | |
INCOME_ACCOUNT_ID int | |
) | |
type AccountsListResponse struct { | |
Response struct { | |
Accounts []struct { | |
Id int `json:"id"` | |
Name string `json:"name"` | |
} `json:"accounts"` | |
Status string `json:"status"` | |
Token string `json:"token"` | |
} `json:"response"` | |
} | |
func main() { | |
fmt.Println("Fetching Buxfer's accounts list...") | |
var listResult AccountsListResponse | |
res, _, errs = request.Get(BUXFER_API_URL + "/accounts"). | |
Query("token=" + token). | |
EndStruct(&listResult) | |
if len(errs) > 0 { | |
panic(errs) | |
} | |
if res.StatusCode > 399 { | |
panic(res.Status) | |
} | |
if listResult.Response.Status != "OK" || len(listResult.Response.Accounts) == 0 { | |
panic(errors.New("An error occured when fetching the Buxfer accounts list")) | |
} | |
for _, account := range listResult.Response.Accounts { | |
if account.Name == EXPENSE_ACCOUNT_BUXFER { | |
EXPENSE_ACCOUNT_ID = account.Id | |
} else { | |
INCOME_ACCOUNT_ID = account.Id | |
} | |
} | |
fmt.Println("Accounts list fetched!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment