33
44import com .google .gson .Gson ;
55import com .google .gson .reflect .TypeToken ;
6- import org .jetbrains .annotations .NotNull ;
76
8- import java .io .*;
9- import java .lang .reflect .Type ;
10- import java .net .HttpURLConnection ;
11- import java .nio .charset .StandardCharsets ;
7+ import java .net .URISyntaxException ;
8+ import java .net .URL ;
9+ import java .net .http .HttpRequest ;
1210import java .util .HashMap ;
1311import java .util .Map ;
14- import java .util .Objects ;
15- import java .util .stream .Collectors ;
1612
17- public abstract class APIRequest <Datatype > {
13+ public abstract class APIRequest <Response > {
1814 /**
19- * exaroton API client
15+ * Build the HttpRequest
16+ * @param builder HttpRequest builder with preconfigured options
17+ * @param baseUrl base URL
18+ * @return HttpRequest
19+ * @throws URISyntaxException if the constructed URI is invalid
2020 */
21- protected final ExarotonClient client ;
21+ public HttpRequest build (Gson gson , HttpRequest .Builder builder , URL baseUrl ) throws URISyntaxException {
22+ builder .uri (baseUrl .toURI ().resolve (getPath ()))
23+ .method (this .getMethod (), getBodyPublisher (gson , builder ));
2224
23- /**
24- * Gson instance used for (de-)serialization
25- */
26- protected final Gson gson ;
25+ for (Map .Entry <String , String > header : this .getHeaders ().entrySet ()) {
26+ builder .header (header .getKey (), header .getValue ());
27+ }
2728
28- public APIRequest (@ NotNull ExarotonClient client , @ NotNull Gson gson ) {
29- this .client = Objects .requireNonNull (client );
30- this .gson = Objects .requireNonNull (gson );
29+ return builder .build ();
3130 }
3231
3332 /**
@@ -63,78 +62,6 @@ protected String getPath() {
6362 return path ;
6463 }
6564
66- /**
67- * Execute this API Request and get the raw InputStream
68- * @return InputStream
69- * @throws APIException if the request fails
70- */
71- public InputStream requestRaw () throws APIException {
72- HttpURLConnection connection = null ;
73- InputStream stream ;
74- try {
75- connection = client .createConnection (this .getMethod (), this .getPath ());
76- for (Map .Entry <String , String > entry : this .getHeaders ().entrySet ()) {
77- connection .setRequestProperty (entry .getKey (), entry .getValue ());
78- }
79-
80- Object body = this .getBody ();
81- InputStream inputStream = this .getInputStream ();
82- if (body != null ) {
83- inputStream = new ByteArrayInputStream (gson .toJson (body ).getBytes (StandardCharsets .UTF_8 ));
84- connection .setRequestProperty ("Content-Type" , "application/json" );
85- }
86-
87- if (inputStream != null ) {
88- connection .setDoOutput (true );
89- OutputStream out = connection .getOutputStream ();
90- byte [] buf = new byte [8192 ];
91- int length ;
92- while ((length = inputStream .read (buf )) > 0 ) {
93- out .write (buf , 0 , length );
94- }
95- }
96- stream = connection .getInputStream ();
97- }
98- catch (IOException e ) {
99- if (connection == null || connection .getErrorStream () == null ) {
100- throw new APIException ("Failed to request data from exaroton API" , e );
101- }
102-
103- stream = connection .getErrorStream ();
104- }
105-
106- return stream ;
107- }
108-
109- /**
110- * Execute this API Request and get the response as a String
111- * @return response as a String
112- * @throws APIException if the request fails
113- */
114- public String requestString () throws APIException {
115- try (InputStream stream = this .requestRaw ()) {
116- return new BufferedReader (new InputStreamReader (stream , StandardCharsets .UTF_8 ))
117- .lines ()
118- .collect (Collectors .joining ("\n " ));
119- }
120- catch (IOException e ) {
121- throw new APIException ("Failed to read input stream" , e );
122- }
123- }
124-
125- /**
126- * Execute this API Request and parse the API response
127- * @return Parsed API response
128- * @throws APIException if the request fails
129- */
130- public APIResponse <Datatype > request () throws APIException {
131- String json = this .requestString ();
132- APIResponse <Datatype > response = gson .fromJson (json , this .getType ());
133- if (!response .isSuccess ()) throw new APIException (response .getError ());
134-
135- return response ;
136- }
137-
13865 /**
13966 * @return request headers
14067 */
@@ -148,7 +75,7 @@ protected HashMap<String, String> getHeaders() {
14875 * get the type required for parsing the JSON response
14976 * @return response type
15077 */
151- protected abstract TypeToken <APIResponse <Datatype >> getType ();
78+ protected abstract TypeToken <APIResponse <Response >> getType ();
15279
15380 /**
15481 * data that will be replaced in the endpoint
@@ -159,17 +86,24 @@ protected HashMap<String, String> getData() {
15986 }
16087
16188 /**
162- * @return input stream with data that should be sent to the request
89+ * Get the body publisher for the request
90+ * @param gson gson instance
91+ * @param builder request builder to set the Content-Type header
92+ * @param body request body
93+ * @return a body publisher
16394 */
164- protected InputStream getInputStream () {
165- return null ;
95+ protected HttpRequest .BodyPublisher jsonBodyPublisher (Gson gson , HttpRequest .Builder builder , Object body ) {
96+ builder .header ("Content-Type" , "application/json" );
97+ return HttpRequest .BodyPublishers .ofString (gson .toJson (body ));
16698 }
16799
168100 /**
169- * Get the request body
170- * @return request body
101+ * Get the body publisher for the request
102+ * @param gson gson instance
103+ * @param builder request builder which can be used to set a Content-Type header
104+ * @return a body publisher
171105 */
172- protected Object getBody ( ) {
173- return null ;
106+ protected HttpRequest . BodyPublisher getBodyPublisher ( Gson gson , HttpRequest . Builder builder ) {
107+ return HttpRequest . BodyPublishers . noBody () ;
174108 }
175109}
0 commit comments