move providers into providers subdirectory
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-11-13 17:39:22 -07:00
parent 64f7eed9fd
commit 4c01dc4ea2
19 changed files with 4 additions and 4 deletions

View File

@ -0,0 +1,8 @@
# Bitcoin Provider
A provider for bitcoin, backed by blockstream.info to query address balance and coinconvert.net for fiat conversion.
### Environment Variables
* *bitcoin_address_[0...]* - A series of bitcoin addresses with a suffix of _0 and each additional address counting up.
* *bitcoin_ynab_account* - The YNAB account ID used to keep track of Bitcoin balance

View File

@ -0,0 +1,36 @@
package bitcoin
import (
"fmt"
"net/url"
)
type Address struct {
Address string `json:"address"`
ChainStats struct {
FundedTxoCount int `json:"funded_txo_count"`
FundedTxoSum int `json:"funded_txo_sum"`
SpentTxoCount int `json:"spent_txo_count"`
SpentTxoSum int `json:"spent_txo_sum"`
TxCount int `json:"tx_count"`
} `json:"chain_stats"`
MempoolStats struct {
FundedTxoCount int `json:"funded_txo_count"`
FundedTxoSum int `json:"funded_txo_sum"`
SpentTxoCount int `json:"spent_txo_count"`
SpentTxoSum int `json:"spent_txo_sum"`
TxCount int `json:"tx_count"`
} `json:"mempool_stats"`
}
// GetAddress returns an Address struct populated with data from blockstream.info
// for a given BTC address
func (c *client) GetAddress(address string) (*Address, error) {
addressResponse := &Address{}
err := c.get(fmt.Sprintf("address/%s", address), addressResponse, url.Values{})
if err != nil {
return nil, err
}
return addressResponse, nil
}

View File

@ -0,0 +1,79 @@
package bitcoin
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
const apiBaseURL = "https://blockstream.info/api/"
// A client is the structure that will be used to consume the API
// endpoints. It holds the login credentials, http client/transport,
// rate limit information, and the login session timer.
type client struct {
httpClient *http.Client
transport *http.Transport
}
// Send an HTTP GET request, and return the processed response
func (c *client) get(endpoint string, out interface{}, query url.Values) error {
req, err := http.NewRequest("GET", apiBaseURL+endpoint+"?"+query.Encode(), nil)
if err != nil {
return fmt.Errorf("failed to create new GET request: %v", err)
}
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("http GET request failed: %v", err)
}
err = c.processResponse(res, out)
if err != nil {
return fmt.Errorf("failed to process response: %v", err)
}
return nil
}
// processResponse takes the body of an HTTP response, and either returns
// the error code, or unmarshalls the JSON response, extracts
// rate limit info, and places it into the object
// output parameter. This function closes the response body after reading it.
func (c *client) processResponse(res *http.Response, out interface{}) error {
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
if res.StatusCode != 200 && res.StatusCode != 201 {
return fmt.Errorf("unexpected http status code '%d': %v", res.StatusCode, err)
}
err = json.Unmarshal(body, out)
if err != nil {
return fmt.Errorf("failed to unmarshal response body: %v", err)
}
return nil
}
// newClient is the factory function for clients
func newClient() (*client, error) {
transport := &http.Transport{
ResponseHeaderTimeout: 5 * time.Second,
}
httpClient := &http.Client{
Transport: transport,
}
// Create a new client
c := &client{
httpClient: httpClient,
transport: transport,
}
return c, nil
}

41
providers/bitcoin/fiat.go Normal file
View File

