-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathclient.go
181 lines (150 loc) · 4.77 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2018, Bruno M V Souza <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-2-Clause license that can be
// found in the LICENSE file.
// Package ynab implements the client API
package ynab // import "github.com/brunomvsouza/ynab.go"
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"sync"
"github.com/brunomvsouza/ynab.go/api"
"github.com/brunomvsouza/ynab.go/api/account"
"github.com/brunomvsouza/ynab.go/api/budget"
"github.com/brunomvsouza/ynab.go/api/category"
"github.com/brunomvsouza/ynab.go/api/month"
"github.com/brunomvsouza/ynab.go/api/payee"
"github.com/brunomvsouza/ynab.go/api/transaction"
"github.com/brunomvsouza/ynab.go/api/user"
)
const apiEndpoint = "https://api.youneedabudget.com/v1"
// ClientServicer contract for a client service API
type ClientServicer interface {
User() *user.Service
Budget() *budget.Service
Account() *account.Service
Category() *category.Service
Payee() *payee.Service
Month() *month.Service
Transaction() *transaction.Service
}
// NewClient facilitates the creation of a new client instance
func NewClient(accessToken string) ClientServicer {
c := &client{
accessToken: accessToken,
client: http.DefaultClient,
}
c.user = user.NewService(c)
c.budget = budget.NewService(c)
c.account = account.NewService(c)
c.category = category.NewService(c)
c.payee = payee.NewService(c)
c.month = month.NewService(c)
c.transaction = transaction.NewService(c)
return c
}
// client API
type client struct {
sync.Mutex
accessToken string
client *http.Client
user *user.Service
budget *budget.Service
account *account.Service
category *category.Service
payee *payee.Service
month *month.Service
transaction *transaction.Service
}
// User returns user.Service API instance
func (c *client) User() *user.Service {
return c.user
}
// Budget returns budget.Service API instance
func (c *client) Budget() *budget.Service {
return c.budget
}
// Account returns account.Service API instance
func (c *client) Account() *account.Service {
return c.account
}
// Category returns category.Service API instance
func (c *client) Category() *category.Service {
return c.category
}
// Payee returns payee.Service API instance
func (c *client) Payee() *payee.Service {
return c.payee
}
// Month returns month.Service API instance
func (c *client) Month() *month.Service {
return c.month
}
// Transaction returns transaction.Service API instance
func (c *client) Transaction() *transaction.Service {
return c.transaction
}
// GET sends a GET request to the YNAB API
func (c *client) GET(url string, responseModel interface{}) error {
return c.do(http.MethodGet, url, responseModel, nil)
}
// POST sends a POST request to the YNAB API
func (c *client) POST(url string, responseModel interface{}, requestBody []byte) error {
return c.do(http.MethodPost, url, responseModel, requestBody)
}
// PUT sends a PUT request to the YNAB API
func (c *client) PUT(url string, responseModel interface{}, requestBody []byte) error {
return c.do(http.MethodPut, url, responseModel, requestBody)
}
// PATCH sends a PATCH request to the YNAB API
func (c *client) PATCH(url string, responseModel interface{}, requestBody []byte) error {
return c.do(http.MethodPatch, url, responseModel, requestBody)
}
// DELETE sends a DELETE request to the YNAB API
func (c *client) DELETE(url string, responseModel interface{}) error {
return c.do(http.MethodDelete, url, responseModel, nil)
}
// do sends a request to the YNAB API
func (c *client) do(method, url string, responseModel interface{}, requestBody []byte) error {
fullURL := fmt.Sprintf("%s%s", apiEndpoint, url)
req, err := http.NewRequest(method, fullURL, bytes.NewBuffer(requestBody))
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
if method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch {
req.Header.Set("Content-Type", "application/json")
}
res, err := c.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode >= 400 {
response := struct {
Error *api.Error `json:"error"`
}{}
if err := json.Unmarshal(body, &response); err != nil {
// returns a forged *api.Error fore ease of use
// because either the response body is empty or the response is
// non compliant with YNAB's API specification
// https://api.youneedabudget.com/#errors
apiError := &api.Error{
ID: strconv.Itoa(res.StatusCode),
Name: "unknown_api_error",
Detail: "Unknown API error",
}
return apiError
}
return response.Error
}
return json.Unmarshal(body, &responseModel)
}