Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class AsteroidsController {
//Aura enabled needs to be static
@AuraEnabled
public static List<AsteroidWrapper> getAsteroids(String fromDate, String toDate) {
DateTime startDate;
DateTime endDate;
List<AsteroidWrapper> asteroidList = new List<AsteroidWrapper>();
System.debug('***fromDate is: ' + fromDate);
System.debug('***toDate is: ' + toDate);
if(String.isBlank(fromDate) && String.isBlank(toDate))
{
//Yesterday
startDate = Date.today();
//Yesterday + 7 days
endDate = startDate.addDays(3);
}
else
{
//Locale datetime format issue- so format incoming date consistently as yyyy-MM-dd
List<String> dateParts = fromDate.split('-');
startDate = DateTime.newInstance(Integer.valueOf(dateParts[0]),Integer.valueOf(dateParts[1]),Integer.valueOf(dateParts[2]));
dateParts = toDate.split('-');
endDate = DateTime.newInstance(Integer.valueOf(dateParts[0]),Integer.valueOf(dateParts[1]),Integer.valueOf(dateParts[2]));
}
System.debug('***startDate is: ' + startDate);
System.debug('***endDate is: ' + endDate);
String endpoint = 'https://api.nasa.gov/neo/rest/v1/feed?start_date='+startDate.format('yyyy-MM-dd')+'&end_date='+endDate.format('yyyy-MM-dd')+'&api_key=REPLACE_WITH_YOUR_KEY';
System.debug('***endpoint is: ' + endpoint);
HttpRequest req = new HttpRequest();
//req.setEndpoint('https://api.nasa.gov/neo/rest/v1/feed?start_date=2016-06-06&end_date=2016-06-13&api_key=REPLACE_WITH_YOUR_KEY');
req.setEndpoint(endpoint);
req.setMethod('GET');
Http http = new Http();
HttpResponse response = http.send(req);
String resJSON = response.getBody();
//System.debug('resJSON is: ' + resJSON);
//Parse the JSON response from NASA - YAY!!!
JSONParser parser = JSON.createParser(response.getBody());
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
AsteroidData ad = (AsteroidData)parser.readValueAs(AsteroidData.class);
System.debug('AsteroidData :' + ad);
//Add to the list
if(ad.name != null)
{
AsteroidWrapper astro = new AsteroidWrapper();
astro.str_Asteroid_Name = ad.Name;
astro.str_Magnitude = ad.absolute_magnitude_h;
astro.str_Estimated_Diameter_Max_Kms = ad.estimated_diameter.kilometers.estimated_diameter_max;
astro.isAsteroidDangerous = (!ad.is_potentially_hazardous_asteroid)? 'No':'Yes';
astro.str_closest_Approach_Earth_Date = ad.close_approach_data[0].close_approach_date;
astro.str_velocity_per_sec_kms = ad.close_approach_data[0].relative_velocity.kilometers_per_second;
astro.str_misses_Earth_by_kms = ad.close_approach_data[0].miss_distance.kilometers;
astro.str_NASA_URL = ad.nasa_jpl_url;
asteroidList.add(astro);
}
}
}
}
}
//System.debug('asteroidList is:' + asteroidList);
//System.debug('asteroidList.size() is:' + asteroidList.size());
return asteroidList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.