Browse Source

get_ids ad get_messages

Lauren Green 6 years ago
parent
commit
ff7424bf5d
2 changed files with 24 additions and 17 deletions
  1. 12
    9
      Client/src/main/java/SimpleShell.java
  2. 12
    8
      Client/src/main/java/YouAreEll.java

+ 12
- 9
Client/src/main/java/SimpleShell.java View File

@@ -1,3 +1,4 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
1 2
 import com.mashape.unirest.http.HttpResponse;
2 3
 import com.mashape.unirest.http.JsonNode;
3 4
 import com.mashape.unirest.http.Unirest;
@@ -13,10 +14,16 @@ import java.util.List;
13 14
 public class SimpleShell {
14 15
 
15 16
 
16
-    public static void prettyPrint(String output) {
17
-        // yep, make an effort to format things nicely, eh?
18
-        System.out.println(output);
17
+    public static void prettyPrint(JsonNode jsonNode) {
18
+        try {
19
+            ObjectMapper mapper = new ObjectMapper();
20
+            Object json = mapper.readValue(jsonNode.toString(), Object.class);
21
+            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
22
+        } catch (Exception e) {
23
+            System.out.println("Sorry, pretty print didn't work");
24
+        }
19 25
     }
26
+
20 27
     public static void main(String[] args) throws java.io.IOException {
21 28
 
22 29
         YouAreEll webber = new YouAreEll();
@@ -65,15 +72,13 @@ public class SimpleShell {
65 72
 
66 73
                 // ids
67 74
                 if (list.contains("ids")) {
68
-                    String results = webber.get_ids();
69
-                    SimpleShell.prettyPrint(results);
75
+                    webber.get_ids();
70 76
                     continue;
71 77
                 }
72 78
 
73 79
                 // messages
74 80
                 if (list.contains("messages")) {
75
-                    String results = webber.get_messages();
76
-                    SimpleShell.prettyPrint(results);
81
+                    webber.get_messages();
77 82
                     continue;
78 83
                 }
79 84
                 // you need to add a bunch more.
@@ -111,8 +116,6 @@ public class SimpleShell {
111 116
             //catch ioexception, output appropriate message, resume waiting for input
112 117
             catch (IOException e) {
113 118
                 System.out.println("Input Error, Please try again!");
114
-            } catch (UnirestException e) {
115
-                e.printStackTrace();
116 119
             }
117 120
             // So what, do you suppose, is the meaning of this comment?
118 121
             /** The steps are:

+ 12
- 8
Client/src/main/java/YouAreEll.java View File

@@ -2,6 +2,8 @@ import com.mashape.unirest.http.HttpResponse;
2 2
 import com.mashape.unirest.http.JsonNode;
3 3
 import com.mashape.unirest.http.Unirest;
4 4
 import com.mashape.unirest.http.exceptions.UnirestException;
5
+import org.json.JSONArray;
6
+import org.json.JSONObject;
5 7
 
6 8
 import java.io.IOException;
7 9
 import java.net.HttpURLConnection;
@@ -22,30 +24,32 @@ public class YouAreEll {
22 24
 //        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
23 25
     }
24 26
 
25
-    public String get_ids(){
27
+    public void get_ids(){
26 28
 
27 29
         try {
28 30
             HttpResponse<JsonNode> jsonResponse = Unirest.get(underARock + "/ids")
29 31
                     .header("accept", "application/json")
30 32
                     .asJson();
31 33
 
32
-            String ids = jsonResponse.getBody().toString();
33
-            System.out.println(ids);
34
-            return ids;
34
+            SimpleShell.prettyPrint(jsonResponse.getBody());
35
+
35 36
         } catch (UnirestException e) {
36 37
             System.out.println("Error");
37 38
         }
38 39
 
39
-        return null;
40 40
     }
41 41
 
42
-    public String get_messages() throws UnirestException {
42
+    public void get_messages() {
43 43
         try {
44
-        return MakeURLCall("/messages", "GET", "");
44
+            HttpResponse<JsonNode> jsonResponse = Unirest.get(underARock + "/messages")
45
+                    .header("accept", "application/json")
46
+                    .asJson();
47
+
48
+            SimpleShell.prettyPrint(jsonResponse.getBody());
49
+
45 50
         } catch (UnirestException e) {
46 51
             System.out.println("Error");
47 52
         }
48
-        return null;
49 53
     }
50 54
 
51 55
     public String MakeURLCall(String mainurl, String method, String jpayload) throws UnirestException {