Servicios web
Ya hemos visto como crearnos nuestro propio servicio web, con el API que hemos puesto pública para utilizar desde nuestros javascript y también para que terceras personas puedan interactuar con nuestra web. En esta sección vamos a ver como consumir servicios webs.
Vamos a utilizar el API de twitter, para hacer consultas y rellenar nuestra base de datos en función de algún termino. Después utilizaremos el buscador que ya tenemos implementado para buscar entre esos tweets.
app/controllers/Timeline.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static void searchInTwitter(String query){
if(query != null && !query.isEmpty()){
JsonObject json = WS.url(“http://search.twitter.com/search.json?q=”+query).get().getJson().getAsJsonObject();
JsonArray results = json.get(“results”).getAsJsonArray();
<span class="k">for</span><span class="o">(</span><span class="n">JsonElement</span> <span class="n">item</span> <span class="o">:</span> <span class="n">results</span><span class="o">){</span>
<span class="n">JsonObject</span> <span class="n">itemObject</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="na">getAsJsonObject</span><span class="o">();</span>
<span class="n">String</span> <span class="n">msg</span> <span class="o">=</span> <span class="n">itemObject</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"text"</span><span class="o">).</span><span class="na">getAsString</span><span class="o">();</span>
<span class="n">String</span> <span class="n">user</span> <span class="o">=</span> <span class="n">itemObject</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"from_user"</span><span class="o">).</span><span class="na">getAsString</span><span class="o">();</span>
<span class="n">createTweetFromTwitter</span><span class="o">(</span><span class="n">user</span><span class="o">,</span> <span class="n">msg</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="n">search</span><span class="o">(</span><span class="n">query</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">createTweetFromTwitter</span><span class="o">(</span><span class="n">String</span> <span class="n">username</span><span class="o">,</span> <span class="n">String</span> <span class="n">msg</span><span class="o">){</span>
<span class="n">User</span> <span class="n">user</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="na">find</span><span class="o">(</span><span class="s">"byUsername"</span><span class="o">,</span> <span class="n">username</span><span class="o">).</span><span class="na">first</span><span class="o">();</span>
<span class="k">if</span><span class="o">(</span><span class="n">user</span> <span class="o">==</span> <span class="kc">null</span><span class="o">){</span>
<span class="n">user</span> <span class="o">=</span> <span class="k">new</span> <span class="n">User</span><span class="o">(</span><span class="n">username</span><span class="o">,</span> <span class="n">username</span><span class="o">);</span>
<span class="n">user</span><span class="o">.</span><span class="na">save</span><span class="o">();</span>
<span class="o">}</span>
<span class="n">Tweet</span> <span class="n">t</span> <span class="o">=</span> <span class="n">Tweet</span><span class="o">.</span><span class="na">create</span><span class="o">(</span><span class="n">msg</span><span class="o">,</span> <span class="n">user</span><span class="o">);</span>
<span class="n">t</span><span class="o">.</span><span class="na">create</span><span class="o">();</span>
<span class="o">}</span><span class="w">
app/views/search.html
1
2
3
4
5
<h3>Buscar en Twitter <small>(y almacenarlos en la base de datos)</small></h3>
{form @Timeline.searchInTwitter()}
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"query"</span> <span class="na">value=</span><span class="s">"${query}"</span> <span class="na">class=</span><span class="s">"xxlarge"</span> <span class="na">placeholder=</span><span class="s">"Busqueda"</span><span class="err">"</span><span class="nt">/></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">class=</span><span class="s">"btn primary"</span> <span class="na">value=</span><span class="s">"Buscar"</span><span class="nt">/></span>
{/form}