Browse Source

posting and getting in pretty print

April Rivera 6 years ago
parent
commit
9492ca64c4

+ 50
- 0
Client/src/main/java/Entity/GitHubUser.java View File

@@ -0,0 +1,50 @@
1
+package Entity;
2
+
3
+public class GitHubUser {
4
+
5
+    String github;
6
+    String name;
7
+    String userid;
8
+
9
+    public GitHubUser(String github, String name, String userId) {
10
+        this.github = github;
11
+        this.name = name;
12
+        this.userid = userId;
13
+    }
14
+
15
+    public GitHubUser(){}
16
+
17
+    public String getGithub() {
18
+
19
+        return github;
20
+    }
21
+
22
+    public void setGithub(String github) {
23
+        this.github = github;
24
+    }
25
+
26
+    public String getName() {
27
+        return name;
28
+    }
29
+
30
+    public void setName(String name) {
31
+        this.name = name;
32
+    }
33
+
34
+    public String getuserid() {
35
+        return userid;
36
+    }
37
+
38
+    public void setuserid(String userId) {
39
+        this.userid = userId;
40
+    }
41
+
42
+    @Override
43
+    public String toString() {
44
+        StringBuilder stringBuilder = new StringBuilder();
45
+        stringBuilder.append("Github: " + this.github + "\n");
46
+        stringBuilder.append("User ID: " + this.userid + "\n");
47
+        stringBuilder.append("Name: " + this.name + "\n");
48
+        return stringBuilder.toString();
49
+    }
50
+}

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

@@ -1,3 +1,4 @@
1
+import Entity.GitHubUser;
1 2
 import Entity.Id;
2 3
 import com.fasterxml.jackson.databind.ObjectMapper;
3 4
 
@@ -11,15 +12,15 @@ import java.util.List;
11 12
 public class SimpleShell {
12 13
 
13 14
 
14
-    public static void prettyPrint(String output) {
15
+    public static void prettyPrint(Object output) {
15 16
         ObjectMapper jacksonMapper = new ObjectMapper();
16 17
         try {
17
-            Object jsonObject = jacksonMapper.readValue(output, Id.class);
18
-            String prettyJson = jacksonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
19
-            System.out.println(prettyJson);
20
-        } catch (IOException e) {
21
-            e.printStackTrace();
18
+            System.out.println(jacksonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(output));
19
+
22 20
         }
21
+            catch (Exception e){
22
+                e.printStackTrace();
23
+            }
23 24
 
24 25
     }
25 26
 
@@ -71,11 +72,16 @@ public class SimpleShell {
71 72
 
72 73
                 // ids
73 74
                 if (list.contains("ids")) {
74
-                    String results = webber.get_ids();
75
-                    SimpleShell.prettyPrint(results);
76
-                    continue;
75
+                    try {
76
+                        List<GitHubUser> results = webber.get_idsAsList();
77
+                        SimpleShell.prettyPrint(results);
78
+                        continue;
79
+                    }catch (Exception e){
80
+                        e.printStackTrace();
81
+                    }
77 82
                 }
78 83
 
84
+
79 85
                 // messages
80 86
                 if (list.contains("messages")) {
81 87
                     String results = webber.get_messages();

+ 38
- 23
Client/src/main/java/YouAreEll.java View File

@@ -1,12 +1,14 @@
1
-import com.fasterxml.jackson.core.JsonProcessingException;
1
+import Entity.GitHubUser;
2
+import com.fasterxml.jackson.core.type.TypeReference;
3
+import com.fasterxml.jackson.databind.ObjectMapper;
2 4
 import com.mashape.unirest.http.HttpResponse;
3 5
 import com.mashape.unirest.http.JsonNode;
4
-import com.mashape.unirest.http.ObjectMapper;
5 6
 import com.mashape.unirest.http.Unirest;
6 7
 import com.mashape.unirest.http.exceptions.UnirestException;
7 8
 import com.mashape.unirest.request.HttpRequestWithBody;
8 9
 
9
-import java.io.IOException;
10
+import java.util.ArrayList;
11
+import java.util.List;
10 12
 
11 13
 public class YouAreEll {
12 14
 
@@ -14,37 +16,50 @@ public class YouAreEll {
14 16
     }
15 17
 
16 18
     public static void main(String[] args) {
19
+//      jackson mapper
20
+        ObjectMapper mapper = new ObjectMapper();
17 21
         YouAreEll urlhandler = new YouAreEll();
18 22
 
19
-        Unirest.setObjectMapper(new ObjectMapper() {
20
-            private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
21
-                    = new com.fasterxml.jackson.databind.ObjectMapper();
23
+//       Unirest.setObjectMapper(mapper);
24
+//      list to store github users
25
+        String json = urlhandler.MakeURLCall("/ids", "GET", "");
26
+        System.out.println(json);
27
+        List<GitHubUser> list = new ArrayList<GitHubUser>();
28
+        try {
29
+//      type reference is a list of github users
30
+           list = mapper.readValue(json, new TypeReference<List<GitHubUser>>(){});
31
+            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list));
22 32
 
23
-            public <T> T readValue(String value, Class<T> valueType) {
24
-                try {
25
-                    return jacksonObjectMapper.readValue(value, valueType);
26
-                } catch (IOException e) {
27
-                    throw new RuntimeException(e);
28
-                }
29
-            }
33
+        } catch (Exception e){
34
+            e.printStackTrace();
35
+        }
30 36
 
31
-            public String writeValue(Object value) {
32
-                try {
33
-                    return jacksonObjectMapper.writeValueAsString(value);
34
-                } catch (JsonProcessingException e) {
35
-                    throw new RuntimeException(e);
36
-                }
37
-            }
38
-        });
37
+        for(GitHubUser gitHubUser : list){
38
+            System.out.println(gitHubUser.toString());
39
+        }
40
+
41
+        GitHubUser april = new GitHubUser("aprilcr8777", "APRILAGAIN", "32585650");
42
+        try {
43
+        String jsonPayLoad =  mapper.writeValueAsString(april);
44
+        System.out.println(jsonPayLoad);
39 45
 
40
-        System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
41
-        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
46
+            urlhandler.MakeURLCall("/ids", "POST", mapper.writeValueAsString(april));
47
+            } catch (Exception e){
48
+            e.printStackTrace();
49
+        }
42 50
     }
43 51
 
44 52
     public String get_ids() {
45 53
         return MakeURLCall("/ids", "GET", "");
46 54
     }
47 55
 
56
+    public List<GitHubUser> get_idsAsList() throws Exception{
57
+        ObjectMapper objectMapper = new ObjectMapper();
58
+
59
+     return objectMapper.readValue(MakeURLCall("/ids", "GET", ""), new TypeReference<List<GitHubUser>>(){});
60
+
61
+    }
62
+
48 63
     public String get_messages() {
49 64
         return MakeURLCall("/messages", "GET", "");
50 65
     }

+ 4
- 0
pom.xml View File

@@ -20,6 +20,8 @@
20 20
             <version>1.4.9</version>
21 21
         </dependency>
22 22
 
23
+
24
+
23 25
         <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
24 26
         <dependency>
25 27
             <groupId>com.fasterxml.jackson.core</groupId>
@@ -27,6 +29,8 @@
27 29
             <version>2.8.6</version>
28 30
         </dependency>
29 31
 
32
+
33
+
30 34
     </dependencies>
31 35
 </project>
32 36