Nov 242016
 
 November 24, 2016  Posted by at 1:32 pm C# & F#, Dev Environment  Add comments

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 at OpenWeatherMap

weather-api-in-five-minutes0

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.

weather-api-in-five-minutes1

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

  2 Responses to “Weather API in five minutes”

  1. or simple: curl wttr.in/your_location

  2. 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 }.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 4 + 3 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)