Created
December 13, 2010 21:00
-
-
Save ryanswanstrom/739599 to your computer and use it in GitHub Desktop.
This code demonstrates how to make web service calls with the Play Framework.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package controllers; | |
| import com.google.gson.JsonArray; | |
| import com.google.gson.JsonElement; | |
| import com.google.gson.JsonObject; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import org.w3c.dom.Document; | |
| import org.w3c.dom.Element; | |
| import org.w3c.dom.Node; | |
| import org.w3c.dom.NodeList; | |
| import play.libs.WS; | |
| import play.libs.WS.WSRequest; | |
| import play.mvc.Controller; | |
| /** | |
| * | |
| */ | |
| public class MashUp extends Controller { | |
| public static void getDiigoLinks(final int num) { | |
| List<String> data = new ArrayList<String>(); | |
| WS.authenticate("username", ""); | |
| WSRequest diigoRequest = WS.url("http://api2.diigo.com/bookmarks"); | |
| // get diigo bookmarks | |
| diigoRequest.setParameter("users", "swgoof"); | |
| diigoRequest.setParameter("rows", Integer.toString(num)); | |
| try { | |
| JsonElement diigoJson = diigoRequest.get().getJson(); | |
| JsonArray diigoLinks = diigoJson.getAsJsonArray(); | |
| for (int i = 0; i < diigoLinks.size(); i++) { | |
| JsonObject inner = diigoLinks.get(i).getAsJsonObject(); | |
| data.add(inner.get("title").getAsString() ); | |
| data.add(inner.get("url").getAsString()); | |
| //data.add( inner.get("user").getAsString() ); | |
| data.add( inner.get("tags").getAsString() ); | |
| } | |
| } catch (Exception ex) { | |
| data.add("Problem with the Rest call"); | |
| data.add(ex.getMessage()); | |
| } | |
| mashResult(data); | |
| } | |
| public static void postDiigoLink() { | |
| List<String> data = new ArrayList<String>(); | |
| WS.authenticate("username", ""); | |
| WSRequest diigoRequest = WS.url("http://api2.diigo.com/bookmarks"); | |
| // post bookmark | |
| diigoRequest.setParameter("title", "MongoHQ - the hosted mongodb solution"); | |
| diigoRequest.setParameter("url", "https://app.mongohq.com/home"); | |
| diigoRequest.setParameter("shared", "yes"); | |
| diigoRequest.setParameter("tags", "mongo, database, cloud_computing"); | |
| diigoRequest.setParameter("desc", "Cloud mongodb hosting. It includes a free account."); | |
| try { | |
| String resp = diigoRequest.post().getJson().getAsJsonObject().get("message").getAsString(); | |
| data.add(resp); | |
| } catch (Exception ex) { | |
| data.add("Problem with the Rest call"); | |
| data.add(ex.getMessage()); | |
| } | |
| mashResult(data); | |
| } | |
| public static void getTumblrLinks(final int num) { | |
| List<String> data = new ArrayList<String>(); | |
| WSRequest tumblrRequest = WS.url("http://gooflog.tumblr.com/api/read"); | |
| // get tumblr posts(links) | |
| tumblrRequest.setParameter("type", "link"); | |
| tumblrRequest.setParameter("num", Integer.toString(num)); | |
| try { | |
| Document doc = tumblrRequest.get().getXml(); | |
| NodeList nList = doc.getElementsByTagName("post"); // or put link-text or link-url | |
| for (int i = 0; i < nList.getLength(); i++) { | |
| Node node = nList.item(i); | |
| if (node.getNodeType() == Node.ELEMENT_NODE) { | |
| Element e = (Element)node; | |
| data.add("link-title: " + e.getElementsByTagName("link-text").item(0).getTextContent()); | |
| data.add("link url: " + e.getElementsByTagName("link-url").item(0).getTextContent()); | |
| NodeList content = e.getElementsByTagName("link-description"); | |
| if (content.getLength() > 0) { | |
| data.add("link desc: " + content.item(0).getTextContent()); | |
| } | |
| NodeList tags = e.getElementsByTagName("tag"); | |
| if (tags.getLength() > 0) { | |
| for (int t = 0; t < tags.getLength(); t++) { | |
| data.add("link tag: " + tags.item(t).getTextContent()); | |
| } | |
| } | |
| } | |
| } | |
| } catch (Exception ex) { | |
| data.add("Problem with the Rest call"); | |
| data.add(ex.getMessage()); | |
| } | |
| mashResult(data); | |
| } | |
| public static void postTumblrLink() { | |
| List<String> data = new ArrayList<String>(); | |
| WSRequest tumblrRequest = WS.url("http://www.tumblr.com/api/write"); | |
| tumblrRequest.setParameter("email", ""); | |
| tumblrRequest.setParameter("password", ""); | |
| tumblrRequest.setParameter("type", "link"); | |
| tumblrRequest.setParameter("name", "MongoHQ - the hosted mongodb solution"); | |
| tumblrRequest.setParameter("url", "https://app.mongohq.com/home"); | |
| tumblrRequest.setParameter("description", "Cloud mongodb hosting. It includes a free account."); | |
| tumblrRequest.setParameter("tags", "mongo, database,cloud_computing"); | |
| int res = tumblrRequest.post().getStatus(); | |
| data.add("response: " + res); // 201 means a good response | |
| mashResult(data); | |
| } | |
| /** | |
| * A method to use the github API. | |
| */ | |
| public static void viewGithub() { | |
| // get user info | |
| WSRequest req = WS.url("http://github.com/api/v2/json/user/show/goof"); | |
| List<String> results = new ArrayList<String>(); | |
| results.add(req.get().getString()); | |
| // get user emails, requires Auth | |
| req = WS.url("http://github.com/api/v2/xml/user/emails"); | |
| req.setParameter("login", "username"); | |
| req.setParameter("token", ""); | |
| results.add(req.get().getString()); | |
| // get users following binarylogic | |
| req = WS.url("http://github.com/api/v2/yaml/user/show/swgoof/followers"); | |
| //req.setParameter("login", "username"); | |
| //req.setParameter("token", ""); | |
| results.add(req.get().getString()); | |
| mashResult(results); | |
| } | |
| public static void mashResult(List<String> data) { | |
| render(data); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment