-
Notifications
You must be signed in to change notification settings - Fork 10
/
requisitions.go
168 lines (139 loc) · 3.83 KB
/
requisitions.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
package nordigen
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const requisitionsPath = "requisitions"
type Requisition struct {
Id string `json:"id,omitempty"`
Created time.Time `json:"created,omitempty"`
Redirect string `json:"redirect,omitempty"`
Status string `json:"status,omitempty"`
// There is an issue in the api, the status is still a string
// like in v1
//Status Status `json:"status,omitempty"`
InstitutionId string `json:"institution_id,omitempty"`
Agreement string `json:"agreement,omitempty"`
Reference string `json:"reference,omitempty"`
Accounts []string `json:"accounts,omitempty"`
UserLanguage string `json:"user_language,omitempty"`
Link string `json:"link,omitempty"`
}
type Status struct {
Short string `json:"short,omitempty"`
Long string `json:"long,omitempty"`
Description string `json:"description,omitempty"`
}
func (c Client) CreateRequisition(r Requisition) (Requisition, error) {
req := http.Request{
Method: http.MethodPost,
URL: &url.URL{
Path: strings.Join([]string{requisitionsPath, ""}, "/"),
},
}
data, err := json.Marshal(r)
if err != nil {
return Requisition{}, err
}
req.Body = io.NopCloser(bytes.NewBuffer(data))
resp, err := c.c.Do(&req)
if err != nil {
return Requisition{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Requisition{}, err
}
if resp.StatusCode != http.StatusCreated {
return Requisition{}, &APIError{resp.StatusCode, string(body), err}
}
err = json.Unmarshal(body, &r)
if err != nil {
return Requisition{}, err
}
return r, nil
}
func (c Client) GetRequisition(id string) (r Requisition, err error) {
req := http.Request{
Method: http.MethodGet,
URL: &url.URL{
Path: strings.Join([]string{requisitionsPath, id, ""}, "/"),
},
}
resp, err := c.c.Do(&req)
if err != nil {
return Requisition{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Requisition{}, err
}
if resp.StatusCode != http.StatusOK {
return Requisition{}, &APIError{resp.StatusCode, string(body), err}
}
err = json.Unmarshal(body, &r)
if err != nil {
return Requisition{}, err
}
return r, nil
}
func (c Client) ListRequisitions() (rs []Requisition, err error) {
url := &url.URL{
Path: strings.Join([]string{requisitionsPath, ""}, "/"),
}
err = c.fetchRequisitions(url, &rs)
if err != nil {
return []Requisition{}, err
}
return rs, nil
}
type requisitionsResponse struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []Requisition `json:"results"`
}
// fetchRequisitions recursively
func (c Client) fetchRequisitions(u *url.URL, allRequisitions *[]Requisition) error {
req := http.Request{
Method: http.MethodGet,
URL: u,
}
resp, err := c.c.Do(&req)
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return &APIError{resp.StatusCode, string(body), err}
}
if resp.StatusCode != http.StatusOK {
return &APIError{resp.StatusCode, string(body), err}
}
var requisitions requisitionsResponse
err = json.Unmarshal(body, &requisitions)
if err != nil {
return &APIError{resp.StatusCode, string(body), err}
}
*allRequisitions = append(*allRequisitions, requisitions.Results...)
// If there is a next URL, make a recursive call
if requisitions.Next != "" {
next, err := url.Parse(requisitions.Next)
if err != nil {
panic("requisitions pagination url is invalid")
}
// The client expects the URL to be just the path, mangle it here and
// append the query we got from the pagination URL.
return c.fetchRequisitions(&url.URL{
Path: strings.Join([]string{requisitionsPath, ""}, "/"),
RawQuery: next.RawQuery,
}, allRequisitions)
}
return nil
}