Created
March 5, 2015 03:56
-
-
Save TheRyanBurke/e6de6dbda05ec4e7808e to your computer and use it in GitHub Desktop.
Java example ThingFabric API interaction
This file contains 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
package com.twolemetry.app.sampleapiinterface; | |
import java.io.IOException; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import com.mashape.unirest.http.HttpResponse; | |
import com.mashape.unirest.http.JsonNode; | |
import com.mashape.unirest.http.Unirest; | |
import com.mashape.unirest.http.exceptions.UnirestException; | |
/** | |
* Basic example of using Java HTTP requests to interact with ThingFabric API | |
* | |
*/ | |
public class App | |
{ | |
private static final String uri = "https://q.thingfabric.com/3/sql"; | |
private static final String username = "YOUR USERNAME"; | |
private static final String password = "YOUR PASSWORD"; | |
private static final String project = "YOUR PROJECT ID"; | |
public static void main( String[] args ) throws IOException | |
{ | |
System.out.println( "Starting ThingFabric API Wrapper Example" ); | |
try { | |
// get list of rules | |
HttpResponse<JsonNode> response = getRules(); | |
System.out.println(response.getBody()); | |
// add a new rule | |
response = createRule(); | |
System.out.println(response.getBody()); | |
// see updated list of rules | |
response = getRules(); | |
System.out.println(response.getBody()); | |
} catch (UnirestException ue) { | |
System.out.println("caught unirest error"); | |
System.out.println(ue.toString()); | |
} | |
Unirest.shutdown(); | |
} | |
public static HttpResponse<JsonNode> getRules() throws UnirestException | |
{ | |
return Unirest.get(uri) | |
.basicAuth(username, password) | |
.asJson(); | |
} | |
public static HttpResponse<JsonNode> createRule() throws UnirestException | |
{ | |
JSONObject obj = new JSONObject(); | |
obj.put("query", "SELECT * FROM '".concat(project).concat("/sensors'")); | |
JSONArray integrations = new JSONArray(); | |
JSONObject integration = new JSONObject(); | |
integration.put("type", "http"); | |
integration.put("url", "http://myurl.com"); | |
integration.put("method", "PUT"); | |
integrations.add(integration); | |
obj.put("integrations", integrations); | |
return Unirest.post(uri) | |
.header("Content-type", "application/json") | |
.basicAuth(username, password) | |
.body(obj.toJSONString()) | |
.asJson(); | |
} | |
} |
This file contains 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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.twolemetry.app</groupId> | |
<artifactId>sampleapiinterface</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>sampleapiinterface</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.mashape.unirest</groupId> | |
<artifactId>unirest-java</artifactId> | |
<version>1.4.5</version> | |
</dependency> | |
<dependency> | |
<groupId>com.googlecode.json-simple</groupId> | |
<artifactId>json-simple</artifactId> | |
<version>1.1.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment