code first, ask questions later

Google Weather API with RestSharp

Guest post by DK!

The Google Weather API is a grand service for developers to get weather data for any location with ease. RestSharp is a open source .NET REST Client…

Been doing some work getting weather information from Google Weather and I thought I might take the time to explain how I used Restsharp with this API. I started with the xml from the API itself.

http://www.google.com/ig/api?weather=Brisbane+au
<xml_api_reply version="1">
    <weather>
        <forecast_information>
            <city data="Brisbane, QLD"/>
            <postal_code data="Brisbane au"/>
            <latitude_e6 data=""/>
            <longitude_e6 data=""/>
            <forecast_date data="2010-04-14"/>
            <current_date_time data="2010-04-14 14:00:00 +0000"/>
            <unit_system data="US"/>
        </forecast_information>
        <current_conditions>
            <condition data="Partly Cloudy"/>
            <temp_f data="72"/>
            <temp_c data="22"/>
            <humidity data="Humidity: 64%"/>
            <icon data="/ig/images/weather/partly_cloudy.gif"/>
            <wind_condition data="Wind: SW at 14 mph"/>
        </current_conditions>
        <forecast_conditions>
            <day_of_week data="Wed"/>
            <low data="64"/>
            <high data="78"/>
            <icon data="/ig/images/weather/chance_of_rain.gif"/>
            <condition data="Chance of Rain"/>
        </forecast_conditions>
	  ...
    </weather>
</xml_api_reply>

Notice the weather element contains multiple forecast_conditions elements without a single container element as well as the other forecast_information and current_conditions elements. At first this was a problem, I spoke to John Sheehan (the creator of RestSharp) about this and he told me it was currently unsupported with RestSharp. So I took a dive into the RestSharp source (mainly the XmlDeserialiser) to try and find a solution to this problem and I came across the support for Derived Lists, therein lies a solution…

public class xml_api_reply
{
    public string version { get; set; }
    public Weather weather { get; set; }
}
public class Weather : List<Forecast_Conditions>
{
    public Forecast_Information Forecast_Information { get; set; }
    public Current_Conditions Current_Conditions { get; set; }
}
public class DataElement
{
    public string Data { get; set; }
}
public class Forecast_Information
{
    public DataElement City { get; set; }
    public DataElement Postal_Code { get; set; }
    public DataElement Forecast_Date { get; set; }
    public DataElement Unit_System { get; set; }
}
public class Current_Conditions
{
    public DataElement Condition { get; set; }
    public DataElement Temp_c { get; set; }
    public DataElement Humidity { get; set; }
    public DataElement Icon { get; set; }
    public DataElement Wind_condition { get; set; }
}
public class Forecast_Conditions
{
    public DataElement Day_Of_Week { get; set; }
    public DataElement Condition { get; set; }
    public DataElement Low { get; set; }
    public DataElement High { get; set; }
    public DataElement Icon { get; set; }
}
These classes are then used by RestSharp to Deserialise the response. xml_api_reply is the root element and under it is weather. The weather class inherits from a List<forecast_conditions> because it can contain multiple elements as well as other properties. The DataElement class was created because of the way the xml has its data ie. <city data=”Brisbane, QLD”/>  instead of <city>Brisbane, QLD</city>.

Now that we have setup the Response classes we can use get to the real code…

var client = new RestClient("http://www.google.com/ig/api");
var request = new RestRequest(Method.GET);
request.AddParameter("weather", "Brisbane");

var response = client.Execute<Models.xml_api_reply>(request);

Pretty easy, and the response is a RestResponse<T> where T is my xml_api_reply class, this object then gives us access to anything we could need from the response including the content itself (response.Content) and the deserialised class (response.Data).

And to find the current temperature:

response.Data.weather.Current_Conditions.Temp_c.Data
Now you know how to use RestSharp the world of Restful services is yours for the taking!

Update: Updated the reponse class to Pascal Case.

blog comments powered by Disqus