Browse Source

Post Messages and Get Ids

Garrett Arant 6 years ago
parent
commit
258b0eb0cc

+ 9
- 2
Client/Client.iml View File

@@ -5,8 +5,6 @@
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">
7 7
       <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8
-      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9
-      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10 8
       <excludeFolder url="file://$MODULE_DIR$/target" />
11 9
     </content>
12 10
     <orderEntry type="inheritedJdk" />
@@ -14,5 +12,14 @@
14 12
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
15 13
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
16 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" />
17 24
   </component>
18 25
 </module>

+ 46
- 0
Client/src/main/java/ID.java View File

@@ -0,0 +1,46 @@
1
+public class ID {
2
+
3
+    private String userid;
4
+    private String name;
5
+    private String github;
6
+
7
+    public ID(String userid, String name, String github) {
8
+        this.userid = userid;
9
+        this.name = name;
10
+        this.github = github;
11
+    }
12
+
13
+    public ID(String name, String github) {
14
+        this.name = name;
15
+        this.github = github;
16
+    }
17
+
18
+    public String getUserid() {
19
+        return userid;
20
+    }
21
+
22
+    public void setUserid(String userid) {
23
+        this.userid = userid;
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 getGithub() {
35
+        return github;
36
+    }
37
+
38
+    public void setGithub(String github) {
39
+        this.github = github;
40
+    }
41
+
42
+    @Override
43
+    public String toString(){
44
+        return "UserID: " + userid + " Name: " + name + " Github: " + github;
45
+    }
46
+}

+ 62
- 0
Client/src/main/java/Messages.java View File

@@ -0,0 +1,62 @@
1
+public class Messages {
2
+
3
+    private String sequence;
4
+    private String timestamp;
5
+    private String fromid;
6
+    private String toid;
7
+    private String message;
8
+
9
+    public Messages(String sequence, String timestamp, String fromid, String toid, String message) {
10
+        this.sequence = sequence;
11
+        this.timestamp = timestamp;
12
+        this.fromid = fromid;
13
+        this.toid = toid;
14
+        this.message = message;
15
+    }
16
+
17
+    public Messages(String fromid, String toid, String message) {
18
+        this.fromid = fromid;
19
+        this.toid = toid;
20
+        this.message = message;
21
+    }
22
+
23
+    public String getSequence() {
24
+        return sequence;
25
+    }
26
+
27
+    public void setSequence(String sequence) {
28
+        this.sequence = sequence;
29
+    }
30
+
31
+    public String getTimestamp() {
32
+        return timestamp;
33
+    }
34
+
35
+    public void setTimestamp(String timestamp) {
36
+        this.timestamp = timestamp;
37
+    }
38
+
39
+    public String getFromid() {
40
+        return fromid;
41
+    }
42
+
43
+    public void setFromid(String fromid) {
44
+        this.fromid = fromid;
45
+    }
46
+
47
+    public String getToid() {
48
+        return toid;
49
+    }
50
+
51
+    public void setToid(String toid) {
52
+        this.toid = toid;
53
+    }
54
+
55
+    public String getMessage() {
56
+        return message;
57
+    }
58
+
59
+    public void setMessage(String message) {
60
+        this.message = message;
61
+    }
62
+}

+ 50
- 1
Client/src/main/java/YouAreEll.java View File

@@ -1,14 +1,39 @@
1
+
2
+import com.fasterxml.jackson.core.JsonProcessingException;
3
+import com.fasterxml.jackson.databind.ObjectMapper;
4
+import com.mashape.unirest.http.Unirest;
5
+import com.mashape.unirest.http.exceptions.UnirestException;
6
+import com.mashape.unirest.request.GetRequest;
7
+
1 8
 public class YouAreEll {
2 9
 
3 10
     YouAreEll() {
4 11
     }
5 12
 
6
-    public static void main(String[] args) {
13
+    public static void main(String[] args) throws JsonProcessingException {
7 14
         YouAreEll urlhandler = new YouAreEll();
8 15
         System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9 16
         System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
17
+        urlhandler.postID("G", "Github");
18
+        urlhandler.postMessage("gjarant", "bell7692", "Is it working?");
19
+    }
20
+
21
+    public void postID(String name, String github) throws JsonProcessingException {
22
+        ObjectMapper objectMapper = new ObjectMapper();
23
+        ID idTest = new ID( name, github);
24
+        String payload = objectMapper.writeValueAsString(idTest);
25
+        MakeURLCall("/ids", "POST", payload);
10 26
     }
11 27
 
28
+    public void postMessage(String fromId, String toid, String message) throws JsonProcessingException {
29
+        ObjectMapper objectMapper = new ObjectMapper();
30
+        Messages messagesTest = new Messages(fromId, toid, message);
31
+        String payload = objectMapper.writeValueAsString(messagesTest);
32
+        String url = "/ids/" + toid + "/messages";
33
+        MakeURLCall(url, "POST", payload);
34
+    }
35
+
36
+
12 37
     public String get_ids() {
13 38
         return MakeURLCall("/ids", "GET", "");
14 39
     }
@@ -18,6 +43,30 @@ public class YouAreEll {
18 43
     }
19 44
 
20 45
     public String MakeURLCall(String mainurl, String method, String jpayload) {
46
+        String completeURL = "http://zipcode.rocks:8085" + mainurl;
47
+        GetRequest request = Unirest.get(completeURL);
48
+
49
+        if (method.equals("GET")) {
50
+            try {
51
+                return request.asJson().getBody().toString();
52
+            } catch (UnirestException e) {
53
+                e.printStackTrace();
54
+            }
55
+        }
56
+        else if (method.equals("POST")){
57
+            try {
58
+                return Unirest.post(completeURL).body(jpayload).asJson().getBody().toString();
59
+            } catch (UnirestException e) {
60
+                e.printStackTrace();
61
+            }
62
+        }
63
+//        else if (method.equals("PUT")){
64
+//            try {
65
+//                return Unirest.put(completeURL).body(jpayload).asJson().getBody().toString();
66
+//            } catch (UnirestException e) {
67
+//                e.printStackTrace();
68
+//            }
69
+//        }
21 70
         return "nada";
22 71
     }
23 72
 }

+ 9
- 0
YouAreEll.iml View File

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

+ 5
- 0
pom.xml View File

@@ -18,5 +18,10 @@
18 18
             <artifactId>jackson-databind</artifactId>
19 19
             <version>2.8.6</version>
20 20
         </dependency>
21
+        <dependency>
22
+            <groupId>com.mashape.unirest</groupId>
23
+            <artifactId>unirest-java</artifactId>
24
+            <version>1.4.9</version>
25
+        </dependency>
21 26
     </dependencies>
22 27
 </project>