Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lacebark.uclient;
- import com.mashape.unirest.http.HttpResponse;
- import com.mashape.unirest.http.exceptions.UnirestException;
- import com.mashape.unirest.request.GetRequest;
- import java.util.ArrayList;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class Client
- {
- private static String base_url = "http://lpmutil.cm:4567";
- private static String username = "admin";
- private static String password = "secret_access";
- private static String template_service = "com.lacebark.utility.service.impl.TemplateService";
- public Client() {}
- public static void main(String[] args) {}
- public static int[] getUnitIds() throws UnirestException
- {
- HttpResponse<com.mashape.unirest.http.JsonNode> response = com.mashape.unirest.http.Unirest.get(base_url + "/get_unit_ids").basicAuth(username, password).asJson();
- JSONArray array = ((com.mashape.unirest.http.JsonNode)response.getBody()).getArray();
- int[] result = new int[array.length()];
- for (int i = 0; i < array.length(); i++) {
- result[i] = array.getInt(i);
- }
- return result;
- }
- public static JSONArray getUnitDetails() throws UnirestException {
- HttpResponse<com.mashape.unirest.http.JsonNode> response = com.mashape.unirest.http.Unirest.get(base_url + "/get_unit_details").basicAuth(username, password).asJson();
- JSONArray array = ((com.mashape.unirest.http.JsonNode)response.getBody()).getArray();
- return array;
- }
- public static String getElectrictyNotice(String first_name, String last_name, Double amount) throws UnirestException { String body = "";
- JSONArray args = new JSONArray();
- args.put(0, first_name);
- args.put(1, last_name);
- args.put(2, amount);
- JSONObject jo = new JSONObject();
- jo.put("cn", template_service);
- jo.put("mn", "electricalNotice");
- jo.put("args", args);
- body = jo.toString();
- HttpResponse<String> response = com.mashape.unirest.http.Unirest.post(base_url + "/api").basicAuth(username, password).field("request", body).asString();
- return (String)response.getBody();
- }
- public static String getWaterNotice(String first_name, String last_name, Double amount) throws UnirestException { String body = "";
- JSONArray args = new JSONArray();
- args.put(0, first_name);
- args.put(1, last_name);
- args.put(2, amount);
- JSONObject jo = new JSONObject();
- jo.put("cn", template_service);
- jo.put("mn", "waterNotice");
- jo.put("args", args);
- body = jo.toString();
- HttpResponse<String> response = com.mashape.unirest.http.Unirest.post(base_url + "/api").basicAuth(username, password).field("request", body).asString();
- return (String)response.getBody();
- }
- public static String getRentNotice(String first_name, String last_name, Double amount) throws UnirestException { String body = "";
- JSONArray args = new JSONArray();
- args.put(0, first_name);
- args.put(1, last_name);
- args.put(2, amount);
- JSONObject jo = new JSONObject();
- jo.put("cn", template_service);
- jo.put("mn", "rentNotice");
- jo.put("args", args);
- body = jo.toString();
- HttpResponse<String> response = com.mashape.unirest.http.Unirest.post(base_url + "/api").basicAuth(username, password).field("request", body).asString();
- return (String)response.getBody();
- }
- public static String getFinalNotice(String first_name, String last_name, Double amount) throws UnirestException { String body = "";
- JSONArray args = new JSONArray();
- args.put(0, first_name);
- args.put(1, last_name);
- args.put(2, amount);
- JSONObject jo = new JSONObject();
- jo.put("cn", template_service);
- jo.put("mn", "finalNotice");
- jo.put("args", args);
- body = jo.toString();
- HttpResponse<String> response = com.mashape.unirest.http.Unirest.post(base_url + "/api").basicAuth(username, password).field("request", body).asString();
- return (String)response.getBody();
- }
- public static double costOfUnit(int unit_id, boolean electrical, boolean rent, boolean water) throws UnirestException
- {
- int costs = 0;
- ArrayList<String> list = new ArrayList();
- if (electrical) {
- list.add("electricity_cost");
- }
- if (rent) {
- list.add("rent_cost");
- }
- if (water) {
- list.add("water_cost");
- }
- String query = String.join("+", list);
- String uid = Integer.toString(unit_id);
- HttpResponse<String> response = com.mashape.unirest.http.Unirest.get(base_url + "/unit_cost/{unit_id}").routeParam("unit_id", uid).basicAuth(username, password).queryString("str", query).asString();
- String resp = (String)response.getBody();
- return Double.valueOf(resp).doubleValue();
- }
- public static double costOfUnit(int unit_id) throws UnirestException {
- return costOfUnit(unit_id, true, true, true);
- }
- public static double costOfUnitWithLateFee(int unit_id, double fee, boolean electrical, boolean rent, boolean water) throws UnirestException
- {
- int costs = 0;
- ArrayList<String> list = new ArrayList();
- if (electrical) {
- list.add("electricity_cost");
- }
- if (rent) {
- list.add("rent_cost");
- }
- if (water) {
- list.add("water_cost");
- }
- String pquery = String.join("+", list);
- String query = "(" + pquery + ")*" + Double.toString(fee);
- String uid = Integer.toString(unit_id);
- HttpResponse<String> response = com.mashape.unirest.http.Unirest.get(base_url + "/unit_cost/{unit_id}").routeParam("unit_id", uid).basicAuth(username, password).queryString("str", query).asString();
- String resp = (String)response.getBody();
- return Double.valueOf(resp).doubleValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement