Compare commits
No commits in common. "54417bf436fa293b4ac9016b11226a2297b7441d" and "92a6246052e4f438a8f0a92005b71e3e798062b5" have entirely different histories.
54417bf436
...
92a6246052
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
// Reference: https://api.ynab.com/v1#/Accounts/
|
// Reference: https://api.ynab.com/v1#/Accounts/
|
||||||
|
|
||||||
type accounts struct {
|
type Accounts struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
Account struct {
|
Account struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
@ -29,8 +29,8 @@ type accounts struct {
|
|||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getAccount(accountID string) (*accounts, error) {
|
func (c *Client) GetAccount(accountID string) (*Accounts, error) {
|
||||||
response := accounts{}
|
response := Accounts{}
|
||||||
|
|
||||||
err := c.get(fmt.Sprintf("/accounts/%s", accountID), &response, url.Values{})
|
err := c.get(fmt.Sprintf("/accounts/%s", accountID), &response, url.Values{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -49,7 +49,7 @@ func (c *Client) SetAccountBalance(accountID string, balance int) error {
|
|||||||
return fmt.Errorf("failed to get ynab capital gains transaction ID: %v", err)
|
return fmt.Errorf("failed to get ynab capital gains transaction ID: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ynabAccount, err := c.getAccount(accountID)
|
ynabAccount, err := c.GetAccount(accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get ynab account with id '%s': %v", accountID, err)
|
return fmt.Errorf("failed to get ynab account with id '%s': %v", accountID, err)
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ const apiBaseURL = "https://api.ynab.com/v1/budgets/"
|
|||||||
// endpoints. It holds the login credentials, http client/transport,
|
// endpoints. It holds the login credentials, http client/transport,
|
||||||
// rate limit information, and the login session timer.
|
// rate limit information, and the login session timer.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
bearerToken string
|
BearerToken string
|
||||||
BudgetID string
|
BudgetID string
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
transport *http.Transport
|
transport *http.Transport
|
||||||
@ -32,7 +32,7 @@ func (c *Client) get(endpoint string, out interface{}, query url.Values) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create new GET request: %v", err)
|
return fmt.Errorf("failed to create new GET request: %v", err)
|
||||||
}
|
}
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.BearerToken))
|
||||||
|
|
||||||
res, err := c.httpClient.Do(req)
|
res, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -58,7 +58,7 @@ func (c *Client) post(endpoint string, out interface{}, body interface{}) error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create new POST request: %v", err)
|
return fmt.Errorf("failed to create new POST request: %v", err)
|
||||||
}
|
}
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.BearerToken))
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
|
||||||
res, err := c.httpClient.Do(req)
|
res, err := c.httpClient.Do(req)
|
||||||
@ -85,7 +85,7 @@ func (c *Client) put(endpoint string, out interface{}, body interface{}) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create new POST request: %v", err)
|
return fmt.Errorf("failed to create new POST request: %v", err)
|
||||||
}
|
}
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.BearerToken))
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
|
||||||
res, err := c.httpClient.Do(req)
|
res, err := c.httpClient.Do(req)
|
||||||
@ -140,7 +140,7 @@ func NewClient(budgetID, bearerToken string) (*Client, error) {
|
|||||||
// Create a new client
|
// Create a new client
|
||||||
c := &Client{
|
c := &Client{
|
||||||
BudgetID: budgetID,
|
BudgetID: budgetID,
|
||||||
bearerToken: bearerToken,
|
BearerToken: bearerToken,
|
||||||
httpClient: client,
|
httpClient: client,
|
||||||
transport: transport,
|
transport: transport,
|
||||||
loc: loc,
|
loc: loc,
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Reference: https://api.ynab.com/v1#/Transactions/
|
// Reference: https://api.ynab.com/v1#/Transactions/
|
||||||
type transaction struct {
|
type Transaction struct {
|
||||||
Type string `json:"type,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
ParentTransactionID interface{} `json:"parent_transaction_id,omitempty"`
|
ParentTransactionID interface{} `json:"parent_transaction_id,omitempty"`
|
||||||
ID string `json:"id,omitempty"`
|
ID string `json:"id,omitempty"`
|
||||||
@ -31,31 +31,31 @@ type transaction struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Used for single transaction requests
|
// Used for single transaction requests
|
||||||
type transactionRequest struct {
|
type TransactionRequest struct {
|
||||||
Transaction transaction `json:"transaction,omitempty"`
|
Transaction Transaction `json:"transaction,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used for single transaction responses
|
// Used for single transaction responses
|
||||||
type transactionResponse struct {
|
type TransactionResponse struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
TransactionIDs []string `json:"transaction_ids,omitempty"`
|
TransactionIDs []string `json:"transaction_ids,omitempty"`
|
||||||
Transaction transaction `json:"transaction"`
|
Transaction Transaction `json:"transaction"`
|
||||||
ServerKnowledge int `json:"server_knowledge,omitempty"`
|
ServerKnowledge int `json:"server_knowledge,omitempty"`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used for multiple transaction requests / responses
|
// Used for multiple transaction requests / responses
|
||||||
type transactionListResponse struct {
|
type TransactionsResponse struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
Transactions []transaction `json:"transactions"`
|
Transactions []Transaction `json:"transactions"`
|
||||||
ServerKnowledge int `json:"server_knowledge"`
|
ServerKnowledge int `json:"server_knowledge"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accepts a YNAB account ID and timestamp and returns all transactions in that account
|
// Accepts a YNAB account ID and timestamp and returns all transactions in that account
|
||||||
// since the date provided
|
// since the date provided
|
||||||
func (c *Client) GetAccountTransactions(accountID string, sinceDate time.Time) (*transactionListResponse, error) {
|
func (c *Client) GetAccountTransactions(accountID string, sinceDate time.Time) (*TransactionsResponse, error) {
|
||||||
response := transactionListResponse{}
|
response := TransactionsResponse{}
|
||||||
urlQuery := url.Values{}
|
urlQuery := url.Values{}
|
||||||
urlQuery.Add("since_date", sinceDate.Format("2006-01-02"))
|
urlQuery.Add("since_date", sinceDate.Format("2006-01-02"))
|
||||||
|
|
||||||
@ -86,8 +86,8 @@ func (c *Client) getTodayYnabCapitalGainsTransaction(accountID string) (string,
|
|||||||
// Accepts a YNAB account ID, transaction ID and transaction amount and updates the YNAB transaction with the matching ID
|
// Accepts a YNAB account ID, transaction ID and transaction amount and updates the YNAB transaction with the matching ID
|
||||||
// If transaction ID is blank, a new transaction is created for the amount specified
|
// If transaction ID is blank, a new transaction is created for the amount specified
|
||||||
func (c *Client) updateTodayYNABCapitalGainsTransaction(accountID string, transactionID string, amount int) error {
|
func (c *Client) updateTodayYNABCapitalGainsTransaction(accountID string, transactionID string, amount int) error {
|
||||||
request := transactionRequest{
|
request := TransactionRequest{
|
||||||
Transaction: transaction{
|
Transaction: Transaction{
|
||||||
AccountID: accountID,
|
AccountID: accountID,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
Date: time.Now().In(c.loc).Format("2006-01-02"),
|
Date: time.Now().In(c.loc).Format("2006-01-02"),
|
||||||
@ -97,7 +97,7 @@ func (c *Client) updateTodayYNABCapitalGainsTransaction(accountID string, transa
|
|||||||
Memo: fmt.Sprintf("Quoted at: %s", time.Now().In(c.loc).Format("2006-01-02 15:04:05")),
|
Memo: fmt.Sprintf("Quoted at: %s", time.Now().In(c.loc).Format("2006-01-02 15:04:05")),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
response := &transactionResponse{}
|
response := &TransactionResponse{}
|
||||||
var err error
|
var err error
|
||||||
if transactionID == "" { // create transaction
|
if transactionID == "" { // create transaction
|
||||||
err = c.post("/transactions", response, request)
|
err = c.post("/transactions", response, request)
|
||||||
|
Loading…
Reference in New Issue
Block a user