-
Notifications
You must be signed in to change notification settings - Fork 5
/
Episode 4.gs
142 lines (119 loc) · 3.97 KB
/
Episode 4.gs
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
const cc = DataStudioApp.createCommunityConnector();
function getAuthType() {
return cc.newAuthTypeResponse()
.setAuthType(cc.AuthType.KEY)
.setHelpUrl('https://p.nomics.com/cryptocurrency-bitcoin-api')
.build();
}
function resetAuth() {
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteProperty('dscc.key');
}
function isAuthValid() {
var userProperties = PropertiesService.getUserProperties();
var key = userProperties.getProperty('dscc.key');
var res = UrlFetchApp.fetch(`https://api.nomics.com/v1/markets?key=${key}`, { 'muteHttpExceptions': true });
return res.getResponseCode() == 200;
}
function setCredentials(request) {
var key = request.key;
var res = UrlFetchApp.fetch(`https://api.nomics.com/v1/markets?key=${key}`, { 'muteHttpExceptions': true });
if (res.getResponseCode() != 200) {
return cc.newSetCredentialsResponse()
.setIsValid(false)
.build();
} else {
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('dscc.key', key);
return cc.newSetCredentialsResponse()
.setIsValid(true)
.build();
}
}
function getConfig() {
const config = cc.getConfig();
const option1 = config.newOptionBuilder()
.setLabel('U.S. Dollars (USD)')
.setValue("USD");
const option2 = config.newOptionBuilder()
.setLabel('Euros (EUR)')
.setValue("EUR");
const option3 = config.newOptionBuilder()
.setLabel('Japanese Yen (JPY)')
.setValue("JPY");
const option4 = config.newOptionBuilder()
.setLabel('Pound Sterling (GBP)')
.setValue("GBP");
config.newSelectSingle()
.setId('currency')
.setName('Please Select a Currency to Quote the Ticker Prices')
.addOption(option1)
.addOption(option2)
.addOption(option3)
.addOption(option4);
config.newInfo()
.setText('To learn more about currencies please go here: https://en.wikipedia.org/wiki/Currency');
config.setDateRangeRequired(true);
return config.build();
}
function getFields() {
const fields = cc.getFields();
fields.newDimension()
.setId('date')
.setName('Date')
.setDescription('The Date at Midnight')
.setType(cc.FieldType.YEAR_MONTH_DAY);
fields.newDimension()
.setId('ticker')
.setName('Ticker Symbol')
.setDescription('The Ticker Symbol')
.setType(cc.FieldType.TEXT);
fields.newMetric()
.setId('prices')
.setName('Prices')
.setDescription('The Price of the Cryptocurrency')
.setType(cc.FieldType.NUMBER);
return fields;
}
function getSchema() {
return cc.newGetSchemaResponse()
.setFields(getFields())
.build();
}
function getData(request) {
const key = PropertiesService.getUserProperties().getProperty('dscc.key');
const ids = request.dimensionsFilters != null ? request.dimensionsFilters[0][0].values.join() : 'BTC,ETH,DOGE';
const start = request.dateRange.startDate;
const end = request.dateRange.endDate;
const currency = request.configParams != null ? request.configParams['currency'] : 'USD';
let res = UrlFetchApp.fetch(`https://api.nomics.com/v1/currencies/sparkline?key=${key}&ids=${ids}&start=${start}T00%3A00%3A00Z&end=${end}T00%3A00%3A00Z&convert=${currency}`);
res = JSON.parse(res);
let requestedIds = request.fields.map(object => object['name']);
let data = [];
for (let coin of res) {
for (let timestamp in coin['timestamps']) {
let row = [];
for (let requestedId of requestedIds) {
switch (requestedId) {
case 'ticker':
row.push(coin['currency']);
break;
case 'date':
row.push(coin['timestamps'][timestamp].split('T')[0].replace(/-/g, ''));
break;
case 'prices':
row.push(coin['prices'][timestamp]);
break;
default:
break;
}
}
data.push(row)
}
}
let fields = getFields().forIds(requestedIds);
return cc.newGetDataResponse()
.setFields(fields)
.addAllRows(data)
.build();
}