Преглед на файлове

messing with the methods

Trinh Tong преди 6 години
родител
ревизия
7996c10a40

+ 1
- 1
Client/src/main/java/CmdBuilder.java Целия файл

@@ -8,7 +8,7 @@ public class  CmdBuilder {
8 8
         String name = cmdArr[1];
9 9
         String gitName = cmdArr[cmdArr.length-1];
10 10
 
11
-        return new JSONObject("{\"name\": \"" + name + "\",\"githubName\": \"" + gitName + "\"}");
11
+        return new JSONObject("{\"name\": \"" + name + "\",\"github\": \"" + gitName + "\"}");
12 12
 
13 13
     }
14 14
 }

+ 35
- 0
Client/src/main/java/PrettyPrint.java Целия файл

@@ -0,0 +1,35 @@
1
+import com.fasterxml.jackson.core.PrettyPrinter;
2
+import com.fasterxml.jackson.core.type.TypeReference;
3
+import com.fasterxml.jackson.databind.ObjectMapper;
4
+import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
5
+import ids.Id;
6
+
7
+import java.io.IOException;
8
+import java.util.List;
9
+
10
+public class PrettyPrint {
11
+
12
+    public PrettyPrint() {
13
+    }
14
+
15
+    public static void prettyPrintJsonArray(String output) throws IOException {
16
+        ObjectMapper mapper = new ObjectMapper();
17
+        mapper.registerModule(new JsonOrgModule());
18
+
19
+        List<Id> idArray = mapper.readValue(output, new TypeReference<List<Id>>(){});
20
+
21
+        //Id idArray = mapper.readValue(output, Id.class);
22
+
23
+        System.out.println(idArray.toString());
24
+    }
25
+
26
+    public static void prettyPrintSingle(String output) throws IOException {
27
+
28
+        // Deserialize JSON string into Java class objects; assume JSONArray
29
+        ObjectMapper mapper = new ObjectMapper();
30
+        Object json = mapper.readValue(output, Id.class);
31
+        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
32
+//        mapper.registerModule(new JsonOrgModule());
33
+
34
+    }
35
+}

+ 2
- 17
Client/src/main/java/SimpleShell.java Целия файл

