Browse Source

messages and ids sendable

Lawrence Wu 6 years ago
parent
commit
4e5f9508e5

+ 2
- 9
Client/Client.iml View File

@@ -12,14 +12,7 @@
12 12
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
13 13
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
14 14
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
15
-    <orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
16
-    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.2" level="project" />
17
-    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.4" level="project" />
18
-    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
19
-    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.9" level="project" />
20
-    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.1.1" level="project" />
21
-    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.4.4" level="project" />
22
-    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.5.2" level="project" />
23
-    <orderEntry type="library" name="Maven: org.json:json:20160212" level="project" />
15
+    <orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.10.0" level="project" />
16
+    <orderEntry type="library" name="Maven: com.squareup.okio:okio:1.14.0" level="project" />
24 17
   </component>
25 18
 </module>

+ 3
- 3
Client/pom.xml View File

@@ -17,9 +17,9 @@
17 17
             <version>2.8.6</version>
18 18
         </dependency>
19 19
         <dependency>
20
-            <groupId>com.mashape.unirest</groupId>
21
-            <artifactId>unirest-java</artifactId>
22
-            <version>1.4.9</version>
20
+            <groupId>com.squareup.okhttp3</groupId>
21
+            <artifactId>okhttp</artifactId>
22
+            <version>3.10.0</version>
23 23
         </dependency>
24 24
     </dependencies>
25 25
 

+ 5
- 1
Client/src/main/java/Message.java View File

@@ -12,7 +12,11 @@ public class Message {
12 12
         this.toId = toId;
13 13
         this.message = message;
14 14
     }
15
-
15
+    public Message(String fromId, String toId, String message){
16
+        this.fromId = fromId;
17
+        this.toId = toId;
18
+        this.message = message;
19
+    }
16 20
     public String getSequence() {
17 21
         return sequence;
18 22
     }

+ 6
- 2
Client/src/main/java/SimpleShell.java View File

@@ -1,3 +1,5 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
2
+
1 3
 import java.io.BufferedReader;
2 4
 import java.io.IOException;
3 5
 import java.io.InputStream;
@@ -8,9 +10,11 @@ import java.util.List;
8 10
 public class SimpleShell {
9 11
 
10 12
 
11
-    public static void prettyPrint(String output) {
13
+    public static void prettyPrint(String output) throws IOException {
12 14
         // yep, make an effort to format things nicely, eh?
13
-        System.out.println(output);
15
+        ObjectMapper objectMapper = new ObjectMapper();
16
+        Object json = objectMapper.readValue(output, Object.class);
17
+        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
14 18
     }
15 19
     public static void main(String[] args) throws java.io.IOException {
16 20
 

+ 4
- 1
Client/src/main/java/UserId.java View File

@@ -9,7 +9,10 @@ public class UserId {
9 9
         this.name = name;
10 10
         this.gitHub = gitHub;
11 11
     }
12
-
12
+    public UserId(String name, String gitHub){
13
+        this.name = name;
14
+        this.gitHub = gitHub;
15
+    }
13 16
     public String getUserId() {
14 17
         return userId;
15 18
     }

+ 52
- 17
Client/src/main/java/YouAreEll.java View File

@@ -1,43 +1,78 @@
1
-import com.mashape.unirest.http.Unirest;
2
-import com.mashape.unirest.http.exceptions.UnirestException;
1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.databind.ObjectMapper;
3
+import okhttp3.*;
4
+
5
+import java.io.IOException;
3 6
 
4 7
 public class YouAreEll {
5 8
 
6 9
     private String url = "http://zipcode.rocks:8085";
10
+    OkHttpClient client;
11
+    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
12
+    ObjectMapper objectMapper;
7 13
 
8 14
     YouAreEll() {
15
+        objectMapper = new ObjectMapper();
16
+         client = new OkHttpClient();
9 17
     }
10 18
 
11
-    public static void main(String[] args) {
19
+    public static void main(String[] args) throws IOException {
12 20
         YouAreEll urlhandler = new YouAreEll();
21
+        ObjectMapper mapper = new ObjectMapper();
22
+        UserId me = new UserId("Larry", "wulawrence");
23
+        String payload = mapper.writeValueAsString(me);
24
+//        urlhandler.MakeURLCall("/ids", "POST", payload);
25
+        Message message = new Message("wulawrence", "JoeHendricks415", "Look behind you");
26
+        String messageload = mapper.writeValueAsString(message);
27
+//        urlhandler.MakeURLCall("/ids/wulawrence/messages", "POST", messageload);
28
+
13 29
         System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
14 30
         System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
15 31
     }
16
-
17
-    public String get_ids() {
32
+    public String get_ids() throws IOException {
18 33
         return MakeURLCall("/ids", "GET", "");
19 34
     }
20
-
21
-    public String get_messages() {
35
+    public String get_messages() throws IOException {
22 36
         return MakeURLCall("/messages", "GET", "");
23 37
     }
38
+    public String postName(String payload) throws IOException {
39
+        return MakeURLCall("/ids", "POST", payload);
40
+    }
41
+    public String postMessage(String payload) throws IOException {
42
+        return MakeURLCall("/ids/wulawrence/messages", "POST", payload);
43
+    }
44
+    public String putName(String payload) throws IOException {
45
+        return MakeURLCall("/ids", "PUT", payload);
46
+    }
47
+    public String putMessage(String payload) throws IOException {
48
+        return MakeURLCall("/ids/wulawrence/messages", "PUT", payload);
49
+    }
24 50
 
25
-    public String MakeURLCall(String mainurl, String method, String jpayload) {
51
+    public String MakeURLCall(String mainurl, String method, String jpayload) throws IOException{
26 52
         String fullUrl = url + mainurl;
27 53
 
28
-        if (method.equalsIgnoreCase("get")){
29
-            try {
30
-                return String.valueOf(Unirest.get(fullUrl).asString());
31
-            } catch (UnirestException e) {
32
-                e.printStackTrace();
33
-            }
54
+
55
+        if (method.equalsIgnoreCase("GET")){
56
+            RequestBody body = RequestBody.create(JSON, jpayload);
57
+            Request request = new Request.Builder().url(fullUrl).build();
58
+            Response response = client.newCall(request).execute();
59
+            return response.body().string();
34 60
         }
35
-        if (method.equalsIgnoreCase("put")){
61
+        if (method.equalsIgnoreCase("POST")){
62
+            RequestBody body = RequestBody.create(JSON, jpayload);
63
+            Request request = new Request.Builder().url(fullUrl).post(body).build();
64
+            Response response = client.newCall(request).execute();
65
+            return response.body().string();
36 66
 
37 67
         }
38
-        if (method.equalsIgnoreCase("post")){
68
+
69
+        if (method.equalsIgnoreCase("PUT")){
70
+            RequestBody body = RequestBody.create(JSON, jpayload);
71
+            Request request = new Request.Builder().url(fullUrl).put(body).build();
72
+            Response response = client.newCall(request).execute();
73
+            return response.body().string();
39 74
 
40 75
         }
41
-        return "nada";
76
+        return "invald method: " + method;
42 77
     }
43 78
 }