Skip to content

Instantly share code, notes, and snippets.

@IndiceeCoder
Last active September 26, 2015 11:58
Show Gist options
  • Save IndiceeCoder/1093936 to your computer and use it in GitHub Desktop.
Save IndiceeCoder/1093936 to your computer and use it in GitHub Desktop.
Java Example - Print first 100 rows of data from first report
package com.indicee.api.examples;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.w3c.dom.Document;
public class ShowReport {
private static final String HOST = "https://secure.indicee.com";
private static final String CONSUMER_KEY = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
private static final String CONSUMER_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
private static final String TOKEN_KEY = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
private static final String TOKEN_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
private static final String REPORT_IQN = "iqn.2://indicee.com/{user_id}/{space_id}/report/{report_uuid}";
public static void main(String[] args) throws Exception {
CommonsHttpOAuthConsumer commonsHttpConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
commonsHttpConsumer.setTokenWithSecret(TOKEN_KEY, TOKEN_SECRET);
String path = "/api/reports/v3?window=0,0,100,100&qname=" + REPORT_IQN;
DefaultHttpClient httpClient = createHttpClient();
HttpGet httpGet = new HttpGet(HOST + path);
commonsHttpConsumer.sign(httpGet);
// send the data to get the response
HttpResponse httpResponse = httpClient.execute(httpGet);
int status = httpResponse.getStatusLine().getStatusCode();
if (status != 200) {
throw new Exception("Error making call: " + path);
}
InputStream is = httpResponse.getEntity().getContent();
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
documentToStream(document, System.out);
}
private static DefaultHttpClient createHttpClient() throws Exception {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
try {
SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getDefault(),
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", 443, sf));
} catch (NoSuchAlgorithmException e) {
throw new Exception("Problem creating ssl connection", e);
}
ClientConnectionManager conman = new SingleClientConnManager(registry);
DefaultHttpClient httpClient = new DefaultHttpClient(conman);
return httpClient;
}
private static void documentToStream(Document document, OutputStream outputStream) {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(2));
Result result = new StreamResult(outputStream);
Source source = new DOMSource(document);
transformer.transform(source, result);
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment