-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (73 loc) · 2.64 KB
/
index.js
File metadata and controls
78 lines (73 loc) · 2.64 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
const fetch = require("cross-fetch"); // npm install cross-fetch
const FormData = require("isomorphic-form-data"); // npm install isomorphic-form-data
require("dotenv").config();
const TOKEN_EXPIRATION_TIME = 900;
function getApplicationToken() {
return new Promise(function(resolve, reject) {
let parameters = new FormData();
parameters.append('f', 'json');
parameters.append('client_id', process.env.CLIENT_ID);
parameters.append('client_secret', process.env.CLIENT_SECRET);
parameters.append('grant_type', 'client_credentials');
parameters.append('expiration', TOKEN_EXPIRATION_TIME);
fetch("https://www.arcgis.com/sharing/rest/oauth2/token", {
method: 'POST',
body: parameters
})
.then(function(response) {
response.json()
.then(function(arcgisResponse) {
if (arcgisResponse.error) {
reject(new Error(arcgisResponse.error.message));
} else {
resolve(arcgisResponse.access_token);
}
})
.catch(function(error) {
reject(error);
})
});
})
}
function geoEnrichStudyArea(studyArea) {
return new Promise(function(resolve, reject) {
getApplicationToken()
.then(function(applicationToken) {
let parameters = new FormData();
parameters.append('f', 'json');
parameters.append('token', applicationToken);
parameters.append('studyAreas', studyArea);
fetch('https://geoenrich.arcgis.com/arcgis/rest/services/World/GeoenrichmentServer/Geoenrichment/enrich', {
method: 'POST',
body: parameters
})
.then(function(response) {
response.json()
.then(function(arcgisResponse) {
if (arcgisResponse.error) {
reject(new Error(arcgisResponse.error.message));
} else {
resolve(arcgisResponse);
}
})
.catch(function(error) {
reject(error);
})
});
}, function(error) {
reject(error);
})
.catch(function(exception) {
reject(exception);
});
})
}
geoEnrichStudyArea('[{"geometry":{"x":-117.1956,"y":34.0572}}]')
.then(function(arcgisResponse) {
console.log("Study area: " + JSON.stringify(arcgisResponse));
}, function(error) {
console.log(error.toString());
})
.catch(function(exception) {
console.log(exception.toString());
});