daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.lacebark.uclient;
  2.  
  3. import com.mashape.unirest.http.HttpResponse;
  4. import com.mashape.unirest.http.exceptions.UnirestException;
  5. import com.mashape.unirest.request.GetRequest;
  6. import java.util.ArrayList;
  7. import org.json.JSONArray;
  8. import org.json.JSONObject;
  9.  
  10. public class Client
  11. {
  12.   private static String base_url = "http://lpmutil.cm:4567";
  13.   private static String username = "admin";
  14.   private static String password = "secret_access";
  15.   private static String template_service = "com.lacebark.utility.service.impl.TemplateService";
  16.  
  17.   public Client() {}
  18.  
  19.   public static void main(String[] args) {}
  20.  
  21.   public static int[] getUnitIds() throws UnirestException
  22.   {
  23.     HttpResponse<com.mashape.unirest.http.JsonNode> response = com.mashape.unirest.http.Unirest.get(base_url + "/get_unit_ids").basicAuth(username, password).asJson();
  24.     JSONArray array = ((com.mashape.unirest.http.JsonNode)response.getBody()).getArray();
  25.     int[] result = new int[array.length()];
  26.     for (int i = 0; i < array.length(); i++) {
  27.       result[i] = array.getInt(i);
  28.     }
  29.     return result;
  30.   }
  31.  
  32.   public static JSONArray getUnitDetails() throws UnirestException {
  33.     HttpResponse<com.mashape.unirest.http.JsonNode> response = com.mashape.unirest.http.Unirest.get(base_url + "/get_unit_details").basicAuth(username, password).asJson();
  34.     JSONArray array = ((com.mashape.unirest.http.JsonNode)response.getBody()).getArray();
  35.     return array;
  36.   }
  37.  
  38.   public static String getElectrictyNotice(String first_name, String last_name, Double amount) throws UnirestException { String body = "";
  39.     JSONArray args = new JSONArray();
  40.     args.put(0, first_name);
  41.     args.put(1, last_name);
  42.     args.put(2, amount);
  43.     JSONObject jo = new JSONObject();
  44.     jo.put("cn", template_service);
  45.     jo.put("mn", "electricalNotice");
  46.     jo.put("args", args);
  47.    
  48.     body = jo.toString();
  49.    
  50.  
  51.  
  52.  
  53.     HttpResponse<String> response = com.mashape.unirest.http.Unirest.post(base_url + "/api").basicAuth(username, password).field("request", body).asString();
  54.     return (String)response.getBody();
  55.   }
  56.  
  57.   public static String getWaterNotice(String first_name, String last_name, Double amount) throws UnirestException { String body = "";
  58.     JSONArray args = new JSONArray();
  59.     args.put(0, first_name);
  60.     args.put(1, last_name);
  61.     args.put(2, amount);
  62.     JSONObject jo = new JSONObject();
  63.     jo.put("cn", template_service);
  64.     jo.put("mn", "waterNotice");
  65.     jo.put("args", args);
  66.    
  67.     body = jo.toString();
  68.    
  69.  
  70.  
  71.  
  72.     HttpResponse<String> response = com.mashape.unirest.http.Unirest.post(base_url + "/api").basicAuth(username, password).field("request", body).asString();
  73.     return (String)response.getBody();
  74.   }
  75.  
  76.   public static String getRentNotice(String first_name, String last_name, Double amount) throws UnirestException { String body = "";
  77.     JSONArray args = new JSONArray();
  78.     args.put(0, first_name);
  79.     args.put(1, last_name);
  80.     args.put(2, amount);
  81.     JSONObject jo = new JSONObject();
  82.     jo.put("cn", template_service);
  83.     jo.put("mn", "rentNotice");
  84.     jo.put("args", args);
  85.    
  86.     body = jo.toString();
  87.    
  88.  
  89.  
  90.  
  91.     HttpResponse<String> response = com.mashape.unirest.http.Unirest.post(base_url + "/api").basicAuth(username, password).field("request", body).asString();
  92.     return (String)response.getBody();
  93.   }
  94.  
  95.   public static String getFinalNotice(String first_name, String last_name, Double amount) throws UnirestException { String body = "";
  96.     JSONArray args = new JSONArray();
  97.     args.put(0, first_name);
  98.     args.put(1, last_name);
  99.     args.put(2, amount);
  100.     JSONObject jo = new JSONObject();
  101.     jo.put("cn", template_service);
  102.     jo.put("mn", "finalNotice");
  103.     jo.put("args", args);
  104.    
  105.     body = jo.toString();
  106.    
  107.  
  108.  
  109.  
  110.     HttpResponse<String> response = com.mashape.unirest.http.Unirest.post(base_url + "/api").basicAuth(username, password).field("request", body).asString();
  111.     return (String)response.getBody();
  112.   }
  113.  
  114.   public static double costOfUnit(int unit_id, boolean electrical, boolean rent, boolean water) throws UnirestException
  115.   {
  116.     int costs = 0;
  117.     ArrayList<String> list = new ArrayList();
  118.     if (electrical) {
  119.       list.add("electricity_cost");
  120.     }
  121.     if (rent) {
  122.       list.add("rent_cost");
  123.     }
  124.     if (water) {
  125.       list.add("water_cost");
  126.     }
  127.     String query = String.join("+", list);
  128.    
  129.     String uid = Integer.toString(unit_id);
  130.    
  131.  
  132.  
  133.  
  134.     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();
  135.     String resp = (String)response.getBody();
  136.     return Double.valueOf(resp).doubleValue();
  137.   }
  138.  
  139.   public static double costOfUnit(int unit_id) throws UnirestException {
  140.     return costOfUnit(unit_id, true, true, true);
  141.   }
  142.  
  143.   public static double costOfUnitWithLateFee(int unit_id, double fee, boolean electrical, boolean rent, boolean water) throws UnirestException
  144.   {
  145.     int costs = 0;
  146.     ArrayList<String> list = new ArrayList();
  147.     if (electrical) {
  148.       list.add("electricity_cost");
  149.     }
  150.     if (rent) {
  151.       list.add("rent_cost");
  152.     }
  153.     if (water) {
  154.       list.add("water_cost");
  155.     }
  156.     String pquery = String.join("+", list);
  157.     String query = "(" + pquery + ")*" + Double.toString(fee);
  158.    
  159.     String uid = Integer.toString(unit_id);
  160.    
  161.  
  162.  
  163.  
  164.     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();
  165.     String resp = (String)response.getBody();
  166.     return Double.valueOf(resp).doubleValue();
  167.   }
  168. }
RAW Paste Data
Top