· 6 years ago · Oct 17, 2019, 06:08 PM
1package com.wti.osmosis.app.api;
2
3import okhttp3.HttpUrl;
4import okhttp3.MediaType;
5import okhttp3.OkHttpClient;
6import okhttp3.Request;
7import okhttp3.RequestBody;
8import okhttp3.Response;
9
10import com.wti.osmosis.app.utils.Utils;
11
12import org.json.simple.JSONArray;
13import org.json.simple.JSONObject;
14import org.json.simple.parser.JSONParser;
15import org.json.simple.parser.ParseException;
16
17import java.io.IOException;
18import java.util.List;
19import java.util.Map;
20import java.util.stream.Collectors;
21
22/**
23 * Osmosis' API client
24 */
25public class APIClient {
26 private static final String NODES = "Nodes";
27 private static final String MESSAGES = "Messages";
28 private OkHttpClient client;
29 private final String baseUrl;
30
31 /**
32 * Basic configurator
33 *
34 * @param baseUrl API base url
35 */
36 public APIClient(String baseUrl) {
37 this.baseUrl = baseUrl;
38 this.client = new OkHttpClient();
39 }
40
41 /**
42 * Request current messages to API
43 *
44 * @return connections json array
45 * @throws IOException
46 * @throws ParseException
47 */
48 public JSONArray getCurrentMessages(JSONObject filter) throws IOException, ParseException {
49 Response response = get(MESSAGES, filter);
50 return (JSONArray) ((JSONObject) new JSONParser().parse(response.body().string())).get("data");
51 }
52
53 /**
54 * Request current connections to API
55 *
56 * @return connections json array
57 * @throws IOException
58 * @throws ParseException
59 */
60 public JSONArray getCurrentConnections() throws IOException, ParseException {
61 Response response = get(NODES);
62 return (JSONArray) ((JSONObject) new JSONParser().parse(response.body().string())).get("data");
63 }
64
65 /**
66 * Request current connections to API
67 *
68 * @return connections json array
69 * @throws IOException
70 * @throws ParseException
71 */
72 public JSONObject sendMessage(JSONObject object) throws IOException, ParseException {
73 Response response = post(MESSAGES, object);
74 return (JSONObject) new JSONParser().parse(response.body().string());
75 }
76
77 /**
78 * Basic request function
79 *
80 * @param url
81 * @param method
82 * @param data
83 * @return
84 * @throws IOException
85 */
86 private Response request(String url, String method, JSONObject data) throws IOException {
87 final boolean isGetMethod = method.equals("GET");
88 RequestBody body = (!isGetMethod && data != null) ? RequestBody.create(MediaType.parse("application/json"), data.toString()) : null;
89 HttpUrl requestUrl = createURL(url, data);
90 Request request = new Request.Builder()
91 .url(requestUrl)
92 .method(method, body)
93 .build();
94 return client.newCall(request).execute();
95 }
96
97 private HttpUrl createURL(String resource, JSONObject queryParams) {
98 HttpUrl.Builder builder = new HttpUrl.Builder()
99 .scheme("http")
100 .port(9091)
101 .host(baseUrl)
102 .addPathSegment(resource);
103 if (queryParams != null) {
104 for (Object value : queryParams.keySet()) {
105 String key = value.toString();
106 if (queryParams.get(key) instanceof JSONArray) {
107 JSONArray array = (JSONArray) queryParams.get(key);
108 if (array != null) {
109 for (Object o : array) {
110 builder.addQueryParameter(key, o.toString());
111 }
112 }
113 } else {
114 builder.addQueryParameter(key, queryParams.get(key).toString());
115 }
116 }
117 }
118 return builder.build();
119 }
120
121 /**
122 * Basic GET function
123 *
124 * @param url request url
125 * @return HTTP response
126 * @throws IOException
127 */
128 private Response get(String url) throws IOException {
129 return get(url, null);
130 }
131
132 /**
133 * Basic GET function
134 *
135 * @param url request url
136 * @return HTTP response
137 * @throws IOException
138 */
139 private Response get(String url, JSONObject data) throws IOException {
140 return request(url, "GET", data);
141 }
142
143 /**
144 * Basic POST function
145 *
146 * @param url request url
147 * @return HTTP response
148 * @throws IOException
149 */
150 private Response post(String url, JSONObject data) throws IOException {
151 return request(url, "POST", data);
152 }
153
154 public Map getNode(String uuid) throws IOException, ParseException {
155 JSONArray a = getCurrentConnections();
156 List lit = (List) a.stream().filter(o -> ((JSONObject) o).get("uuid").toString().equals(uuid)).collect(Collectors.toList());
157 return (JSONObject) lit.get(0);
158 }
159}