-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (154 loc) · 5.64 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
"strings"
)
type CurrenciesResponse struct {
Currencies map[string]string `json:"-"`
}
type CurrenciesRequest struct {
Items []string `json:"items"`
}
type ExchangeRatesResponse struct {
Timestamp int64 `json:"timestamp"`
Base string `json:"base"`
Rates map[string]float64 `json:"rates"`
}
type ExchangeRatesRequest struct {
Timestamp int64 `json:"timestamp"`
Base string `json:"base"`
Items []CurrenciesRate `json:"items"`
}
type CurrenciesRate struct {
Code string `json:"code"`
Rate string `json:"rate"`
}
func main() {
// Parse command line arguments
datePtr := flag.String("date", "", "Specify the date in the format Y-M-d (e.g., 2023-09-14)")
envPathPtr := flag.String("env", ".env", "Specify the path to the .env file")
flag.Parse()
err := godotenv.Load(*envPathPtr)
if err != nil {
fmt.Printf(".env file not found at %s", *envPathPtr)
}
if os.Getenv("OPEN_EXCHANGE_RATES_TOKEN") == "" {
panic("OPEN_EXCHANGE_RATES_TOKEN is not specified")
}
if os.Getenv("ECONUMO_CURRENCY_BASE") == "" {
panic("ECONUMO_CURRENCY_BASE is not specified")
}
if os.Getenv("ECONUMO_API_URL") == "" {
panic("ECONUMO_API_URL is not specified")
}
if os.Getenv("ECONUMO_SYSTEM_API_KEY") == "" {
panic("ECONUMO_SYSTEM_API_KEY is not specified")
}
openExchangeRatesToken := os.Getenv("OPEN_EXCHANGE_RATES_TOKEN")
symbols := os.Getenv("OPEN_EXCHANGE_RATES_SYMBOLS")
baseSymbol := os.Getenv("ECONUMO_CURRENCY_BASE")
econumoAPIURL := os.Getenv("ECONUMO_API_URL")
econumoAPIKey := os.Getenv("ECONUMO_SYSTEM_API_KEY")
currenciesURL := fmt.Sprintf("https://openexchangerates.org/api/currencies.json?app_id=%s", openExchangeRatesToken)
respCurrencies, err := http.Get(currenciesURL)
if err != nil {
log.Fatalf("Failed to fetch currencies: %v", err)
}
if respCurrencies.StatusCode != 200 {
log.Fatalf("Failed to fetch currencies. Status code: %d", respCurrencies.StatusCode)
}
// Parse the currencies JSON response
var currenciesResp CurrenciesResponse
// Unmarshal the JSON data into the struct
err = json.NewDecoder(respCurrencies.Body).Decode(¤ciesResp.Currencies)
if err != nil {
log.Fatalf("Failed to parse currencies response: %v", err)
}
defer respCurrencies.Body.Close()
// Create a filtered map of currencies based on OPEN_EXCHANGE_RATES_SYMBOLS
var filteredCurrencies CurrenciesRequest
symbolList := strings.Split(symbols, ",")
for code, _ := range currenciesResp.Currencies {
if symbols == "" {
filteredCurrencies.Items = append(filteredCurrencies.Items, code)
} else {
for _, symbol := range symbolList {
if symbol == code {
filteredCurrencies.Items = append(filteredCurrencies.Items, code)
}
}
}
}
// Convert filteredCurrencies to JSON
currenciesJSON, err := json.Marshal(filteredCurrencies)
if err != nil {
log.Fatalf("Failed to marshal filteredCurrencies to JSON: %v", err)
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/system/import-currency-list", econumoAPIURL), strings.NewReader(string(currenciesJSON)))
if err != nil {
log.Fatalf("Failed to create request to send currencies data to econumo API: %v", err)
}
req.Header.Set("Authorization", econumoAPIKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to send currencies data to econumo API: %v", err)
}
defer resp.Body.Close()
fmt.Printf("Currencies sent to econumo API successfully: %s", string(currenciesJSON))
// Fetch currency rates from openexchangerates.org
var openExchangeRatesURL string
if *datePtr == "" {
openExchangeRatesURL = fmt.Sprintf("https://openexchangerates.org/api/latest.json?app_id=%s", openExchangeRatesToken)
} else {
openExchangeRatesURL = fmt.Sprintf("https://openexchangerates.org/api/historical/%s.json?app_id=%s", *datePtr, openExchangeRatesToken)
}
openExchangeRatesURL += fmt.Sprintf("&base=%s", baseSymbol)
if symbols != "" {
openExchangeRatesURL += fmt.Sprintf("&symbols=%s", symbols)
}
resp, err = http.Get(openExchangeRatesURL)
if err != nil {
log.Fatalf("Failed to fetch currency rates: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("Failed to fetch currency rates. Status code: %d", resp.StatusCode)
}
// Parse the currency rates JSON response
var exchangeRates ExchangeRatesResponse
err = json.NewDecoder(resp.Body).Decode(&exchangeRates)
if err != nil {
log.Fatalf("Failed to parse response: %v", err)
}
// Send currency rates data to econumo API
var exchangeRatesRequest ExchangeRatesRequest
exchangeRatesRequest.Timestamp = exchangeRates.Timestamp
exchangeRatesRequest.Base = exchangeRates.Base
for code, rate := range exchangeRates.Rates {
exchangeRatesRequest.Items = append(exchangeRatesRequest.Items, CurrenciesRate{Code: code, Rate: fmt.Sprintf("%f", rate)})
}
exchangeRatesJSON, err := json.Marshal(exchangeRatesRequest)
if err != nil {
log.Fatalf("Failed to marshal combinedData to JSON: %v", err)
}
req, err = http.NewRequest("POST", fmt.Sprintf("%s/api/v1/system/import-currency-rates", econumoAPIURL), strings.NewReader(string(exchangeRatesJSON)))
if err != nil {
log.Fatalf("Failed to create request to send data to econumo API: %v", err)
}
req.Header.Set("Authorization", econumoAPIKey)
req.Header.Set("Content-Type", "application/json")
resp, err = client.Do(req)
if err != nil {
log.Fatalf("Failed to send data to econumo API: %v", err)
}
defer resp.Body.Close()
fmt.Printf("Currency rates sent to econumo API successfully: %s", string(exchangeRatesJSON))
}