@@ -14,21 +14,6 @@ import java.util.List;
14 14
 public class SimpleShell {
15 15
 
16 16
 
17
-    public static void prettyPrint(String output) throws IOException {
18
-        // Assume it's a String
19
-        // yep, make an effort to format things nicely, eh?
20
-
21
-        // Deserialize JSON string into Java class objects; assume JSONArray
22
-
23
-        ObjectMapper mapper = new ObjectMapper();
24
-        mapper.registerModule(new JsonOrgModule());
25
-
26
-        List<Id> idArray = mapper.readValue(output, new TypeReference<List<Id>>(){});
27
-
28
-        //Id idArray = mapper.readValue(output, Id.class);
29
-
30
-        System.out.println(idArray.toString());
31
-    }
32 17
     public static void main(String[] args) throws java.io.IOException {
33 18
 
34 19
         YouAreEll webber = new YouAreEll();
@@ -81,14 +66,14 @@ public class SimpleShell {
81 66
                 // ids
82 67
                 if (list.contains("ids")) {
83 68
                     String results = webber.get_ids();
84
-                    SimpleShell.prettyPrint(results);
69
+                    PrettyPrint.prettyPrintJsonArray(results);
85 70
                     continue;
86 71
                 }
87 72
 
88 73
                 // messages
89 74
                 if (list.contains("messages")) {
90 75
                     String results = webber.get_messages();
91
-                    SimpleShell.prettyPrint(results);
76
+                    PrettyPrint.prettyPrintJsonArray(results);
92 77
                     continue;
93 78
                 }
94 79
                 // you need to add a bunch more.

+ 65
- 16
Client/src/main/java/YouAreEll.java Целия файл

@@ -1,6 +1,6 @@
1
-import com.fasterxml.jackson.annotation.JsonValue;
2 1
 import ids.Id;
3 2
 import org.apache.http.HttpResponse;
3
+import org.apache.http.client.ClientProtocolException;
4 4
 import org.apache.http.client.HttpClient;
5 5
 import org.apache.http.client.methods.HttpGet;
6 6
 import org.apache.http.client.methods.HttpPost;
@@ -15,57 +15,60 @@ import java.io.InputStreamReader;
15 15
 public class YouAreEll {
16 16
     private HttpClient client;
17 17
     private final String baseUrl = "http://zipcode.rocks:8085";
18
+
18 19
     YouAreEll() {
19 20
         this.client = HttpClientBuilder.create().build();
20 21
     }
21 22
 
22 23
 
23
-
24 24
     public static void main(String[] args) throws IOException {
25 25
         YouAreEll urlhandler = new YouAreEll();
26
-        Id luna = new Id("ea795ee660116450ae935a1374166a142b263e64", "new-name", "new-github");
26
+        Id luna = new Id("ea795ee660116450ae935a1374166a142b263e64", "Reset-Me", "new-github");
27 27
 //        String response = urlhandler.MakeURLCall("/ids", "GET", "");
28
-        System.out.println(luna.toString());
29
-        String postResponse = urlhandler.MakeURLCall("/ids", "PUT", luna.toString());
28
+//        System.out.println(luna.toString());
29
+        String getResponse = urlhandler.get_ids();
30
+        PrettyPrint.prettyPrintJsonArray(getResponse);
30 31
         // this should be dematerialized to obtain info inside the json object
31
-        System.out.println(postResponse);
32
+//        PrettyPrint.prettyPrintSingle(postResponse);
32 33
         // multiple ids with the same github
33 34
         // with the put, how to update without knowing the id?
34 35
 //        SimpleShell.prettyPrint(postResponse);
35
-//      SimpleShell.prettyPrint("{\"userid\":\"\\\"\\\"\",\"name\":\"uhaushasuh\",\"githubName\":\"hasdhadhdash\"}");
36
-       // System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
36
+//      SimpleShell.prettyPrint("{\"userid\":\"\\\"\\\"\",\"name\":\"uhaushasuh\",\"github\":\"hasdhadhdash\"}");
37
+        // System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
37 38
     }
38 39
 
39
-    public String get_ids() {
40
+    public String get_ids() throws IOException {
40 41
         return MakeURLCall("/ids", "GET", "");
41 42
     }
42 43
 
43
-    public String get_messages() {
44
+    public String get_messages() throws IOException {
44 45
         return MakeURLCall("/messages", "GET", "");
45 46
     }
46 47
 
47
-    public String MakeURLCall(String mainurl, String method, String jpayload) {
48
-        switch(method) {
48
+    public String MakeURLCall(String mainurl, String method, String jpayload) throws IOException {
49
+        switch (method) {
49 50
             case "GET":
51
+
50 52
                 HttpGet request = new HttpGet(baseUrl + mainurl);
51 53
                 try {
54
+//                    request.setEntity(new StringEntity(jpayload));
52 55
                     HttpResponse response = client.execute(request);
53
-                    // response.getStatusLine().getStatusCode(); // 200 OK, 404 Not Found, etc.
54
-
55 56
                     BufferedReader rd = new BufferedReader(
56 57
                             new InputStreamReader(response.getEntity().getContent()));
57 58
 
59
+                    // response.getStatusLine().getStatusCode(); // 200 OK, 404 Not Found, etc.
58 60
                     StringBuilder responseString = new StringBuilder();
59 61
                     String line;
60 62
 
61 63
                     while ((line = rd.readLine()) != null) {
62 64
                         responseString.append(line);
63 65
                     }
64
-                    //System.out.println("response: " + responseString);
66
+
65 67
                     return responseString.toString();
66
-                } catch(IOException e) {
68
+                } catch (IOException e) {
67 69
                     return ("An unexpected IO error occurred: " + e.getMessage());
68 70
                 }
71
+
69 72
             case "POST":
70 73
                 HttpPost postReq = new HttpPost(baseUrl + mainurl);
71 74
                 try {
@@ -85,6 +88,7 @@ public class YouAreEll {
85 88
                 } catch (IOException e) {
86 89
                     return ("An unexpected IO error occurred: " + e.getMessage());
87 90
                 }
91
+
88 92
             case "PUT":
89 93
                 HttpPut putReq = new HttpPut(baseUrl + mainurl);
90 94
                 try {
@@ -108,4 +112,49 @@ public class YouAreEll {
108 112
                 return "nada";
109 113
         }
110 114
     }
115
+
116
+
117
+    private String getCall(String url) {
118
+
119
+        HttpGet request = new HttpGet("http://zipcode.rocks:8085/ids");
120
+        try {
121
+            HttpResponse response = client.execute(request);
122
+            return readCalls(response);
123
+        } catch (IOException e) {
124
+            return ("Unhandled Exception error");
125
+        }
126
+    }
127
+
128
+
129
+    public String postCall(HttpPost post, String payload) throws IOException {
130
+        try {
131
+            post.setEntity(new StringEntity(payload));
132
+            HttpResponse response = client.execute(post);
133
+            return readCalls(response);
134
+        } catch (IOException e) {
135
+            return ("An unexpected IO error occurred: " + e.getMessage());
136
+        }
137
+    }
138
+
139
+    private String readCalls(HttpResponse response) throws IOException {
140
+        try {
141
+        BufferedReader rd = new BufferedReader(
142
+                new InputStreamReader(response.getEntity().getContent()));
143
+
144
+        StringBuilder responseString = new StringBuilder();
145
+        String line;
146
+
147
+        while ((line = rd.readLine()) != null) {
148
+            responseString.append(line);
149
+        }
150
+
151
+            return responseString.toString();
152
+
153
+        } catch (IOException e) {
154
+            return ("An unexpected IO error occurred: " + e.getMessage());
155
+    }
156
+
157
+    }
111 158
 }
159
+
160
+

+ 3
- 3
Client/src/main/java/ids/Id.java Целия файл

@@ -1,7 +1,7 @@
1 1
 package ids;
2 2
 
3 3
 public class Id {
4
-    public String userid, name, githubName;
4
+    public String userid, name, github;
5 5
 
6 6
     public Id() {
7 7
         super();
@@ -10,7 +10,7 @@ public class Id {
10 10
     public Id(String userId, String name, String github) {
11 11
         this.userid = userId;
12 12
         this.name = name;
13
-        this.githubName = github;
13
+        this.github = github;
14 14
     }
15 15
 
16 16
     @Override
@@ -18,7 +18,7 @@ public class Id {
18 18
         return "\n{" +
19 19
                 "\n\t\"userid\": \"" + this.userid + "\"," +
20 20
                 "\n\t\"name\": \"" + this.name + "\"," +
21
-                "\n\t\"github\": \"" + this.githubName + "\"" +
21
+                "\n\t\"github\": \"" + this.github + "\"" +
22 22
                 "\n}";
23 23
     }
24 24
 }

+ 1
- 1
Client/src/test/CmdBuilderTest.java Целия файл

@@ -12,7 +12,7 @@ public class CmdBuilderTest {
12 12
 
13 13
         String[] cmdLine = cmd.split(" ");
14 14
 
15
-        String expected = (new JSONObject("{\"name\": \"" + cmdLine[1] + "\",\"githubName\": \"" + cmdLine[2] + "\"}")).toString();
15
+        String expected = (new JSONObject("{\"name\": \"" + cmdLine[1] + "\",\"github\": \"" + cmdLine[2] + "\"}")).toString();
16 16
         String actual = CmdBuilder.toPayload(cmdLine).toString();
17 17
 
18 18
         System.out.println(actual);