|
@@ -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
|
}
|