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