77import java .net .HttpURLConnection ;
88import java .net .URL ;
99import java .net .UnknownHostException ;
10+ import java .util .List ;
11+ import java .util .Map ;
12+
13+ import javax .json .Json ;
14+ import javax .json .JsonArray ;
15+ import javax .json .JsonObject ;
1016
1117import javajs .http .HttpClient ;
1218import javajs .http .HttpClient .HttpRequest ;
@@ -27,14 +33,20 @@ public class Test_HTTP extends Test_ {
2733 */
2834
2935 }
36+
3037 @ SuppressWarnings ("unused" )
3138 public static void main (String [] args ) {
3239
40+ try {
41+ getAPIJson ("2023-05-06" );
42+ } catch (IOException e ) {
43+ e .printStackTrace ();
44+ }
3345 HttpClient client = HttpClientFactory .getClient (null );
3446 HttpRequest req = null ;
3547
3648 System .out .println ("Testing localhost:5000 from 8000" );
37-
49+
3850// try {
3951// URL url = new URL("http://localhost:5000/t.txt");
4052// HttpURLConnection c = (HttpURLConnection) url.openConnection();
@@ -47,10 +59,8 @@ public static void main(String[] args) {
4759// System.out.println(e);
4860// }
4961
50-
51-
5262 System .out .println ("Testing httpstat 405" );
53-
63+
5464 try {
5565 URL url = new URL ("http://httpstat.us/405" );
5666 HttpURLConnection c = (HttpURLConnection ) url .openConnection ();
@@ -63,9 +73,8 @@ public static void main(String[] args) {
6373 System .out .println (e );
6474 }
6575
66-
6776 System .out .println ("Testing httpstat 201" );
68-
77+
6978 try {
7079 URL url = new URL ("http://httpstat.us/201" );
7180 HttpURLConnection c = (HttpURLConnection ) url .openConnection ();
@@ -78,7 +87,6 @@ public static void main(String[] args) {
7887 System .out .println (e );
7988 }
8089
81-
8290 System .out .println ("Testing unknown host" );
8391
8492 try {
@@ -87,12 +95,11 @@ public static void main(String[] args) {
8795 int code = c .getResponseCode ();
8896 InputStream oi = url .openStream ();
8997 } catch (IOException e ) {
90- assert (e instanceof UnknownHostException );
98+ assert (e instanceof UnknownHostException );
9199 }
92-
93100
94101 System .out .println ("Testing sync GET" );
95-
102+
96103 try {
97104 req = javajs .http .SimpleHttpClient .createRequest (client , "get" ,
98105 "https://www.compbio.dundee.ac.uk/slivka/api/services" );
@@ -106,9 +113,8 @@ public static void main(String[] args) {
106113 System .out .println ("Testing async GET" );
107114 doAsync ("async GET" , req );
108115
109-
110116 System .out .println ("Testing sync POST" );
111-
117+
112118 try {
113119 req = javajs .http .SimpleHttpClient .createRequest (client , "post" ,
114120 "https://www.compbio.dundee.ac.uk/slivka/api/services/example" );
@@ -120,10 +126,9 @@ public static void main(String[] args) {
120126 } catch (IOException e ) {
121127 System .err .println (e );
122128 }
123-
124-
129+
125130 System .out .println ("Testing sync PUT" );
126-
131+
127132 try {
128133 req = javajs .http .SimpleHttpClient .createRequest (client , "put" ,
129134 "https://www.compbio.dundee.ac.uk/slivka/api/services/example" );
@@ -135,14 +140,41 @@ public static void main(String[] args) {
135140 } catch (IOException e ) {
136141 System .err .println (e );
137142 }
138-
139-
143+
140144 System .out .println ("Testing async PUT, reuse of req" );
141-
145+
142146 req .addFormPart ("testing" , "here" );
143147 doAsync ("async PUT" , req );
144148 }
145149
150+ private static void getAPIJson (String date ) throws IOException {
151+ URL url = new URL ("<url here>" + date );
152+ HttpURLConnection c = (HttpURLConnection ) url .openConnection ();
153+ c .addRequestProperty ("Authorization" , "<code here>" );
154+ int code = c .getResponseCode ();
155+ Map <String , List <String >> header = c .getHeaderFields ();
156+ for (Map .Entry <String , List <String >> e : header .entrySet ()) {
157+ System .out .println (e .getKey () + "=" + e .getValue ());
158+ }
159+
160+ InputStream is = c .getInputStream ();
161+ JsonObject json = Json .createReader (is ).readObject ();
162+ is .close ();
163+ JsonArray fields = json .getJsonArray ("fields" );
164+ for (int i = 0 ; i < fields .size (); i ++) {
165+ JsonObject field = fields .getJsonObject (i );
166+ String duty = field .getString ("fieldName" );
167+ if (duty .startsWith ("Future" ))
168+ continue ;
169+ JsonArray signups = field .getJsonArray ("signups" );
170+ for (int j = 0 ; j < signups .size (); j ++) {
171+ JsonObject who = signups .getJsonObject (j );
172+ String name = who .getString ("userFirstName" ) + " " + who .getString ("userName" );
173+ System .out .println (date + "\t " + duty + "\t " + name );
174+ }
175+ }
176+ }
177+
146178 private static void doAsync (String msg , HttpRequest req ) {
147179 req .executeAsync ((resp ) -> {
148180 System .out .println (msg + " returned SUCCESS:" );
0 commit comments