Andrew's Site

NASA APIs and Kotlin

June 10, 2020

The first piece of code I ever wrote was a piece of basic code that calculated your weight on different planets. My father had bought me this book, and it contained a how-to guide to build this planet weight calculator. I copied over the code to my parents computer, it compiled, and all of a sudden I had become a programmer. Of sorts.

So when I found out that NASA had an API of all exoplanets (planets outside our solar system), I figured this was the perfect thing to spend my weekend building - a new, improved planet weight calculator! I’ve been trying to learn a few new languages, and figured this was a great opportunity to learn a bit of Kotlin.

You can find the docs for this API on swagger. Click through to the planet-calculator, and throw in a query. Why does Pluto return a value when it’s not a planet you ask? Because it’s my API and I decided it could be there, that’s why! Why isn’t K2-16 on there? Despite this data being provided by NASA, not all planets had weights and diameters (the two things you need to calculate the impact of gravity on an object). A lot of planets in their database have mass of the planet described as a multiplication of the sin of the inclination of the planet - which I guess they don’t know, otherwise they’d just multiply it out. I chose not to return a result for those planets. Oddly, NASA also doesn’t provide some of the common names for exoplanets.
So you’ll need to search by the scientific name.

I do really like Kotlin’s null checking system. Definitely less bulky than using Optionals everywhere. Hopefully I’ll get a chance to use it in anger one day.