/
service.go
101 lines (86 loc) · 2.9 KB
/
service.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
// Copyright (c) 2018, Bruno M V Souza <github@b.bmvs.io>. 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 payee
import (
"fmt"
"go.bmvs.io/ynab/api"
)
// NewService facilitates the creation of a new payee service instance
func NewService(c api.ClientReader) *Service {
return &Service{c}
}
// Service wraps YNAB payee API endpoints
type Service struct {
c api.ClientReader
}
// GetPayees fetches the list of payees from a budget
// https://api.youneedabudget.com/v1#/Payees/getPayees
func (s *Service) GetPayees(budgetID string) ([]*Payee, error) {
resModel := struct {
Data struct {
Payees []*Payee `json:"payees"`
} `json:"data"`
}{}
url := fmt.Sprintf("/budgets/%s/payees", budgetID)
if err := s.c.GET(url, &resModel); err != nil {
return nil, err
}
return resModel.Data.Payees, nil
}
// GetPayee fetches a specific payee from a budget
// https://api.youneedabudget.com/v1#/Payees/getPayeeById
func (s *Service) GetPayee(budgetID, payeeID string) (*Payee, error) {
resModel := struct {
Data struct {
Payee *Payee `json:"payee"`
} `json:"data"`
}{}
url := fmt.Sprintf("/budgets/%s/payees/%s", budgetID, payeeID)
if err := s.c.GET(url, &resModel); err != nil {
return nil, err
}
return resModel.Data.Payee, nil
}
// GetPayeeLocations fetches the list of payee locations from a budget
// https://api.youneedabudget.com/v1#/Payee_Locations/getPayeeLocations
func (s *Service) GetPayeeLocations(budgetID string) ([]*Location, error) {
resModel := struct {
Data struct {
PayeeLocations []*Location `json:"payee_locations"`
} `json:"data"`
}{}
url := fmt.Sprintf("/budgets/%s/payee_locations", budgetID)
if err := s.c.GET(url, &resModel); err != nil {
return nil, err
}
return resModel.Data.PayeeLocations, nil
}
// GetPayeeLocation fetches a specific payee location from a budget
// https://api.youneedabudget.com/v1#/Payee_Locations/getPayeeLocationById
func (s *Service) GetPayeeLocation(budgetID, payeeLocationID string) (*Location, error) {
resModel := struct {
Data struct {
PayeeLocation *Location `json:"payee_location"`
} `json:"data"`
}{}
url := fmt.Sprintf("/budgets/%s/payee_locations/%s", budgetID, payeeLocationID)
if err := s.c.GET(url, &resModel); err != nil {
return nil, err
}
return resModel.Data.PayeeLocation, nil
}
// GetPayeeLocationsByPayee fetches the list of locations of a specific payee from a budget
// https://api.youneedabudget.com/v1#/Payee_Locations/getPayeeLocationsByPayee
func (s *Service) GetPayeeLocationsByPayee(budgetID, payeeID string) ([]*Location, error) {
resModel := struct {
Data struct {
PayeeLocations []*Location `json:"payee_locations"`
} `json:"data"`
}{}
url := fmt.Sprintf("/budgets/%s/payees/%s/payee_locations", budgetID, payeeID)
if err := s.c.GET(url, &resModel); err != nil {
return nil, err
}
return resModel.Data.PayeeLocations, nil
}