-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempgetdata.js
More file actions
84 lines (75 loc) · 2.47 KB
/
Copy pathtempgetdata.js
File metadata and controls
84 lines (75 loc) · 2.47 KB
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
//get your country code - KR
const getCountryCode = async () => {
const response = await fetch('https://ipinfo.io/json?token=c76190236a0eea')
if (response.status === 200) {
const location = await response.json()
return location.country
} else {
throw new Error('Unable to get your location')
}
}
//get general currency code with my country code - KRW
const getCurrencyCode = async (countryCode) => {
const response = await fetch('https://restcountries.eu/rest/v2/all')
if (response.status === 200) {
const data = await response.json()
const country = data.find((country) => country.alpha2Code === countryCode)
return country.currencies[0].code
} else {
throw new Error('Unable to get the currency code')
}
}
//get currency rates from currencyCode - 1,122.13
const getCurrencyRate = async (currencyCode) => {
const response = await fetch('https://open.exchangerate-api.com/v6/latest')
if (response.status === 200) {
const data = await response.json()
return data.rates[currencyCode]
} else {
throw new Error('Unable to get the currency rate')
}
}
const alphavantagerequestUrl = 'https://www.alphavantage.co/query?';
const apiKey = 'ZO8S591P8HTYI8LV'
const symbols = ['AAPL', 'MSFT', 'GOOG', 'GOOGL'];
const getInfoData = async (symbols) => {
let values = [];
symbols.forEach( (value, index, array) => {
let symbol = value;
let queryParams = `function=OVERVIEW&symbol=${symbol}&apikey=${apiKey}` ;
let url = alphavantagerequestUrl + queryParams ;
let response = await fetch(url);
if (response.status === 200) {
let data = await response.json()
return data.rates[currencyCode]
values.push()
} else {
throw new Error('Unable to get the currency rate')
}
})
//return
}
//Three functions assemble
const getCurrency = async () => {
const countryCode = await getCountryCode()
const currencyCode = await getCurrencyCode(countryCode)
const currencyRate = await getCurrencyRate(currencyCode)
return currencyRate
}
/////
//to get my country's currency code
getCountryCode().then((data) => {
return getCurrencyCode(data)
}).then((data) => {
console.log(data)
}).catch((err) => {
console.log(err)
})
// KRW
//to get my country's currency
getCurrency().then((data) => {
console.log(data)
}).catch((err) => {
console.log(err)
})
//1122.13