@ -0,0 +1,41 @@
package bitcoin
import (
"fmt"
"net/http"
)
// example: https://api.coinconvert.net/convert/btc/cad?amount=1
const fiatConvertURL = "https://api.coinconvert.net/convert/btc/cad"
type FiatConversion struct {
Status string `json:"status"`
BTC int `json:"BTC"`
CAD float64 `json:"CAD"`
}
// BTC to CAD FIAT conversion - accepts an
// amount in satoshi's and returns a CAD amount * 1000
func (c *client) ConvertBTCToCAD(amount int) (int, error) {
fiatConversion := &FiatConversion{}
req, err := http.NewRequest("GET", fiatConvertURL+"?amount=1", nil)
if err != nil {
return 0, fmt.Errorf("failed to create new GET request: %v", err)
}
res, err := c.httpClient.Do(req)
if err != nil {
return 0, fmt.Errorf("http GET request failed: %v", err)
}
err = c.processResponse(res, fiatConversion)
if err != nil {
return 0, fmt.Errorf("failed to process response: %v", err)
}
if fiatConversion.Status != "success" {
return 0, fmt.Errorf("fiat conversion status was '%s' but expected 'success'", fiatConversion.Status)
}
return (amount * int(fiatConversion.CAD*1000)) / 100000000, nil // one BTC = one hundred million satoshi's
}

View File

@ -0,0 +1,70 @@
package bitcoin
import (
"fmt"
"os"
)
type Provider struct {
bitcoinAddresses []string // Slice of bitcoin addresses this provider monitors
ynabAccountID string // YNAB account ID this provider updates - all bitcoin addresses are summed up and mapped to this YNAB account
client *client // HTTP client for interacting with Questrade API
}
func (p *Provider) Name() string {
return "Bitcoin - Blockstream.info"
}
// Configures the provider for usage via environment variables and persistentData
// If an error is returned, the provider will not be used
func (p *Provider) Configure() error {
var err error
// Load environment variables in continous series with suffix starting at 0
// Multiple addresses can be configured, (eg _1, _2)
// As soon as the series is interrupted, we assume we're done
p.bitcoinAddresses = make([]string, 0)
for i := 0; true; i++ {
bitcoinAddress := os.Getenv(fmt.Sprintf("bitcoin_address_%d", i))
if bitcoinAddress == "" {
if i == 0 {
return fmt.Errorf("this account provider is not configured")
}
break
}
p.bitcoinAddresses = append(p.bitcoinAddresses, bitcoinAddress)
}
p.ynabAccountID = os.Getenv("bitcoin_ynab_account")
// Create new HTTP client
p.client, err = newClient()
if err != nil {
return fmt.Errorf("failed to create new bitcoin client: %v", err)
}
return nil
}
// Returns slices of account balances and mapped YNAB account IDs, along with an error
func (p *Provider) GetBalances() ([]int, []string, error) {
balances := make([]int, 0)
ynabAccountIDs := make([]string, 0)
var satoshiBalance int
for _, bitcoinAddress := range p.bitcoinAddresses {
addressResponse, err := p.client.GetAddress(bitcoinAddress)
if err != nil {
return balances, ynabAccountIDs, fmt.Errorf("failed to get bitcoin address '%s': %v", bitcoinAddress, err)
}
satoshiBalance += addressResponse.ChainStats.FundedTxoSum - addressResponse.ChainStats.SpentTxoSum
}
fiatBalance, err := p.client.ConvertBTCToCAD(satoshiBalance)
if err != nil {
return balances, ynabAccountIDs, fmt.Errorf("failed to convert satoshi balance to fiat balance: %v", err)
}
balances = append(balances, fiatBalance)
ynabAccountIDs = append(ynabAccountIDs, p.ynabAccountID)
return balances, ynabAccountIDs, nil
}

View File

@ -0,0 +1,8 @@
# Questrade Provider
A provider for Questrade using the Questrade API.
### Environment Variables
* *questrade_account_[0...]* - A series of Questrade account numbers with a suffix of _0 and each additional account counting up.
* *questrade_ynab_account_[0...]* - The YNAB account ID for the matching questrade account. Each configured questrade account must have a matching YNAB account.

View File

@ -0,0 +1,111 @@
package questrade
import (
"fmt"
"net/url"
"strconv"
)
// Account represents an account associated with the user on behalf
// of which the API client is authorized.
//
// Ref: http://www.questrade.com/api/documentation/rest-operations/account-calls/accounts
type Account struct {
// Type of the account (e.g., "Cash", "Margin").
Type string `json:"type"`
// Eight-digit account number (e.g., "26598145")
// Stored as a string, it's used for making account-related API calls
Number string `json:"number"`
// Status of the account (e.g., Active).
Status string `json:"status"`
// Whether this is a primary account for the holder.
IsPrimary bool `json:"isPrimary"`
// Whether this account is one that gets billed for various expenses such as inactivity fees, market data, etc.
IsBilling bool `json:"isBilling"`
// Type of client holding the account (e.g., "Individual").
ClientAccountType string `json:"clientAccountType"`
}
// Balance belonging to an Account
//
// Ref: http://www.questrade.com/api/documentation/rest-operations/account-calls/accounts-id-balances
type Balance struct {
// Currency of the balance figure(e.g., "USD" or "CAD").
Currency string `json:"currency"`
// Balance amount.
Cash float32 `json:"cash"`
// Market value of all securities in the account in a given currency.
MarketValue float32 `json:"marketValue"`
// Equity as a difference between cash and marketValue properties.
TotalEquity float32 `json:"totalEquity"`
// Buying power for that particular currency side of the account.
BuyingPower float32 `json:"buyingPower"`
// Maintenance excess for that particular side of the account.
MaintenanceExcess float32 `json:"maintenanceExcess"`
// Whether real-time data was used to calculate the above values.
IsRealTime bool `json:"isRealTime"`
}
// AccountBalances represents per-currency and combined balances for a specified account.
//
// Ref: http://www.questrade.com/api/documentation/rest-operations/account-calls/accounts-id-balances
type AccountBalances struct {
PerCurrencyBalances []Balance `json:"perCurrencyBalances"`
CombinedBalances []Balance `json:"combinedBalances"`
SODPerCurrencyBalances []Balance `json:"sodPerCurrencyBalances"`
SODCombinedBalances []Balance `json:"sodCombinedBalances"`
}
// GetAccounts returns the logged-in User ID, and a list of accounts
// belonging to that user.
func (c *client) GetAccounts() (int, []Account, error) {
list := struct {
UserID int `json:"userId"`
Accounts []Account `json:"accounts"`
}{}
err := c.get("v1/accounts", &list, url.Values{})
if err != nil {
return 0, []Account{}, err
}
return list.UserID, list.Accounts, nil
}
// GetBalances returns the balances for the account with the specified account number
func (c *client) GetBalances(number string) (AccountBalances, error) {
bal := AccountBalances{}
err := c.get("v1/accounts/"+number+"/balances", &bal, url.Values{})
if err != nil {
return AccountBalances{}, err
}
return bal, nil
}
func (c *client) GetQuestradeAccountBalance(accountID int) (int, error) {
balances, err := c.GetBalances(strconv.Itoa(accountID))
if err != nil {
return 0, fmt.Errorf("failed to get balances for account ID '%d': %v", accountID, err)
}
for _, balance := range balances.CombinedBalances {
if balance.Currency != "CAD" {
continue
}
return int(balance.TotalEquity) * 1000, nil
}
return 0, fmt.Errorf("could not find a CAD balance for this account in questade response")
}

View File

@ -0,0 +1,132 @@
package questrade
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
const loginServerURL = "https://login.questrade.com/oauth2/"
type LoginCredentials struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
ApiServer string `json:"api_server"`
}
// A client is the structure that will be used to consume the API
// endpoints. It holds the login credentials, http client/transport,
// rate limit information, and the login session timer.
type client struct {
Credentials LoginCredentials
SessionTimer *time.Timer
RateLimitRemaining int
RateLimitReset time.Time
httpClient *http.Client
transport *http.Transport
}
// authHeader is a shortcut that returns a string to be placed
// in the authorization header of API calls
func (l LoginCredentials) authHeader() string {
return l.TokenType + " " + l.AccessToken
}
// Send an HTTP GET request, and return the processed response
func (c *client) get(endpoint string, out interface{}, query url.Values) error {
req, err := http.NewRequest("GET", c.Credentials.ApiServer+endpoint+"?"+query.Encode(), nil)
if err != nil {
return err
}
req.Header.Add("Authorization", c.Credentials.authHeader())
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("http get request failed: %v", err)
}
err = c.processResponse(res, out)
if err != nil {
return err
}
return nil
}
// processResponse takes the body of an HTTP response, and either returns
// the error code, or unmarshalls the JSON response, extracts
// rate limit info, and places it into the object
// output parameter. This function closes the response body after reading it.
func (c *client) processResponse(res *http.Response, out interface{}) error {
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return err
}
if res.StatusCode != 200 {
return newQuestradeError(res, body)
}
err = json.Unmarshal(body, out)
if err != nil {
return err
}
reset, _ := strconv.Atoi(res.Header.Get("X-RateLimit-Reset"))
c.RateLimitReset = time.Unix(int64(reset), 0)
c.RateLimitRemaining, _ = strconv.Atoi(res.Header.Get("X-RateLimit-Remaining"))
return nil
}
// Login takes the refresh token from the client login credentials
// and exchanges it for an access token. Returns a timer that
// expires when the login session is over.
// TODO - Return a proper error when login fails with HTTP 400 - Bad Request
func (c *client) login() error {
vars := url.Values{"grant_type": {"refresh_token"}, "refresh_token": {c.Credentials.RefreshToken}}
res, err := c.httpClient.PostForm(loginServerURL+"token", vars)
if err != nil {
return err
}
err = c.processResponse(res, &c.Credentials)
if err != nil {
return err
}
c.SessionTimer = time.NewTimer(time.Duration(c.Credentials.ExpiresIn) * time.Second)
return nil
}
// newClient is the factory function for clients - takes a refresh token and logs in
func newClient(refreshToken string) (*client, error) {
transport := &http.Transport{
ResponseHeaderTimeout: 5 * time.Second,
}
httpClient := &http.Client{
Transport: transport,
}
// Create a new client
c := &client{
Credentials: LoginCredentials{
RefreshToken: refreshToken,
},
httpClient: httpClient,
transport: transport,
}
err := c.login()
if err != nil {
return nil, err
}
return c, nil
}

View File

@ -0,0 +1,40 @@
package questrade
import (
"encoding/json"
"fmt"
"net/http"
)
type QuestradeError struct {
Code int `json:"code"`
StatusCode int
Message string `json:"message"`
Endpoint string
}
func newQuestradeError(res *http.Response, body []byte) QuestradeError {
// Unmarshall the error text
var e QuestradeError
err := json.Unmarshal(body, &e)
if err != nil {
e.Code = -999
e.Message = string(body)
}
e.StatusCode = res.StatusCode
e.Endpoint = res.Request.URL.String()
return e
}
func (q QuestradeError) Error() string {
return fmt.Sprintf("\nQuestradeError:\n"+
"\tStatus code: HTTP %d\n"+
"\tEndpoint: %s\n"+
"\tError code: %d\n"+
"\tMessage: %s\n",
q.StatusCode,
q.Endpoint,
q.Code,
q.Message)
}

View File

@ -0,0 +1,147 @@
package questrade
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strconv"
"time"
)
type persistentData struct {
QuestradeRefreshToken string `json:"questradeRefreshToken"` // Questrade API OAuth2 refresh token
}
type Provider struct {
questradeAccountIDs []int // Slice of Questrade account numbers this provider monitors
ynabAccountIDs []string // Slice of YNAB account ID's this provider updates - index position maps with questradeAccountIDs
data *persistentData // Data stored on disk and loaded when program starts
client *client // HTTP client for interacting with Questrade API
lastRefresh time.Time
}
func (p *Provider) Name() string {
return "Questrade"
}
// Configures the provider for usage via environment variables and persistentData
// If an error is returned, the provider will not be used
func (p *Provider) Configure() error {
var err error
p.questradeAccountIDs = make([]int, 0)
p.ynabAccountIDs = make([]string, 0)
// Load environment variables in continous series with suffix starting at 0
// Multiple accounts can be configured, (eg _1, _2)
// As soon as the series is interrupted, we assume we're done
for i := 0; true; i++ {
questradeAccountIDString := os.Getenv(fmt.Sprintf("questrade_account_%d", i))
ynabAccountID := os.Getenv(fmt.Sprintf("questrade_ynab_account_%d", i))
if questradeAccountIDString == "" || ynabAccountID == "" {
if i == 0 {
return fmt.Errorf("this account provider is not configured")
}
break
}
questradeAccountID, err := strconv.Atoi(questradeAccountIDString)
if err != nil {
return fmt.Errorf("failed to convert environment variable questrade_account_%d with value of '%s' to integer: %v", i, questradeAccountIDString, err)
}
p.questradeAccountIDs = append(p.questradeAccountIDs, questradeAccountID)
p.ynabAccountIDs = append(p.ynabAccountIDs, ynabAccountID)
}
// Load persistent data from disk - the OAuth2.0 refresh tokens are one time use
p.data, err = loadPersistentData()
if err != nil {
return fmt.Errorf("failed to load questrade configuration: %v", err)
}
// Create new HTTP client and login to API - will error if login fails
err = p.refresh()
if err != nil {
return fmt.Errorf("failed to refresh http client: %v", err)
}
return nil
}
// Returns slices of account balances and mapped YNAB account IDs, along with an error
func (p *Provider) GetBalances() ([]int, []string, error) {
// Refresh credentials if past half way until expiration
if p.lastRefresh.Add(time.Second * time.Duration(p.client.Credentials.ExpiresIn) / 2).Before(time.Now()) {
err := p.refresh()
if err != nil {
return make([]int, 0), make([]string, 0), fmt.Errorf("failed to refresh http client: %v", err)
}
}
// Gather account balances from Questrade API
balances := make([]int, 0)
for _, questradeAccountID := range p.questradeAccountIDs {
balance, err := p.client.GetQuestradeAccountBalance(questradeAccountID)
if err != nil {
return balances, p.ynabAccountIDs, fmt.Errorf("failed to get questrade account balance for account ID '%d': %v", questradeAccountID, err)
}
balances = append(balances, balance)
}
return balances, p.ynabAccountIDs, nil
}
func (p *Provider) refresh() error {
var err error
// Create new HTTP client and login to API - will error if login fails
p.client, err = newClient(p.data.QuestradeRefreshToken)
if err != nil {
return fmt.Errorf("failed to create new questrade client: %v", err)
}
p.lastRefresh = time.Now()
// After logging in, we get a new refresh token - save it for next login
p.data.QuestradeRefreshToken = p.client.Credentials.RefreshToken
err = savePersistentData(p.data)
if err != nil {
return fmt.Errorf("failed to save persistent data: %v", err)
}
return nil
}
// Load persistent data from disk, if it fails it initializes using environment variables
func loadPersistentData() (*persistentData, error) {
data := &persistentData{}
f, err := os.Open("data/questrade-data.json")
if errors.Is(err, os.ErrNotExist) {
// handle the case where the file doesn't exist
data.QuestradeRefreshToken = os.Getenv("questrade_refresh_token")
return data, nil
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read file data/questrade-data.json: %v", err)
}
err = json.Unmarshal(b, data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal data/questrade-data.json to PersistentData struct: %v", err)
}
return data, nil
}
// Save persistent data to disk, this should be done any time the data changes to ensure it can be loaded on next run
func savePersistentData(data *persistentData) error {
b, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("failed to marshal persistentData to bytes: %v", err)
}
err = os.WriteFile("data/questrade-data.json", b, 0644)
if err != nil {
return fmt.Errorf("failed to write file data/questrade-data.json: %v", err)
}
return nil
}

View File

@ -0,0 +1,48 @@
# Static JSON Finnhub Provider
If you just want to provide a static JSON file as input containing symbols and quantities owned, this provider will use finnhub for pricing quotes for the symbols provided.
### Example data/staticjsonFinnhub-data.json
You can define many to many relationships, multiple YNAB accounts containing multiple types of securities using json arrays.
```json
{
"accounts":[
{
"ynabAccountId":"d54da05a-cs20-4654-bcff-9ce36f43225d",
"securities":[
{
"symbol":"SPY",
"quantity":420
},
{
"symbol":"BRK.A",
"quantity":5
},
{
"symbol":"CAPE",
"quantity":69
}
]
},
{
"ynabAccountId":"fdfedg45-c2g1-445-abdd-9dsa445sd54",
"securities":[
{
"symbol":"VCN.TO",
"quantity":100
},
{
"symbol":"XSB.TO",
"quantity":50
},
{
"symbol":"DLR.TO",
"quantity":20
}
]
}
]
}
```

View File

@ -0,0 +1,84 @@
package staticjsonFinnhub
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
const apiBaseURL = "https://finnhub.io/api/v1/"
// A client is the structure that will be used to consume the API
// endpoints. It holds the login credentials, http client/transport,
// rate limit information, and the login session timer.
type client struct {
apiToken string
httpClient *http.Client
transport *http.Transport
}
// Send an HTTP GET request, and return the processed response
func (c *client) get(endpoint string, out interface{}, query url.Values) error {
req, err := http.NewRequest("GET", apiBaseURL+endpoint+"?"+query.Encode(), nil)
if err != nil {
return err
}
req.Header.Add("X-Finnhub-Token", c.apiToken)
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("http get request failed: %v", err)
}
err = c.processResponse(res, out)
if err != nil {
return err
}
return nil
}
// processResponse takes the body of an HTTP response, and either returns
// the error code, or unmarshalls the JSON response, extracts
// rate limit info, and places it into the object
// output parameter. This function closes the response body after reading it.
func (c *client) processResponse(res *http.Response, out interface{}) error {
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("got http response status '%d' but expected 200", res.StatusCode)
}
err = json.Unmarshal(body, out)
if err != nil {
return err
}
return nil
}
// newClient is the factory function for clients - takes an API token
func newClient(apiToken string) (*client, error) {
transport := &http.Transport{
ResponseHeaderTimeout: 5 * time.Second,
}
httpClient := &http.Client{
Transport: transport,
}
// Create a new client
c := &client{
apiToken: apiToken,
httpClient: httpClient,
transport: transport,
}
return c, nil
}

View File

@ -0,0 +1,100 @@
package staticjsonFinnhub
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
)
type security struct {
Symbol string `json:"symbol"`
Quantity int `json:"quantity"`
}
type account struct {
YnabAccountID string `json:"ynabAccountId"`
Securities []security `json:"securities"`
}
type inputData struct {
Accounts []account `json:"accounts"`
}
type Provider struct {
data *inputData // Data stored on disk and loaded when program starts
client *client // HTTP client for interacting with Finnhub API
}
func (p *Provider) Name() string {
return "Static JSON - Finnhub"
}
// Configures the provider for usage via environment variables and inputData
// If an error is returned, the provider will not be used
func (p *Provider) Configure() error {
var err error
// Load input data from disk
p.data, err = loadInputData()
if err != nil {
return err
}
b, _ := json.Marshal(p.data)
fmt.Println(string(b))
apiKey := os.Getenv("staticjson_finnhub_key")
if apiKey == "" {
return fmt.Errorf("this account provider is not configured: missing staticjson_finnhub_key environment variable")
}
p.client, err = newClient(apiKey)
if err != nil {
return fmt.Errorf("failed to create new client: %v", err)
}
return nil
}
// Returns slices of account balances and mapped YNAB account IDs, along with an error
func (p *Provider) GetBalances() ([]int, []string, error) {
balances := make([]int, 0)
ynabAccountIDs := make([]string, 0)
for i := range p.data.Accounts {
balance := 0
for j := range p.data.Accounts[i].Securities {
price, err := p.client.getQuote(p.data.Accounts[i].Securities[j].Symbol)
if err != nil {
return balances, ynabAccountIDs, fmt.Errorf("failed to get quote for security with symbol '%s': %v", p.data.Accounts[i].Securities[j].Symbol, err)
}
balance += price * p.data.Accounts[i].Securities[j].Quantity
}
balances = append(balances, balance)
ynabAccountIDs = append(ynabAccountIDs, p.data.Accounts[i].YnabAccountID)
}
return balances, ynabAccountIDs, nil
}
// Load input data from disk
func loadInputData() (*inputData, error) {
data := &inputData{}
f, err := os.Open("data/staticjsonFinnhub-data.json")
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("this account provider is not configured: missing data/staticjsonFinnhub-data.json")
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read file data/staticjsonFinnhub-data.json: %v", err)
}
err = json.Unmarshal(b, data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal data/staticjsonFinnhub-data.json to inputData struct: %v", err)
}
return data, nil
}

View File

