Skip to content

Commit ab38d02

Browse files
Jeffrey BrownJeffrey Brown
Jeffrey Brown
authored and
Jeffrey Brown
committed
Donki API - CME Updated
1 parent e16da8b commit ab38d02

File tree

4 files changed

+84
-12
lines changed

4 files changed

+84
-12
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ Thumbs.db
4545

4646
# misc
4747
.env
48-
./testing
48+
apiTesting.js

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,31 @@
146146
});
147147
```
148148

149+
#### DONKI
150+
The `donki()` function provides access to the Space Weather Database of Notifications, Knowledge, and Information (DONKI). This API is particularly useful for space weather forecasters, scientists, and the general space science community.
151+
152+
The API consists of a variety of components, each providing access to different data.
153+
154+
##### Coronal Mass Ejection (CME)
155+
156+
Coronal Mass Ejections (CMEs) are large expulsions of plasma and magnetic field from the Sun's corona. This API provides data for specific CME events in a specified time period.
157+
158+
The `start_date` and `end_date` parameters are optional, and if omitted from the payload the request will default to pulling the last 30 days worth of data.
159+
160+
161+
```js
162+
const payload = {
163+
start_date: '2021-01-01', // optional //
164+
end_date: '2021-01-02', // optional //
165+
key: 'DEMO_KEY',
166+
type: 'cme'
167+
};
168+
169+
celestial.donki(payload, cmeResponse => {
170+
return cmeResponse;
171+
});
172+
```
173+
149174

150175
## Contributing
151176

lib/donki.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// import libraries //
2+
const axios = require('axios')
3+
const Joi = require('joi')
4+
5+
// import utilities //
6+
const dateUtils = require('../utils/dates.js')
7+
8+
// neows (near earth object web service) //
9+
function donki (payload, callback) {
10+
let request, start, end, key
11+
const type = payload.type
12+
13+
// donki request type is coronal mass ejection (cme) //
14+
if (type === 'cme') {
15+
16+
// define validation schema //
17+
const validationSchema = Joi.object({
18+
end_date: Joi.string().regex(/\d{4}-\d{2}-\d{2}/, { name: 'endDate' }).optional(),
19+
key: Joi.string().required(),
20+
start_date: Joi.string().regex(/\d{4}-\d{2}-\d{2}/, { name: 'startDate' }).optional(),
21+
type: Joi.string().regex(/\w{3,6}/, { name: 'requestType' }).required()
22+
})
23+
24+
key = payload.key
25+
end = payload.end_date
26+
start = payload.start_date
27+
28+
// validate the payload input //
29+
const { error } = validationSchema.validate(payload)
30+
if (error) throw error
31+
32+
// payload logic validation //
33+
if (payload.end_date && (payload.start_date > payload.end_date)) throw new Error('Start date cannot come after the end date provided')
34+
if (payload.end_date && !payload.start_date) throw new Error('Start Date must be included if using End Date')
35+
36+
// define url request parameters //
37+
let requestURLParameters = `api_key=${key}`
38+
if (start) requestURLParameters = requestURLParameters.concat(`&start_date=${payload.start_date}`)
39+
if (end) requestURLParameters = requestURLParameters.concat(`&end_date=${payload.end_date}`)
40+
41+
// define request url //
42+
request = `https://api.nasa.gov/DONKI/CME?${requestURLParameters}`
43+
}
44+
45+
// api request //
46+
return axios(request)
47+
.then((response) => {
48+
try {
49+
callback(response.data)
50+
return response.data
51+
} catch (error) {
52+
callback(response.data)
53+
return response.data
54+
}
55+
})
56+
}
57+
58+
module.exports.donki = donki

testing.js

-11
This file was deleted.

0 commit comments

Comments
 (0)