-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
43 lines (38 loc) · 869 Bytes
/
query.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
package main
import (
"./models"
"encoding/json"
"fmt"
"net/http"
"sync/atomic"
"time"
)
var (
value atomic.Value
)
func GetExchangeRatesFromPrivatBankAsJSON(){
ticker := time.NewTicker(5 * time.Minute)
for _ = range ticker.C {
var (
rate []models.Rate
respJSON []*models.RespJSON
)
resp, err := http.Get("https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5")
if err != nil {
fmt.Println("http get error:", err)
continue
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
if err := json.NewDecoder(resp.Body).Decode(&respJSON); err != nil{
fmt.Println("error with decode:", err)
continue
}
}
for _, obj := range respJSON {
rate = append(rate, models.Rate{Сcy: obj.Ccy, Base_ccy: obj.Base_ccy, Buy: obj.Buy, Sale: obj.Sale})
}
value.Store(rate)
fmt.Printf("%+v\n", rate)
}
}