{"id":160,"date":"2016-06-26T23:46:04","date_gmt":"2016-06-27T04:46:04","guid":{"rendered":"http:\/\/www.decodingthecloud.com\/?p=160"},"modified":"2016-06-27T01:12:53","modified_gmt":"2016-06-27T06:12:53","slug":"a-simple-asteroid-detection-system-with-lightning-components-and-a-nasa-asteroids-neows-api","status":"publish","type":"post","link":"https:\/\/www.decodingthecloud.com\/?p=160","title":{"rendered":"A simple &#8220;Asteroid Detection System&#8221; with Lightning Components and a NASA Asteroids NeoWs API"},"content":{"rendered":"<p>Over the last few days, my attention has turned towards Lightning components. I have worked through some of the tutorials in the past &#8211; chiefly on Trailhead.<\/p>\n<p>I was a little rusty with it and thought why not create a simple app which makes a call out to an API and displays the results. I wanted it to be a little fun too.<br \/>\nI am a huge fan of space exploration and space in general and try to keep up with new developments there.<br \/>\nNASA has been a world leader in space exploration and now you also have private space companies doing mind boggling stuff like SpaceX.<br \/>\nMeanwhile you have ISRO from India which was the first country to successfully send an orbiter to Mars on the first attempt and without a cryogenic rocket!<\/p>\n<p>So in the coming years, be prepared to be amazed by achievements in this sector. <\/p>\n<p>NASA has a good listing of API&#8217;s related to their space exploration. You can find it here.<br \/>\n<a href=\"https:\/\/api.nasa.gov\/api.html#authentication\">NASA API&#8217;s<\/a><\/p>\n<p>One of the API&#8217;s that caught my eye was the Asteroid API which lists the Asteroids and NEOs(Near Earth objects) based on when they are closest to the Earth.<br \/>\n<a href=\"https:\/\/api.nasa.gov\/api.html#NeoWS\">https:\/\/api.nasa.gov\/api.html#NeoWS<\/a><\/p>\n<p>They have some other pretty cool API&#8217;s too including pictures from the Mars rovers. <\/p>\n<p>For this app, I wanted to achieve the following:<\/p>\n<ol>\nSimple app which utilizes Lightning components and the SLDS.<br \/>\nUse a server side controller to make a callout to a simple unauthenticated external API<br \/>\nParse the JSON response and contruct a list of results to display in the UI<br \/>\nPopulate the UI with the API results<br \/>\nAllow the user to specify inputs and refresh the UI.<\/ol>\n<p>So I went ahead and built it. I am not a Lightning expert and so this is probably not the optimal implmentation. As I continue to learn Lightning I will probably improve this.<br \/>\nThis basically displays basic information about Asteroids which are near to the Earth based on the given dates. A link is also provided for each asteroid to learn more about them. <\/p>\n<p>You can find the application here.(I have hosted it on a community).<br \/>\n<a href=\"https:\/\/richardn-dev-developer-edition.na24.force.com\/s\/\">Asteroid Detection System<\/a><\/p>\n<p>So lets go ahead and build this:<\/p>\n<p><strong>1. <\/strong>Firstly, sign up for a NASA API developer key so that you can start making call outs.<br \/>\n<a href=\"https:\/\/api.nasa.gov\/index.html#getting-started\">https:\/\/api.nasa.gov\/index.html#getting-started<\/a><\/p>\n<p><strong>2.<\/strong> Get Lightning set up in your dev org. You basically need to create a domain, roll it out to users and add the SLDS (Salesforce Lightning Design System) to the org.<br \/>\nFollow these steps if you haven&#8217;t already done so.<br \/>\n<a href=\"https:\/\/developer.salesforce.com\/trailhead\/en\/lex_dev_lc_basics\/lex_dev_lc_basics_prereqs\">https:\/\/developer.salesforce.com\/trailhead\/en\/lex_dev_lc_basics\/lex_dev_lc_basics_prereqs<\/a><\/p>\n<p><strong>Please note:<\/strong> Make a note of the static resource that you created for uploading the Lightning Design zip file. This needs to be modified in the code below. <\/p>\n<p><strong>3.<\/strong> After this you should be ready to build the app.<br \/>\nThe Developer console is the easiest place to build the app so go ahead and open it up. So go there by clicking <strong>&#8220;Your Name | Developer Console&#8221;<\/strong><\/p>\n<p><strong>4. <\/strong>Create the backend APEX classes<br \/>\nTo create a class go to:  <strong>File | New | Apex Class.<\/strong><br \/>\nFor this app, we will use APEX to make a callout to the NASA API and retrieve the list of asteroids.<br \/>\nFirstly construct the API URL in a text editor such as notepad. We need to see how the API response looks like. <\/p>\n<p>So it will be of the form:<br \/>\n<a href=\"https:\/\/api.nasa.gov\/neo\/rest\/v1\/feed?start_date=2015-09-07&#038;end_date=2015-09-08&#038;api_key=DEMO_KEY\">https:\/\/api.nasa.gov\/neo\/rest\/v1\/feed?start_date=2015-09-07&#038;end_date=2015-09-08&#038;api_key=DEMO_KEY<\/a><\/p>\n<p>Here, <em>&#8220;DEMO_KEY&#8221;<\/em> is your API key.<br \/>\nSo we can see that the API needs the start date, end date and your API key. Also note that the date format is <strong>yyyy-MM-dd<\/strong>.<\/p>\n<p>Now construct this URL and paste it in a browser address bar and hit ENTER to see the results of the API in JSON format. Review it for a bit to understand the JSON structure.<br \/>\nOur code will need to parse this response and there are quite a few options for it. A good method in my option is to read the JSON into a class structure which can then easily be processed as needed.<br \/>\nI use this website to convert my JSON response to an APEX class. Its a pretty nifty tool.<br \/>\n<a href=\"https:\/\/www.adminbooster.com\/tool\/json2apex\">https:\/\/www.adminbooster.com\/tool\/json2apex<\/a><\/p>\n<p>Just paste your JSON response and name the resulting APEX class as needed. This is the resulting code for me. <\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/calvinnr7\/08e84544947dfa780e05629344ecca04.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/calvinnr7\/08e84544947dfa780e05629344ecca04\">Gist<\/a>.<\/noscript><\/div>\n<p>You can choose what data to display but I selected the following and created a wrapper class for it.<br \/>\nNot that each class member has the &#8220;@AuraEnabled&#8221; class annotation as we are displaying this data in the Lightning Component UI. <\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/calvinnr7\/9d5550db05571e4c1c6e8c45e9805b22.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/calvinnr7\/9d5550db05571e4c1c6e8c45e9805b22\">Gist<\/a>.<\/noscript><\/div>\n<p><strong>You may ask why this wrapper class cannot be an inner class?<\/strong><br \/>\nAs of now in Lightning Components, wrapper classes cannot be inner classes. They need to be in their own standalone class and also the class members need the &#8220;@AuraEnabled&#8221; annotation. <\/p>\n<p>Now lets move on to the main APEX class itself. Here it is:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/calvinnr7\/643ac2eddba04836a5b35b012842462b.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/calvinnr7\/643ac2eddba04836a5b35b012842462b\">Gist<\/a>.<\/noscript><\/div>\n<p>This class is doing the following:<br \/>\n1. The start and end dates are the two parameters being sent in to the method in the class.<br \/>\n2. We need to format the dates as per the format expected by the API.<br \/>\n3. Next we construct the API endpoint with the dates. <strong>Make sure to add your API key here.<\/strong><br \/>\n4. A simple http GET request is made to the API.<br \/>\n5. Next we parse the API response into the JSON class structure<br \/>\n6. For each asteroid entry, a wrapper object is created, populated with the relevant data and added to the list which will be displayed in the UI. <\/p>\n<p>Now our server side set up is done. <\/p>\n<p><strong>5. <\/strong>Create the lightning Component<br \/>\nGo to  File | New | Lightning Component.<br \/>\nCreate your component. See the code below. <\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/calvinnr7\/699af9f08e78dcd4449d0851577dc054.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/calvinnr7\/699af9f08e78dcd4449d0851577dc054\">Gist<\/a>.<\/noscript><\/div>\n<p>The highlights of the code are:<br \/>\n1. Change the path of the static resource to what you have in your org in the <code><ltng:require><\/code> tag.<br \/>\n2. The component uses a slds-form as a container for the two date picker fields and the button.<br \/>\n3. The results from the Asteroids API are displayed in a table by iterating over the results using <code><aura:iteration><\/code><\/p>\n<p><strong>6. <\/strong>Create the client side controller<br \/>\nClick on &#8220;Controller&#8221; on the right hand side (Component bundle).<br \/>\nAdd the following code:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/calvinnr7\/bc0ce1cc477bd1312427d5445e999843.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/calvinnr7\/bc0ce1cc477bd1312427d5445e999843\">Gist<\/a>.<\/noscript><\/div>\n<p>These methods are invoked by the component. <\/p>\n<p><strong>7. <\/strong>Create the client side helper<br \/>\nClick on &#8220;Helper&#8221; on the right hand side (Component bundle).<br \/>\nAdd the following code:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/calvinnr7\/27ac7a30a17a1ed9dfb9680c8e52055e.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/calvinnr7\/27ac7a30a17a1ed9dfb9680c8e52055e\">Gist<\/a>.<\/noscript><\/div>\n<p>The highlights here are:<br \/>\n1. The helper takes the dates from the user input to format them. During initial load, it shows the next 3 days as default.<br \/>\n2. An Action call is made to the APEX controller to retrieve the results of the API and pass the data to the component. <\/p>\n<p><strong>8. <\/strong>Update CSS style as needed<\/p>\n<p>I had made some minor modifications to the styling. This can be tweaked as needed.<br \/>\nSince this was built as a demo I went easy on the styling \ud83d\ude42<\/p>\n<blockquote><p>.THIS .slds-input {<br \/>\n    width: 60%;<br \/>\n}<\/p>\n<p>.THIS .uiButton {<\/p>\n<p>    height: 35px;<br \/>\n    width:5%;<br \/>\n}\n<\/p><\/blockquote>\n<p><strong>9. <\/strong>Create the Application<br \/>\nGo to File | New | Lightning Application.<br \/>\nGive a suitable name and add the following code.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/calvinnr7\/2db269b6301df0056c68690999b4f36c.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/calvinnr7\/2db269b6301df0056c68690999b4f36c\">Gist<\/a>.<\/noscript><\/div>\n<p>Once again, replace the path to your static resource. Also here we are including our component in the Lightning Application. <\/p>\n<p>So there you have it. You can now preview your app or create a community and expose the app in that so that it is available publically.<br \/>\nHappy Asteroid Hunting!<\/p>\n<p><strong>Further Enhancements:<\/strong><br \/>\nAs this is a simple demo app, I have not gone tried to build out each component in detail. If I had the time I would do the following in the future.<br \/>\n1. Improve the look and feel of the app. Update CSS styling.<br \/>\n2. Try to make the callout in the client code to avoid server callout.<br \/>\n3. Add robust error handling. <\/p>\n<p>Please do add your comments and questions below. Any suggestions to improve this or do things differently are very welcome. Thanks for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the last few days, my attention has turned towards Lightning components. I have worked through some of the tutorials in the past &#8211; chiefly on Trailhead. I was a little rusty with it and thought why not create a simple app which makes a call out to an API and displays the results. I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[48,50,47,29,46,49],"_links":{"self":[{"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/160"}],"collection":[{"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":12,"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":170,"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions\/170"}],"wp:attachment":[{"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.decodingthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}