Enhencing the APIClient
This patch enhence the APICLient with additional support to query parameters in getStringValue, getListStrValue. New method that will be aded, will be added with both variation, where the base comunication method: get can accept a null query parameter object. An implementation was added for getMapStrValue that retreive a list of key, value and creates a map out of it. post and delete with and without query parameters. getIntValue with query parameters. getMapListStrValue, getListInetAddressValue, getMapStringSnapshotTabularDataValue And the helper functions for the query parameter: join, set_query_param, set_bool_query_param Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
This commit is contained in:
parent
565d62d4db
commit
67abbeb060
@ -5,6 +5,7 @@ package com.cloudius.urchin.api;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@ -18,9 +19,11 @@ import javax.json.JsonReader;
|
||||
import javax.json.JsonReaderFactory;
|
||||
import javax.json.JsonString;
|
||||
import javax.management.openmbean.TabularData;
|
||||
import javax.management.openmbean.TabularDataSupport;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
|
||||
import com.cloudius.urchin.utils.SnapshotDetailsTabularData;
|
||||
import com.sun.jersey.api.client.Client;
|
||||
import com.sun.jersey.api.client.WebResource;
|
||||
import com.sun.jersey.api.client.WebResource.Builder;
|
||||
@ -46,11 +49,15 @@ public class APIClient {
|
||||
}
|
||||
|
||||
public Builder get(String path, MultivaluedMap<String, String> queryParams) {
|
||||
if (queryParams == null) {
|
||||
return get(path);
|
||||
}
|
||||
ClientConfig config = new DefaultClientConfig();
|
||||
Client client = Client.create(config);
|
||||
WebResource service = client.resource(UriBuilder.fromUri(getBaseUrl())
|
||||
.build());
|
||||
return service.queryParams(queryParams).path(path).accept(MediaType.APPLICATION_JSON);
|
||||
return service.queryParams(queryParams).path(path)
|
||||
.accept(MediaType.APPLICATION_JSON);
|
||||
}
|
||||
|
||||
public void post(String path, MultivaluedMap<String, String> queryParams) {
|
||||
@ -61,24 +68,56 @@ public class APIClient {
|
||||
get(path).post();
|
||||
}
|
||||
|
||||
public String getStringValue(String string) {
|
||||
public void post(String path) {
|
||||
post(path, null);
|
||||
}
|
||||
|
||||
public void delete(String path, MultivaluedMap<String, String> queryParams) {
|
||||
if (queryParams != null) {
|
||||
get(path, queryParams).delete();
|
||||
return;
|
||||
}
|
||||
get(path).delete();
|
||||
}
|
||||
|
||||
public void delete(String path) {
|
||||
delete(path, null);
|
||||
}
|
||||
|
||||
public String getStringValue(String string,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
if (string != "") {
|
||||
return get(string).get(String.class);
|
||||
return get(string, queryParams).get(String.class);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getStringValue(String string) {
|
||||
return getStringValue(string, null);
|
||||
}
|
||||
|
||||
public JsonReader getReader(String string,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
return factory.createReader(new StringReader(getStringValue(string,
|
||||
queryParams)));
|
||||
}
|
||||
|
||||
public JsonReader getReader(String string) {
|
||||
return factory.createReader(new StringReader(getStringValue(string)));
|
||||
return getReader(string, null);
|
||||
}
|
||||
|
||||
public String[] getStringArrValue(String string) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
List<String> val = getListStrValue(string);
|
||||
return val.toArray(new String[val.size()]);
|
||||
}
|
||||
|
||||
public int getIntValue(String string,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
return Integer.parseInt(getStringValue(string, queryParams));
|
||||
}
|
||||
|
||||
public int getIntValue(String string) {
|
||||
return Integer.parseInt(getStringValue(string));
|
||||
return getIntValue(string, null);
|
||||
}
|
||||
|
||||
public boolean getBooleanValue(String string) {
|
||||
@ -89,8 +128,9 @@ public class APIClient {
|
||||
return Double.parseDouble(getStringValue(string));
|
||||
}
|
||||
|
||||
public List<String> getListStrValue(String string) {
|
||||
JsonReader reader = getReader(string);
|
||||
public List<String> getListStrValue(String string,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
JsonReader reader = getReader(string, queryParams);
|
||||
JsonArray arr = reader.readArray();
|
||||
List<String> res = new ArrayList<String>(arr.size());
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
@ -101,19 +141,120 @@ public class APIClient {
|
||||
|
||||
}
|
||||
|
||||
public List<String> getListStrValue(String string) {
|
||||
return getListStrValue(string, null);
|
||||
|
||||
}
|
||||
|
||||
public static List<String> listStrFromJArr(JsonArray arr) {
|
||||
List<String> res = new ArrayList<String>();
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
res.add(arr.getString(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static String join(String[] arr, String joiner) {
|
||||
String res = "";
|
||||
if (arr != null) {
|
||||
for (String name : arr) {
|
||||
if (name != null && !name.equals("")) {
|
||||
if (!res.equals("")) {
|
||||
res = res + ",";
|
||||
}
|
||||
res = res + name;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static String join(String[] arr) {
|
||||
return join(arr, ",");
|
||||
}
|
||||
|
||||
public static boolean set_query_param(
|
||||
MultivaluedMap<String, String> queryParams, String key, String value) {
|
||||
if (queryParams != null && key != null && value != null
|
||||
&& !value.equals("")) {
|
||||
queryParams.add(key, value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean set_bool_query_param(
|
||||
MultivaluedMap<String, String> queryParams, String key,
|
||||
boolean value) {
|
||||
if (queryParams != null && key != null && value) {
|
||||
queryParams.add(key, "true");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map<List<String>, List<String>> getMapListStrValue(String string,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
if (string.equals("")) {
|
||||
return null;
|
||||
}
|
||||
JsonReader reader = getReader(string, queryParams);
|
||||
JsonArray arr = reader.readArray();
|
||||
Map<List<String>, List<String>> map = new HashMap<List<String>, List<String>>();
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
JsonObject obj = arr.getJsonObject(i);
|
||||
if (obj.containsKey("key") && obj.containsKey("value")) {
|
||||
map.put(listStrFromJArr(obj.getJsonArray("key")),
|
||||
listStrFromJArr(obj.getJsonArray("value")));
|
||||
}
|
||||
}
|
||||
reader.close();
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<List<String>, List<String>> getMapListStrValue(String string) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return getMapListStrValue(string, null);
|
||||
}
|
||||
|
||||
public Map<String, String> getMapStrValue(String string,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
if (string.equals("")) {
|
||||
return null;
|
||||
}
|
||||
JsonReader reader = getReader(string, queryParams);
|
||||
JsonArray arr = reader.readArray();
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
JsonObject obj = arr.getJsonObject(i);
|
||||
if (obj.containsKey("key") && obj.containsKey("value")) {
|
||||
map.put(obj.getString("key"), obj.getString("value"));
|
||||
}
|
||||
}
|
||||
reader.close();
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, String> getMapStrValue(String string) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return getMapStrValue(string, null);
|
||||
}
|
||||
|
||||
public List<InetAddress> getListInetAddressValue(String string,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
List<String> vals = getListStrValue(string, queryParams);
|
||||
List<InetAddress> res = new ArrayList<InetAddress>();
|
||||
for (String val : vals) {
|
||||
try {
|
||||
res.add(InetAddress.getByName(val));
|
||||
} catch (UnknownHostException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<InetAddress> getListInetAddressValue(String string) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return getListInetAddressValue(string, null);
|
||||
}
|
||||
|
||||
public Map<String, TabularData> getMapStringTabularDataValue(String string) {
|
||||
@ -121,6 +262,41 @@ public class APIClient {
|
||||
return null;
|
||||
}
|
||||
|
||||
private TabularDataSupport getSnapshotData(String ks, JsonArray arr) {
|
||||
TabularDataSupport data = new TabularDataSupport(
|
||||
SnapshotDetailsTabularData.TABULAR_TYPE);
|
||||
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
JsonObject obj = arr.getJsonObject(i);
|
||||
if (obj.containsKey("key") && obj.containsKey("cf")) {
|
||||
SnapshotDetailsTabularData.from(obj.getString("key"), ks,
|
||||
obj.getString("cf"), obj.getInt("total"),
|
||||
obj.getInt("live"), data);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public Map<String, TabularData> getMapStringSnapshotTabularDataValue(
|
||||
String string, MultivaluedMap<String, String> queryParams) {
|
||||
if (string.equals("")) {
|
||||
return null;
|
||||
}
|
||||
JsonReader reader = getReader(string, queryParams);
|
||||
JsonArray arr = reader.readArray();
|
||||
Map<String, TabularData> map = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
JsonObject obj = arr.getJsonObject(i);
|
||||
if (obj.containsKey("key") && obj.containsKey("value")) {
|
||||
String key = obj.getString("key");
|
||||
map.put(key, getSnapshotData(key, obj.getJsonArray("value")));
|
||||
}
|
||||
}
|
||||
reader.close();
|
||||
return map;
|
||||
}
|
||||
|
||||
public long getLongValue(String string) {
|
||||
return Long.parseLong(getStringValue(string));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user