|
@@ -1,3 +1,11 @@
|
|
1
|
+
|
|
2
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
3
|
+import com.mashape.unirest.http.Unirest;
|
|
4
|
+import com.mashape.unirest.http.exceptions.UnirestException;
|
|
5
|
+import java.io.IOException;
|
|
6
|
+import java.util.ArrayList;
|
|
7
|
+import java.util.Arrays;
|
|
8
|
+
|
1
|
9
|
public class YouAreEll {
|
2
|
10
|
|
3
|
11
|
YouAreEll() {
|
|
@@ -10,14 +18,97 @@ public class YouAreEll {
|
10
|
18
|
}
|
11
|
19
|
|
12
|
20
|
public String get_ids() {
|
13
|
|
- return MakeURLCall("/ids", "GET", "");
|
|
21
|
+ ObjectMapper jsonMapper = new ObjectMapper();
|
|
22
|
+ String jsonString = MakeURLCall("/ids", "GET", "");
|
|
23
|
+ try {
|
|
24
|
+ IDs[] iDSList = jsonMapper.readValue(jsonString, IDs[].class);
|
|
25
|
+ return Arrays.toString(iDSList);
|
|
26
|
+ } catch (IOException e) {
|
|
27
|
+ e.printStackTrace();
|
|
28
|
+ }
|
|
29
|
+ return null;
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ public void postID(String name, String gitHubID){
|
|
33
|
+ String jsonPackage = "{ \"userid\": \"-\", \"name\": \"" + name + "\", \"github\": \"" + gitHubID + "\"}";
|
|
34
|
+ MakeURLCall("/ids", "PUT", jsonPackage);
|
14
|
35
|
}
|
15
|
36
|
|
16
|
37
|
public String get_messages() {
|
17
|
|
- return MakeURLCall("/messages", "GET", "");
|
|
38
|
+ String jsonString = MakeURLCall("/messages", "GET", "");
|
|
39
|
+ String last20 = convertMessagesToString(jsonString);
|
|
40
|
+ if (last20 != null) return last20;
|
|
41
|
+ return null;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ private String convertMessagesToString(String jsonString) {
|
|
45
|
+ ObjectMapper jsonMapper = new ObjectMapper();
|
|
46
|
+ try {
|
|
47
|
+ Messages[] messagesList = jsonMapper.readValue(jsonString, Messages[].class);
|
|
48
|
+ if (messagesList!=null) {
|
|
49
|
+ Messages[] last20 = Arrays.copyOf(messagesList, 20);
|
|
50
|
+ return getRidOfNulls(last20);
|
|
51
|
+ }
|
|
52
|
+ } catch (IOException e) {
|
|
53
|
+ e.printStackTrace();
|
|
54
|
+ }
|
|
55
|
+ return null;
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ private String convertSingeMessageToString(String jsonString) {
|
|
59
|
+ ObjectMapper jsonMapper = new ObjectMapper();
|
|
60
|
+ try {
|
|
61
|
+ Messages message = jsonMapper.readValue(jsonString, Messages.class);
|
|
62
|
+ return message.toString();
|
|
63
|
+ } catch (IOException e) {
|
|
64
|
+ e.printStackTrace();
|
|
65
|
+ }
|
|
66
|
+ return null;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ public String get_messagesForId(String id) {
|
|
70
|
+ String jsonString = MakeURLCall("/ids/"+id+"/messages", "GET", "");
|
|
71
|
+ String last20 = convertMessagesToString(jsonString);
|
|
72
|
+ if (last20 != null) return last20;
|
|
73
|
+ return null;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public String sendMessage(String id, String message){
|
|
77
|
+ String jsonPackage = "{ \"sequence\": \"-\", \"timestamp\": \"2018-03-21T01:00:00.0Z\", \"fromid\": \"" +id + "\"," +
|
|
78
|
+ "\"toid\": \"\", \"message\": \"" + message + "\"}";
|
|
79
|
+ String returnedMessage = MakeURLCall("/ids/" + id + "/messages", "PUT", jsonPackage);
|
|
80
|
+ return convertSingeMessageToString(returnedMessage);
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ public String sendMessageToId(String fromId, String message, String toId){
|
|
84
|
+ String jsonPackage = "{ \"sequence\": \"-\", \"timestamp\": \"2018-03-21T01:00:00.0Z\", \"fromid\": \"" + fromId + "\"," +
|
|
85
|
+ "\"toid\": \""+toId+ "\", \"message\": \"" + message + "\"}";
|
|
86
|
+ String returnedString = MakeURLCall("/ids/" + fromId + "/messages", "PUT", jsonPackage);
|
|
87
|
+ return convertSingeMessageToString(returnedString);
|
18
|
88
|
}
|
19
|
89
|
|
20
|
90
|
public String MakeURLCall(String mainurl, String method, String jpayload) {
|
21
|
|
- return "nada";
|
|
91
|
+
|
|
92
|
+ try {
|
|
93
|
+ if (method.equals("GET")) {
|
|
94
|
+ return Unirest.get("http://zipcode.rocks:8085" + mainurl).asString().getBody();
|
|
95
|
+ }
|
|
96
|
+ if (method.equals("PUT")){
|
|
97
|
+ return Unirest.post("http://zipcode.rocks:8085" + mainurl).body(jpayload).asString().getBody();
|
|
98
|
+ }
|
|
99
|
+ }catch (UnirestException e){
|
|
100
|
+ e.printStackTrace();
|
|
101
|
+ }
|
|
102
|
+ return null;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ private String getRidOfNulls(Messages[] messages){
|
|
106
|
+ ArrayList<Messages> returnArrayList = new ArrayList<>();
|
|
107
|
+ for (Messages message: messages) {
|
|
108
|
+ if (message!=null){
|
|
109
|
+ returnArrayList.add(message);
|
|
110
|
+ }
|
|
111
|
+ }
|
|
112
|
+ return returnArrayList.toString();
|
22
|
113
|
}
|
23
|
114
|
}
|