-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaller.go
59 lines (47 loc) · 1.1 KB
/
caller.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
package main
import (
"fmt"
"os"
)
type Latest struct {
Timestamp int64 `json:"timestamp"`
Rates map[string]any `json:"rates"`
}
func checkForForce() bool {
if len(os.Args) > 1 && (os.Args[1] == "--force" || os.Args[1] == "-f") {
return true
}
return false
}
func caller(now int64) (map[string]float64, error) {
rates := make(map[string]float64)
cache, cacheErr := useCache(now, checkForForce())
if cacheErr == nil {
rates = castRateFromLatest(cache)
return rates, nil
}
api, apiErr := useApi()
if apiErr == nil {
rates = castRateFromLatest(api)
return rates, nil
}
forcedCache, forcedCacheErr := useCache(now, true)
if forcedCacheErr == nil {
rates = castRateFromLatest(forcedCache)
return rates, nil
}
return rates, forcedCacheErr
}
func castRateFromLatest(latestData Latest) map[string]float64 {
rates := make(map[string]float64)
for key, value := range latestData.Rates {
// Cast `any` type to `float64`
rate, ok := value.(float64)
if !ok {
fmt.Println("Error: Unable to convert rate to float64")
os.Exit(1)
}
rates[key] = rate
}
return rates
}