Iris Classon
Iris Classon - In Love with Code

Weather API in five minutes

I have been working on my wardrobe application and added the weather feature a few days ago. I try to find time to finish the application, but it’s going slower than I wish it did. Last Saturday I moved to a new apartment and as I have been unpacking I have also sorted my clothes and the app would have been handy to have. Still need to find washable and small passive NFC tags so it’s faster to register laundry status on the item. If you know where I can find waterproof (washing machine proof!) tags please do let me know!

I thought I’d share with you how simple it is to get started with a weather API I came across, OpenWeatherMap. It’s free as long as calls per minute is below 60 plus Creative Commons license CC BY-SA 4.0 requirements. Double check this, as these things change with time and this post might not ;)

Let’s get started!
Create an account atOpenWeatherMap

Under API Keys you will find your default key- but you can generate more keys if you want to.

weather-api-in-five-minutes

Now simply make the calls as specified in the documentation.

The rules are simple, the majority of the calls start with:
http://api.openweathermap.org/data/2.5/
And after that follows the details, for example
forecast/daily
and then a query string
?id=524901
and they should always contain the key:
APPID= [MY KEY]

I used JSON.Net to deserialize the data, after mapping the JSON to C# objects. With the latter I cheated- I grabbed the JSON data and then selected Edit => Paste Special => Paste JSON as classes in Visual Studio and voilá- I had the classes.

weather-api-in-five-minutes6

I then use Resharper and ‘Fix naming in file’ to get the naming to use the pascal case capitalization style. Then I use Resharper again to extract the types to their own classes in a folder.

There we go- weather in five minutes.

weather-api-in-five-minutes5

Comments

Leave a comment below, or by email.
Florian Frey
11/25/2016 3:25:28 PM
or simple: curl wttr.in/your_location 
Yawar
11/26/2016 7:39:39 PM
I have to admit I got a little excited when I saw that this post's category is 'C# & F#', and then realised there was actually no F#. Well, maybe next time ;-)

Anyway, since I (found an opportunity and) brought it up, here's a tiny bit of F#: the Wind type in F# would look like this: type wind = { speed : float; deg : int }.

To be fair though, C# does have 'record' or struct types too and if you wanted to optimise your JSON types you'd probably end up with the equivalent C# public struct Wind { public float speed; public int deg }. 


Last modified on 2016-11-24

comments powered by Disqus