-
Notifications
You must be signed in to change notification settings - Fork 1
/
turkishbanks.go
101 lines (88 loc) · 2.69 KB
/
turkishbanks.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
package turkishbanks
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)
var TurkishBanksURL = "http://eftemkt.tcmb.gov.tr/bankasubelistesi/bankaSubeTumListe.xml"
type BanksBranchList struct {
XMLName xml.Name `xml:"bankaSubeTumListe"`
BankBranches []BankBranches `xml:"bankaSubeleri"`
Date string `xml:"tarih,attr"`
Xmlns string `xml:"xmlns,attr"`
}
type BankBranches struct {
XMLName xml.Name `xml:"bankaSubeleri"`
Bank Bank `xml:"banka"`
Branch []Branch `xml:"sube"`
BankCode string `xml:"bKd,attr"`
BranchSize string `xml:"sAdt,attr"`
}
type Bank struct {
XMLName xml.Name `xml:"banka"`
BankCode string `xml:"bKd"`
BankName string `xml:"bAd"`
BankCityName string `xml:"bIlAd"`
Address string `xml:"adr"`
LastUpdateType string `xml:"sonIslemTuru,attr"`
LastUpdateTime string `xml:"sonIslemZamani,attr"`
}
type Branch struct {
XMLName xml.Name `xml:"sube"`
BankCode string `xml:"bKd"`
BranchCode string `xml:"sKd"`
BranchName string `xml:"sAd"`
BranchCityCode string `xml:"sIlKd"`
BranchCityName string `xml:"sIlAd"`
BranchCountyCode string `xml:"sIlcKd"`
BranchCountyName string `xml:"sIlcAd"`
Address string `xml:"adr"`
PhoneNumber string `xml:"tlf"`
Fax string `xml:"fks"`
Email string `xml:"epst"`
LastUpdateType string `xml:"sonIslemTuru,attr"`
LastUpdateTime string `xml:"sonIslemZamani,attr"`
}
func GetBankListAsJson() ([]byte, error) {
if banks, err := GetBankList(); err != nil {
log.Printf("TurkishBanks | Failed to get XML: %v", err)
} else {
jsonObject, err := json.Marshal(banks)
if err != nil {
return nil, fmt.Errorf("TurkishBanks | Failed to parse data as json: %v", err)
}
return jsonObject, nil
}
return nil, errors.New("TurkishBanks | Method failed.")
}
func GetBankList() (BanksBranchList, error) {
var list BanksBranchList
if xmlBytes, err := getXML(TurkishBanksURL); err != nil {
log.Printf("TurkishBanks | Failed to get XML: %v", err)
} else {
err := xml.Unmarshal(xmlBytes, &list)
if err != nil {
return BanksBranchList{}, err
}
}
return list, nil
}
func getXML(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return []byte{}, fmt.Errorf("TurkishBanks | GET error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return []byte{}, fmt.Errorf("TurkishBanks | Status error: %v", resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, fmt.Errorf("TurkishBanks | Read body: %v", err)
}
return data, nil
}