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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
// Package accounted is a small client for the Accounted v1 REST API,
// covering what is needed to book vouchers: fiscal periods, draft journal
// entries and commit.
package accounted
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// Money is an amount in öre. It marshals as a JSON number with two decimals
// so no float rounding happens on the way out.
type Money int64
func (m Money) String() string {
sign := ""
if m < 0 {
sign, m = "-", -m
}
return fmt.Sprintf("%s%d.%02d", sign, m/100, m%100)
}
func (m Money) MarshalJSON() ([]byte, error) {
return []byte(m.String()), nil
}
type Line struct {
Account string `json:"account_number"`
Debit Money `json:"debit_amount"`
Credit Money `json:"credit_amount"`
Description string `json:"line_description,omitempty"`
TaxCode string `json:"tax_code,omitempty"`
Dimensions map[string]string `json:"dimensions,omitempty"`
}
type JournalEntry struct {
FiscalPeriodID string `json:"fiscal_period_id"`
EntryDate string `json:"entry_date"`
Description string `json:"description"`
SourceType string `json:"source_type"`
SourceID string `json:"source_id"`
VoucherSeries string `json:"voucher_series,omitempty"`
Notes string `json:"notes,omitempty"`
Lines []Line `json:"lines"`
}
type FiscalPeriod struct {
ID string `json:"id"`
Name string `json:"name"`
PeriodStart string `json:"period_start"`
PeriodEnd string `json:"period_end"`
IsClosed bool `json:"is_closed"`
LockedAt *string `json:"locked_at"`
}
type Committed struct {
ID string `json:"id"`
VoucherSeries string `json:"voucher_series"`
VoucherNumber int `json:"voucher_number"`
Status string `json:"status"`
}
// APIError is the v1 error envelope.
type APIError struct {
Status int `json:"-"`
Code string `json:"code"`
Message string `json:"message_en"`
MessageSV string `json:"message"`
Hint string `json:"recovery_hint"`
Details json.RawMessage `json:"details"`
RequestID string `json:"request_id"`
}
func (e *APIError) Error() string {
msg := e.Message
if msg == "" {
msg = e.MessageSV
}
s := fmt.Sprintf("%s (HTTP %d): %s", e.Code, e.Status, msg)
if len(e.Details) > 0 && string(e.Details) != "null" {
s += " " + string(e.Details)
}
if e.RequestID != "" {
s += " [" + e.RequestID + "]"
}
return s
}
type Client struct {
BaseURL string
APIKey string
CompanyID string
HTTP *http.Client
// Interval is the minimum time between requests. The default API key
// limit is 100 requests per minute.
Interval time.Duration
MaxRetries int
last time.Time
}
func NewClient(baseURL, apiKey, companyID string) *Client {
return &Client{
BaseURL: strings.TrimRight(baseURL, "/"),
APIKey: apiKey,
CompanyID: companyID,
HTTP: &http.Client{Timeout: 60 * time.Second},
Interval: 700 * time.Millisecond,
MaxRetries: 5,
}
}
func (c *Client) FiscalPeriods(ctx context.Context) ([]FiscalPeriod, error) {
var out struct {
FiscalPeriods []FiscalPeriod `json:"fiscal_periods"`
}
err := c.do(ctx, http.MethodGet, c.companyPath("fiscal-periods"), nil, "", false, &out)
return out.FiscalPeriods, err
}
// CreateDraft creates a draft journal entry and returns its id. In dry-run
// mode the server validates the entry and nothing is stored; the id is empty.
func (c *Client) CreateDraft(ctx context.Context, e JournalEntry, idempotencyKey string, dryRun bool) (string, error) {
var out struct {
ID string `json:"id"`
}
err := c.do(ctx, http.MethodPost, c.companyPath("journal-entries"), e, idempotencyKey, dryRun, &out)
return out.ID, err
}
// Commit posts a draft and assigns its voucher number.
func (c *Client) Commit(ctx context.Context, id, idempotencyKey string) (Committed, error) {
var out Committed
err := c.do(ctx, http.MethodPost, c.companyPath("journal-entries/"+url.PathEscape(id)+"/commit"), struct{}{}, idempotencyKey, false, &out)
return out, err
}
func (c *Client) companyPath(p string) string {
return "/api/v1/companies/" + url.PathEscape(c.CompanyID) + "/" + p
}
func (c *Client) do(ctx context.Context, method, path string, body any, idempotencyKey string, dryRun bool, out any) error {
var payload []byte
if body != nil {
var err error
if payload, err = json.Marshal(body); err != nil {
return err
}
}
u := c.BaseURL + path
if dryRun {
u += "?dry_run=true"
}
for attempt := 0; ; attempt++ {
if err := c.wait(ctx); err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, method, u, bytes.NewReader(payload))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.APIKey)
req.Header.Set("Accept", "application/json")
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
// The same key is reused on every retry, so a request that reached
// the server before the connection failed is replayed, not repeated.
if idempotencyKey != "" {
req.Header.Set("Idempotency-Key", idempotencyKey)
}
resp, err := c.HTTP.Do(req)
var delay time.Duration
if err == nil {
retry, d, rerr := c.handle(resp, dryRun, out)
if !retry {
return rerr
}
err, delay = rerr, d
}
if attempt >= c.MaxRetries || ctx.Err() != nil {
return err
}
if delay == 0 {
delay = time.Second << attempt
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(delay):
}
}
}
func (c *Client) handle(resp *http.Response, dryRun bool, out any) (retry bool, delay time.Duration, err error) {
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return true, 0, err
}
if resp.StatusCode >= 300 {
var env struct {
Error *APIError `json:"error"`
}
apiErr := &APIError{Code: "HTTP_ERROR", Message: strings.TrimSpace(string(raw))}
if json.Unmarshal(raw, &env) == nil && env.Error != nil {
apiErr = env.Error
}
apiErr.Status = resp.StatusCode
if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode >= 500 {
if s, perr := strconv.Atoi(resp.Header.Get("Retry-After")); perr == nil {
delay = time.Duration(s) * time.Second
}
return true, delay, apiErr
}
return false, 0, apiErr
}
if dryRun || out == nil {
return false, 0, nil
}
var env struct {
Data json.RawMessage `json:"data"`
}
if err := json.Unmarshal(raw, &env); err != nil {
return false, 0, fmt.Errorf("decode response: %w", err)
}
if err := json.Unmarshal(env.Data, out); err != nil {
return false, 0, fmt.Errorf("decode response data: %w", err)
}
return false, 0, nil
}
func (c *Client) wait(ctx context.Context) error {
if d := time.Until(c.last.Add(c.Interval)); d > 0 {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(d):
}
}
c.last = time.Now()
return nil
}
// IsCode reports whether err is an APIError with the given code.
func IsCode(err error, code string) bool {
var e *APIError
return errors.As(err, &e) && e.Code == code
}
|