-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinstitutions.go
85 lines (71 loc) · 1.74 KB
/
institutions.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
package nordigen
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const institutionsPath = "institutions"
const countryParam = "country"
type Institution struct {
Id string `json:"id"`
Name string `json:"name"`
Bic string `json:"bic"`
TransactionTotalDays string `json:"transaction_total_days"`
Countries []string `json:"countries"`
Logo string `json:"logo"`
}
func (c Client) ListInstitutions(country string) ([]Institution, error) {
req := http.Request{
Method: http.MethodGet,
URL: &url.URL{
Path: strings.Join([]string{institutionsPath, ""}, "/"),
},
}
q := req.URL.Query()
q.Add(countryParam, country)
req.URL.RawQuery = q.Encode()
resp, err := c.c.Do(&req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, &APIError{resp.StatusCode, string(body), err}
}
list := make([]Institution, 0)
err = json.Unmarshal(body, &list)
if err != nil {
return nil, err
}
return list, nil
}
func (c Client) GetInstitution(institutionID string) (Institution, error) {
req := http.Request{
Method: http.MethodGet,
URL: &url.URL{
Path: strings.Join([]string{institutionsPath, institutionID, ""}, "/"),
},
}
resp, err := c.c.Do(&req)
if err != nil {
return Institution{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Institution{}, err
}
if resp.StatusCode != http.StatusOK {
return Institution{}, &APIError{resp.StatusCode, string(body), err}
}
insttn := Institution{}
err = json.Unmarshal(body, &insttn)
if err != nil {
return Institution{}, err
}
return insttn, nil
}