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