| 6 mayo 2013 | Comentarios (0)

I like to be outdoors when life allows so weather is always an interest for me. So i was very pleases to see forecast.io becoming available with global coverage after being jealous of the US only “DarkSkys” app. It was also nice to see that there is a nice REST api for the service which I could play with.

Unfortunately there was only a textual description of service, nothing I could consume with the Java client tools I have been working with. But I though it would be an interesting experiment to try to document an existing service from scratch.

The first thing I did was to get an example of the data that the service would respond with and run it through a JSON->JSON Schema tool. I used JSONSchema.net which gave me a rough v3 JSON Schema; but it wasn’t able to take into account elements that are reused. A little bit of hand editing later I ended up with three JSON Schema files, one for the forecast itself and one for a list of weather details and and one for each particular data point.

From there is was pretty simple to read the developer documentation and write up a simple WADL to describe the two resources provided by the service. Luckily Jason LaPort from the DarkSkys was on hand to fix a few mistake and generally verify my description.

Once I had this all in place I of course found a couple of bugs in my own code, so it was time to update and fix wadl.java.net and release a 1.1.5 version with a revised depdency on the jsonschema2pojoproject. This is why a little bit of adhoc expert testing pay dividends.

So taking the WADL from github it is possible to generate a nice clean Java API using the wadl.java.netgenerator. I won’t reproduce the steps to generate the client here, but this is the end result and the kind of code you can get out of it.

<br />
// Create a Jersey client and setup for POJO JSON mapping</p>
<p>    ClientConfig cc = new DefaultClientConfig();<br />
    cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);<br />
    Client c = Client.create(cc);</p>
<p>    // Get current weather for location and check for any alerts</p>
<p>    Forecast f = ApiForecastIo_Forecast<br />
        .apikey(c, &quot;xxxxxxx&quot;)                  // Get own key from developer.forecast.io<br />
        .latitudeLongitude(51.32d, -1.244d)    // Some location near where I live<br />
           .getAsForecast(<br />
              null,                            // Not JSONP<br />
              Units.SI,                        // Return values in SI units<br />
              &quot;hourly,minutely,daily,flags&quot;);  // Just return current weather and any alerts</p>
<p>    // Output a summary</p>
<p>    System.out.println(<br />
        f.getCurrently().getSummary() + &quot; &quot; +<br />
        f.getCurrently().getTemperature() + &quot; C&quot;);</p>
<p>    // Output any alerts</p>
<p>    List&lt;Alert&gt; alerts = f.getAlerts();<br />
    for (Alert altert : alerts) {<br />
        System.out.println(<br />
            altert.getTitle() + &quot; &quot; + altert.getUri());<br />
    }<br />

The nice thing here was just how pleasantly easy describing a service that already exists was, yes I could have written it by hand in Java; but then when I wanted to move to Jersey 2.x I would have to re-write by hand rather that just regenerating from the metadata to a new technology target. (Also did the same for a BBC REST Schedule API, that is in the same git project)

Comments are closed.

Uso de cookies

Este sitio web utiliza cookies para que usted tenga la mejor experiencia de usuario. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.plugin cookies