|
@@ -1,23 +1,78 @@
|
|
1
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
2
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
3
|
+import okhttp3.*;
|
|
4
|
+
|
|
5
|
+import java.io.IOException;
|
|
6
|
+
|
1
|
7
|
public class YouAreEll {
|
2
|
8
|
|
|
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;
|
|
13
|
+
|
3
|
14
|
YouAreEll() {
|
|
15
|
+ objectMapper = new ObjectMapper();
|
|
16
|
+ client = new OkHttpClient();
|
4
|
17
|
}
|
5
|
18
|
|
6
|
|
- public static void main(String[] args) {
|
|
19
|
+ public static void main(String[] args) throws IOException {
|
7
|
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
|
+
|
8
|
29
|
System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
|
9
|
30
|
System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
|
10
|
31
|
}
|
11
|
|
-
|
12
|
|
- public String get_ids() {
|
|
32
|
+ public String get_ids() throws IOException {
|
13
|
33
|
return MakeURLCall("/ids", "GET", "");
|
14
|
34
|
}
|
15
|
|
-
|
16
|
|
- public String get_messages() {
|
|
35
|
+ public String get_messages() throws IOException {
|
17
|
36
|
return MakeURLCall("/messages", "GET", "");
|
18
|
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
|
+ }
|
|
50
|
+
|
|
51
|
+ public String MakeURLCall(String mainurl, String method, String jpayload) throws IOException{
|
|
52
|
+ String fullUrl = url + mainurl;
|
|
53
|
+
|
|
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();
|
|
60
|
+ }
|
|
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();
|
|
66
|
+
|
|
67
|
+ }
|
|
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();
|
19
|
74
|
|
20
|
|
- public String MakeURLCall(String mainurl, String method, String jpayload) {
|
21
|
|
- return "nada";
|
|
75
|
+ }
|
|
76
|
+ return "invald method: " + method;
|
22
|
77
|
}
|
23
|
78
|
}
|