Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample BIRT Script to Call REST API
logger = java.util.logging.Logger.getLogger("birt.report.logger");
importPackage(Packages.java.io);
importPackage(Packages.java.net);
//ini perintah untuk logging ya, karena kebetulan tidak ada fitur debug
logger.info('Selesai import package...');
var HTTP_POST = "POST";
var HTTP_GET = "GET";
var SAMPLE_API = "http://localhost:1081";
var BEARER = "Bearer ";
var AUTHORIZATION = "Authorization";
var TOKEN = "token";
var HTTP_CONTENT_TYPE = "Content-Type";
var APPLICATION_JSON = "application/json";
var UTF_8 = "UTF-8";
//ini perintah untuk get parameter token melalui URL, ?token=xxxxx
var pToken = params['token'];
logger.info('pToken = ' + pToken);
//base function untuk mendapatkan response dari URL REST API
function subscribe(url, method, token) {
var address = new URL(url);
var urlConnection = address.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod(method);
urlConnection.setRequestProperty(HTTP_CONTENT_TYPE, APPLICATION_JSON+"; "+UTF_8);
if(token != null) {
var bearerToken = BEARER + token;
logger.info(">> bearerToken = " + bearerToken);
urlConnection.setRequestProperty(AUTHORIZATION, bearerToken);
}
urlConnection.connect();
var buffReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
var inputLine;
var response = "";
while ((line = buffReader.readLine()) != null) {
response += line;
}
buffReader.close();
urlConnection.disconnect();
return response;
}
//main function untuk memanggil API dalam bentuk array yang berasal dari JSON
function getData(){
var token = pToken;
var revenueApi = SAMPLE_API + "/api/sample/v1/get-revenue";
var response = subscribe(revenueApi, HTTP_POST, token);
logger.info('>> response = ' + response);
var json = JSON.parse(response);
return json['data'];
}
rowNo = 0;
data = getData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.