@ -0,0 +1,26 @@
package staticjsonFinnhub
import (
"fmt"
"net/url"
)
type quote struct {
C float64 `json:"c"` // Current price
H float64 `json:"h"` // High price of the day
L float64 `json:"l"` // Low price of the day
O float64 `json:"O"` // Open price of the day
Pc float64 `json:"pc"` // Previous close price
T int `json:"t"` // ?
}
func (c client) getQuote(symbol string) (int, error) {
quoteResponse := &quote{}
query := url.Values{}
query.Add("symbol", symbol)
err := c.get("/quote", quoteResponse, query)
if err != nil {
return 0, fmt.Errorf("http get request error: %v", err)
}
return int(quoteResponse.C * 1000), nil
}

View File

@ -0,0 +1,48 @@
# Static JSON Yahoo Finance Provider
If you just want to provide a static JSON file as input containing symbols and quantities owned, this provider will use Yahoo Finance for pricing quotes for the symbols provided.
### Example data/staticjsonYahooFinance-data.json
You can define many to many relationships, multiple YNAB accounts containing multiple types of securities using json arrays.
```json
{
"accounts":[
{
"ynabAccountId":"d54da05a-cs20-4654-bcff-9ce36f43225d",
"securities":[
{
"symbol":"SPY",
"quantity":420
},
{
"symbol":"BRK.A",
"quantity":5
},
{
"symbol":"CAPE",
"quantity":69
}
]
},
{
"ynabAccountId":"fdfedg45-c2g1-445-abdd-9dsa445sd54",
"securities":[
{
"symbol":"VCN.TO",
"quantity":100
},
{
"symbol":"XSB.TO",
"quantity":50
},
{
"symbol":"DLR.TO",
"quantity":20
}
]
}
]
}
```

View File

@ -0,0 +1,83 @@
package staticjsonYahooFinance
import (
"fmt"
"net/url"
)
type chart struct {
Chart struct {
Result []struct {
Meta struct {
Currency string `json:"currency"`
Symbol string `json:"symbol"`
ExchangeName string `json:"exchangeName"`
InstrumentType string `json:"instrumentType"`
FirstTradeDate int `json:"firstTradeDate"`
RegularMarketTime int `json:"regularMarketTime"`
Gmtoffset int `json:"gmtoffset"`
Timezone string `json:"timezone"`
ExchangeTimezoneName string `json:"exchangeTimezoneName"`
RegularMarketPrice float64 `json:"regularMarketPrice"`
ChartPreviousClose float64 `json:"chartPreviousClose"`
PreviousClose float64 `json:"previousClose"`
Scale int `json:"scale"`
PriceHint int `json:"priceHint"`
CurrentTradingPeriod struct {
Pre struct {
Timezone string `json:"timezone"`
Start int `json:"start"`
End int `json:"end"`
Gmtoffset int `json:"gmtoffset"`
} `json:"pre"`
Regular struct {
Timezone string `json:"timezone"`
Start int `json:"start"`
End int `json:"end"`
Gmtoffset int `json:"gmtoffset"`
} `json:"regular"`
Post struct {
Timezone string `json:"timezone"`
Start int `json:"start"`
End int `json:"end"`
Gmtoffset int `json:"gmtoffset"`
} `json:"post"`
} `json:"currentTradingPeriod"`
TradingPeriods [][]struct {
Timezone string `json:"timezone"`
Start int `json:"start"`
End int `json:"end"`
Gmtoffset int `json:"gmtoffset"`
} `json:"tradingPeriods"`
DataGranularity string `json:"dataGranularity"`
Range string `json:"range"`
ValidRanges []string `json:"validRanges"`
} `json:"meta"`
Timestamp []int `json:"timestamp"`
Indicators struct {
Quote []struct {
Volume []any `json:"volume"`
High []any `json:"high"`
Close []any `json:"close"`
Open []any `json:"open"`
Low []any `json:"low"`
} `json:"quote"`
} `json:"indicators"`
} `json:"result"`
Error any `json:"error"`
} `json:"chart"`
}
func (c client) getChart(symbol string) (int, error) {
chartResponse := &chart{}
err := c.get(fmt.Sprintf("/chart/%s", symbol), chartResponse, url.Values{})
if err != nil {
return 0, fmt.Errorf("http get request error: %v", err)
}
if len(chartResponse.Chart.Result) != 1 {
return 0, fmt.Errorf("unexpected length of results - expected 1 but got %d", len(chartResponse.Chart.Result))
}
return int(chartResponse.Chart.Result[0].Meta.RegularMarketPrice * 1000), nil
}

