Mitch Taylor před 6 roky
rodič
revize
d60d232d46
2 změnil soubory, kde provedl 61 přidání a 6 odebrání
  1. 1
    1
      Client/Client.iml
  2. 60
    5
      Client/src/main/java/YouAreEll.java

+ 1
- 1
Client/Client.iml Zobrazit soubor

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">

+ 60
- 5
Client/src/main/java/YouAreEll.java Zobrazit soubor

@@ -1,12 +1,33 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
2
+import okhttp3.*;
3
+
4
+import java.io.IOException;
5
+
1 6
 public class YouAreEll {
2 7
 
8
+    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
9
+    private final String baseUrl = "http://zipcode.rocks:8085";
10
+    private OkHttpClient client;
11
+    private ObjectMapper mapper;
12
+    private String fromId;
13
+
3 14
     YouAreEll() {
15
+        client = new OkHttpClient();
16
+        mapper = new ObjectMapper();
17
+        fromId = "someone";
4 18
     }
5 19
 
6 20
     public static void main(String[] args) {
7
-        YouAreEll urlhandler = new YouAreEll();
8
-        System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9
-        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
21
+        YouAreEll urlHandler = new YouAreEll();
22
+        ObjectMapper mapper = new ObjectMapper();
23
+    }
24
+
25
+    public String getFromId() {
26
+        return fromId;
27
+    }
28
+
29
+    public void setFromId(String fromId) {
30
+        this.fromId = fromId;
10 31
     }
11 32
 
12 33
     public String get_ids() {
@@ -17,7 +38,41 @@ public class YouAreEll {
17 38
         return MakeURLCall("/messages", "GET", "");
18 39
     }
19 40
 
20
-    public String MakeURLCall(String mainurl, String method, String jpayload) {
21
-        return "nada";
41
+    public String MakeURLCall(String mainUrl, String method, String jpayload) {
42
+        String fullUrl = baseUrl + mainUrl;
43
+        Request request = null;
44
+
45
+        if (method.equalsIgnoreCase("get")) {
46
+            request = new Request.Builder()
47
+                    .url(fullUrl)
48
+                    .build();
49
+        }
50
+
51
+
52
+        if (method.equalsIgnoreCase("POST")) {
53
+            RequestBody body = RequestBody.create(JSON, jpayload);
54
+            request = new Request.Builder()
55
+                    .url(fullUrl)
56
+                    .post(body)
57
+                    .build();
58
+        }
59
+
60
+        if (method.equalsIgnoreCase("PUT")) {
61
+            RequestBody body = RequestBody.create(JSON, jpayload);
62
+            request = new Request.Builder()
63
+                    .url(fullUrl)
64
+                    .put(body)
65
+                    .build();
66
+        }
67
+
68
+        if (request != null) {
69
+            try (Response response = client.newCall(request).execute()) {
70
+                return response.body().string();
71
+            } catch (IOException e) {
72
+                e.printStackTrace();
73
+            }
74
+        }
75
+
76
+        return "error: bad method " + method;
22 77
     }
23 78
 }