This commit is contained in:
143
ynab/client.go
Normal file
143
ynab/client.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package ynab
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Reference: https://api.ynab.com/
|
||||
|
||||
const apiBaseURL = "https://api.ynab.com/v1/budgets/"
|
||||
|
||||
// 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 {
|
||||
BearerToken string
|
||||
BudgetID 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+c.BudgetID+endpoint+"?"+query.Encode(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create new GET request: %v", err)
|
||||
}
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.BearerToken))
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Format the message body, send an HTTP POST request, and return the processed response
|
||||
func (c *Client) post(endpoint string, out interface{}, body interface{}) error {
|
||||
// Attempt to marshall the body as JSON
|
||||
json, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal body to json: %v", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", apiBaseURL+c.BudgetID+endpoint, bytes.NewBuffer(json))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create new POST request: %v", err)
|
||||
}
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.BearerToken))
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("http POST request failed: %v", err)
|
||||
}
|
||||
|
||||
err = c.processResponse(res, out)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to process response: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Format the message body, send an HTTP POST request, and return the processed response
|
||||
func (c *Client) put(endpoint string, out interface{}, body interface{}) error {
|
||||
// Attempt to marshall the body as JSON
|
||||
json, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal body to json: %v", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("PUT", apiBaseURL+c.BudgetID+endpoint, bytes.NewBuffer(json))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create new POST request: %v", err)
|
||||
}
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.BearerToken))
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("http POST 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 - takes a bearer token
|
||||
func NewClient(budgetID, bearerToken string) (*Client, error) {
|
||||
transport := &http.Transport{
|
||||
ResponseHeaderTimeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
|
||||
// Create a new client
|
||||
c := &Client{
|
||||
BudgetID: budgetID,
|
||||
BearerToken: bearerToken,
|
||||
httpClient: client,
|
||||
transport: transport,
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
Reference in New Issue
Block a user