View File

@ -0,0 +1,81 @@
package staticjsonYahooFinance
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
const apiBaseURL = "https://query1.finance.yahoo.com/v8/finance/"
// A client is the structure that will be used to consume the API
// endpoints. It holds the login credentials, http client/transport,
// rate limit information, and the login session timer.
type client struct {
httpClient *http.Client
transport *http.Transport
}
// Send an HTTP GET request, and return the processed response
func (c *client) get(endpoint string, out interface{}, query url.Values) error {
req, err := http.NewRequest("GET", apiBaseURL+endpoint+"?"+query.Encode(), nil)
if err != nil {
return err
}
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("http get request failed: %v", err)
}
err = c.processResponse(res, out)
if err != nil {
return err
}
return nil
}
// processResponse takes the body of an HTTP response, and either returns
// the error code, or unmarshalls the JSON response, extracts
// rate limit info, and places it into the object
// output parameter. This function closes the response body after reading it.
func (c *client) processResponse(res *http.Response, out interface{}) error {
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("got http response status '%d' but expected 200", res.StatusCode)
}
err = json.Unmarshal(body, out)
if err != nil {
return err
}
return nil
}
// newClient is the factory function for clients - takes an API token
func newClient() (*client, error) {
transport := &http.Transport{
ResponseHeaderTimeout: 5 * time.Second,
}
httpClient := &http.Client{
Transport: transport,
}
// Create a new client
c := &client{
httpClient: httpClient,
transport: transport,
}
return c, nil
}

View File

@ -0,0 +1,92 @@
package staticjsonYahooFinance
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
)
type security struct {
Symbol string `json:"symbol"`
Quantity int `json:"quantity"`
}
type account struct {
YnabAccountID string `json:"ynabAccountId"`
Securities []security `json:"securities"`
}
type inputData struct {
Accounts []account `json:"accounts"`
}
type Provider struct {
data *inputData // Data stored on disk and loaded when program starts
client *client // HTTP client for interacting with Finnhub API
}
func (p *Provider) Name() string {
return "Static JSON - Yahoo Finance"
}
// Configures the provider for usage via environment variables and inputData
// If an error is returned, the provider will not be used
func (p *Provider) Configure() error {
var err error
// Load input data from disk
p.data, err = loadInputData()
if err != nil {
return err
}
p.client, err = newClient()
if err != nil {
return fmt.Errorf("failed to create new client: %v", err)
}
return nil
}
// Returns slices of account balances and mapped YNAB account IDs, along with an error
func (p *Provider) GetBalances() ([]int, []string, error) {
balances := make([]int, 0)
ynabAccountIDs := make([]string, 0)
for i := range p.data.Accounts {
balance := 0
for j := range p.data.Accounts[i].Securities {
price, err := p.client.getChart(p.data.Accounts[i].Securities[j].Symbol)
if err != nil {
return balances, ynabAccountIDs, fmt.Errorf("failed to get quote for security with symbol '%s': %v", p.data.Accounts[i].Securities[j].Symbol, err)
}
balance += price * p.data.Accounts[i].Securities[j].Quantity
}
balances = append(balances, balance)
ynabAccountIDs = append(ynabAccountIDs, p.data.Accounts[i].YnabAccountID)
}
return balances, ynabAccountIDs, nil
}
// Load input data from disk
func loadInputData() (*inputData, error) {
data := &inputData{}
f, err := os.Open("data/staticjsonYahooFinance-data.json")
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("this account provider is not configured: missing data/staticjsonYahooFinance-data.json")
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read file data/staticjsonYahooFinance-data.json: %v", err)
}
err = json.Unmarshal(b, data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal data/staticjsonYahooFinance-data.json to inputData struct: %v", err)
}
return data, nil
}