refactor!: change the way api calls are made

This commit is contained in:
TheWanderingCrow 2025-11-11 23:10:33 -05:00
parent 79a6c7a2c1
commit 874fd2bafe

View file

@ -11,31 +11,32 @@ type BudgetClient struct {
baseUrl string baseUrl string
apiKey string apiKey string
syncId string syncId string
fullUrl string
} }
func CreateBudgetClient(baseUrl string, apiKey string, syncId string) *BudgetClient { func CreateBudgetClient(baseUrl string, apiKey string, syncId string) *BudgetClient {
client := BudgetClient{baseUrl, apiKey, syncId} fullUrl := baseUrl + "/v1/budgets/" + syncId
client := BudgetClient{baseUrl, apiKey, syncId, fullUrl}
return &client return &client
} }
func (b BudgetClient) BankSync() bool { // Make a call to the actualbudget API
//
// method is POST, GET, ect
// path is the endpoint to call ex. /accounts/banksync
func (b BudgetClient) CallApi(method string, path string) bool {
var httpClient http.Client var httpClient http.Client
req, err := http.NewRequest("POST", b.baseUrl+"/v1/budgets/"+b.syncId+"/accounts/banksync", http.NoBody) req, err := http.NewRequest(method, b.fullUrl+path, http.NoBody)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
req.Header.Add("x-api-key", b.apiKey) req.Header.Add("x-api-key", b.apiKey)
log.Println("Sent off request")
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Println(resp) log.Println(resp)
if resp.StatusCode == http.StatusOK { return resp.StatusCode == http.StatusOK
return true
} else {
return false
}
} }
func main() { func main() {