|
@@ -1,6 +1,6 @@
|
1
|
|
-import com.fasterxml.jackson.annotation.JsonValue;
|
2
|
1
|
import ids.Id;
|
3
|
2
|
import org.apache.http.HttpResponse;
|
|
3
|
+import org.apache.http.client.ClientProtocolException;
|
4
|
4
|
import org.apache.http.client.HttpClient;
|
5
|
5
|
import org.apache.http.client.methods.HttpGet;
|
6
|
6
|
import org.apache.http.client.methods.HttpPost;
|
|
@@ -15,57 +15,60 @@ import java.io.InputStreamReader;
|
15
|
15
|
public class YouAreEll {
|
16
|
16
|
private HttpClient client;
|
17
|
17
|
private final String baseUrl = "http://zipcode.rocks:8085";
|
|
18
|
+
|
18
|
19
|
YouAreEll() {
|
19
|
20
|
this.client = HttpClientBuilder.create().build();
|
20
|
21
|
}
|
21
|
22
|
|
22
|
23
|
|
23
|
|
-
|
24
|
24
|
public static void main(String[] args) throws IOException {
|
25
|
25
|
YouAreEll urlhandler = new YouAreEll();
|
26
|
|
- Id luna = new Id("ea795ee660116450ae935a1374166a142b263e64", "new-name", "new-github");
|
|
26
|
+ Id luna = new Id("ea795ee660116450ae935a1374166a142b263e64", "Reset-Me", "new-github");
|
27
|
27
|
// String response = urlhandler.MakeURLCall("/ids", "GET", "");
|
28
|
|
- System.out.println(luna.toString());
|
29
|
|
- String postResponse = urlhandler.MakeURLCall("/ids", "PUT", luna.toString());
|
|
28
|
+// System.out.println(luna.toString());
|
|
29
|
+ String getResponse = urlhandler.get_ids();
|
|
30
|
+ PrettyPrint.prettyPrintJsonArray(getResponse);
|
30
|
31
|
// this should be dematerialized to obtain info inside the json object
|
31
|
|
- System.out.println(postResponse);
|
|
32
|
+// PrettyPrint.prettyPrintSingle(postResponse);
|
32
|
33
|
// multiple ids with the same github
|
33
|
34
|
// with the put, how to update without knowing the id?
|
34
|
35
|
// SimpleShell.prettyPrint(postResponse);
|
35
|
|
-// SimpleShell.prettyPrint("{\"userid\":\"\\\"\\\"\",\"name\":\"uhaushasuh\",\"githubName\":\"hasdhadhdash\"}");
|
36
|
|
- // System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
|
|
36
|
+// SimpleShell.prettyPrint("{\"userid\":\"\\\"\\\"\",\"name\":\"uhaushasuh\",\"github\":\"hasdhadhdash\"}");
|
|
37
|
+ // System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
|
37
|
38
|
}
|
38
|
39
|
|
39
|
|
- public String get_ids() {
|
|
40
|
+ public String get_ids() throws IOException {
|
40
|
41
|
return MakeURLCall("/ids", "GET", "");
|
41
|
42
|
}
|
42
|
43
|
|
43
|
|
- public String get_messages() {
|
|
44
|
+ public String get_messages() throws IOException {
|
44
|
45
|
return MakeURLCall("/messages", "GET", "");
|
45
|
46
|
}
|
46
|
47
|
|
47
|
|
- public String MakeURLCall(String mainurl, String method, String jpayload) {
|
48
|
|
- switch(method) {
|
|
48
|
+ public String MakeURLCall(String mainurl, String method, String jpayload) throws IOException {
|
|
49
|
+ switch (method) {
|
49
|
50
|
case "GET":
|
|
51
|
+
|
50
|
52
|
HttpGet request = new HttpGet(baseUrl + mainurl);
|
51
|
53
|
try {
|
|
54
|
+// request.setEntity(new StringEntity(jpayload));
|
52
|
55
|
HttpResponse response = client.execute(request);
|
53
|
|
- // response.getStatusLine().getStatusCode(); // 200 OK, 404 Not Found, etc.
|
54
|
|
-
|
55
|
56
|
BufferedReader rd = new BufferedReader(
|
56
|
57
|
new InputStreamReader(response.getEntity().getContent()));
|
57
|
58
|
|
|
59
|
+ // response.getStatusLine().getStatusCode(); // 200 OK, 404 Not Found, etc.
|
58
|
60
|
StringBuilder responseString = new StringBuilder();
|
59
|
61
|
String line;
|
60
|
62
|
|
61
|
63
|
while ((line = rd.readLine()) != null) {
|
62
|
64
|
responseString.append(line);
|
63
|
65
|
}
|
64
|
|
- //System.out.println("response: " + responseString);
|
|
66
|
+
|
65
|
67
|
return responseString.toString();
|
66
|
|
- } catch(IOException e) {
|
|
68
|
+ } catch (IOException e) {
|
67
|
69
|
return ("An unexpected IO error occurred: " + e.getMessage());
|
68
|
70
|
}
|
|
71
|
+
|
69
|
72
|
case "POST":
|
70
|
73
|
HttpPost postReq = new HttpPost(baseUrl + mainurl);
|
71
|
74
|
try {
|
|
@@ -85,6 +88,7 @@ public class YouAreEll {
|
85
|
88
|
} catch (IOException e) {
|
86
|
89
|
return ("An unexpected IO error occurred: " + e.getMessage());
|
87
|
90
|
}
|
|
91
|
+
|
88
|
92
|
case "PUT":
|
89
|
93
|
HttpPut putReq = new HttpPut(baseUrl + mainurl);
|
90
|
94
|
try {
|
|
@@ -108,4 +112,49 @@ public class YouAreEll {
|
108
|
112
|
return "nada";
|
109
|
113
|
}
|
110
|
114
|
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+ private String getCall(String url) {
|
|
118
|
+
|
|
119
|
+ HttpGet request = new HttpGet("http://zipcode.rocks:8085/ids");
|
|
120
|
+ try {
|
|
121
|
+ HttpResponse response = client.execute(request);
|
|
122
|
+ return readCalls(response);
|
|
123
|
+ } catch (IOException e) {
|
|
124
|
+ return ("Unhandled Exception error");
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+ public String postCall(HttpPost post, String payload) throws IOException {
|
|
130
|
+ try {
|
|
131
|
+ post.setEntity(new StringEntity(payload));
|
|
132
|
+ HttpResponse response = client.execute(post);
|
|
133
|
+ return readCalls(response);
|
|
134
|
+ } catch (IOException e) {
|
|
135
|
+ return ("An unexpected IO error occurred: " + e.getMessage());
|
|
136
|
+ }
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ private String readCalls(HttpResponse response) throws IOException {
|
|
140
|
+ try {
|
|
141
|
+ BufferedReader rd = new BufferedReader(
|
|
142
|
+ new InputStreamReader(response.getEntity().getContent()));
|
|
143
|
+
|
|
144
|
+ StringBuilder responseString = new StringBuilder();
|
|
145
|
+ String line;
|
|
146
|
+
|
|
147
|
+ while ((line = rd.readLine()) != null) {
|
|
148
|
+ responseString.append(line);
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ return responseString.toString();
|
|
152
|
+
|
|
153
|
+ } catch (IOException e) {
|
|
154
|
+ return ("An unexpected IO error occurred: " + e.getMessage());
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ }
|
111
|
158
|
}
|
|
159
|
+
|
|
160
|
+
|