66import com .azuriom .azauth .gson .UuidAdapter ;
77import com .azuriom .azauth .model .ErrorResponse ;
88import com .azuriom .azauth .model .User ;
9- import com .google .gson .FieldNamingPolicy ;
10- import com .google .gson .Gson ;
11- import com .google .gson .GsonBuilder ;
12- import com .google .gson .JsonObject ;
13- import com .google .gson .JsonParseException ;
9+ import com .google .gson .*;
1410import org .jetbrains .annotations .Blocking ;
1511import org .jetbrains .annotations .Contract ;
1612import org .jetbrains .annotations .NotNull ;
2218import java .io .InputStreamReader ;
2319import java .io .OutputStream ;
2420import java .net .HttpURLConnection ;
25- import java .net .URL ;
21+ import java .net .URI ;
2622import java .nio .charset .StandardCharsets ;
2723import java .time .Instant ;
2824import java .util .Objects ;
@@ -95,8 +91,8 @@ public AuthClient(@NotNull String url) {
9591 */
9692 @ Blocking
9793 public @ NotNull AuthResult <@ NotNull User > login (@ NotNull String email ,
98- @ NotNull String password ,
99- @ Nullable String code2fa ) throws AuthException {
94+ @ NotNull String password ,
95+ @ Nullable String code2fa ) throws AuthException {
10096 return this .login (email , password , code2fa , User .class );
10197 }
10298
@@ -112,8 +108,8 @@ public AuthClient(@NotNull String url) {
112108 */
113109 @ Blocking
114110 public <T > @ NotNull AuthResult <@ NotNull T > login (@ NotNull String email ,
115- @ NotNull String password ,
116- @ NotNull Class <T > responseType ) throws AuthException {
111+ @ NotNull String password ,
112+ @ NotNull Class <T > responseType ) throws AuthException {
117113 return login (email , password , (String ) null , responseType );
118114 }
119115
@@ -189,9 +185,9 @@ public AuthClient(@NotNull String url) {
189185 */
190186 @ Blocking
191187 public <T > @ NotNull AuthResult <@ NotNull T > login (@ NotNull String email ,
192- @ NotNull String password ,
193- @ Nullable String code2fa ,
194- @ NotNull Class <T > responseType ) throws AuthException {
188+ @ NotNull String password ,
189+ @ Nullable String code2fa ,
190+ @ NotNull Class <T > responseType ) throws AuthException {
195191 JsonObject body = new JsonObject ();
196192 body .addProperty ("email" , email );
197193 body .addProperty ("password" , password );
@@ -253,26 +249,17 @@ public void logout(@NotNull String accessToken) throws AuthException {
253249
254250 @ Blocking
255251 @ Contract ("_, _, null -> null; _, _, !null -> !null" )
256- private <T > AuthResult <T > post (@ NotNull String endPoint , @ NotNull JsonObject body ,
252+ private <T > AuthResult <T > post (@ NotNull String endpoint ,
253+ @ NotNull JsonObject body ,
257254 @ Nullable Class <T > responseType ) throws AuthException {
258255 try {
259- return this .doPost (endPoint , body , responseType );
260- } catch (IOException e ) {
261- throw new AuthException (e );
262- }
263- }
264-
265- @ Blocking
266- @ Contract ("_, _, null -> null; _, _, !null -> !null" )
267- private <T > AuthResult <T > doPost (@ NotNull String endPoint , @ NotNull JsonObject body , @ Nullable Class <T > responseType )
268- throws AuthException , IOException {
269- try {
270- URL apiUrl = new URL (this .url + "/api/auth/" + endPoint );
271- HttpURLConnection connection = (HttpURLConnection ) apiUrl .openConnection ();
256+ URI api = URI .create (this .url + "/api/auth/" + endpoint );
257+ HttpURLConnection connection = (HttpURLConnection ) api .toURL ().openConnection ();
272258 connection .setRequestMethod ("POST" );
273259 connection .setDoOutput (true );
274- connection .addRequestProperty ( "User-Agent" , "AzAuth authenticator v1" );
260+ connection .setUseCaches ( false );
275261 connection .addRequestProperty ("Content-Type" , "application/json; charset=utf-8" );
262+ connection .addRequestProperty ("User-Agent" , "AzAuth authenticator v1" );
276263
277264 try (OutputStream out = connection .getOutputStream ()) {
278265 out .write (body .toString ().getBytes (StandardCharsets .UTF_8 ));
@@ -294,7 +281,8 @@ private <T> AuthResult<T> doPost(@NotNull String endPoint, @NotNull JsonObject b
294281 }
295282 }
296283
297- private <T > AuthResult <T > handleResponse (HttpURLConnection connection , Class <T > type ) throws AuthException , IOException {
284+ private <T > AuthResult <T > handleResponse (HttpURLConnection connection , Class <T > type )
285+ throws AuthException , IOException {
298286 try (BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()))) {
299287 T response = GSON .fromJson (reader , type );
300288
@@ -320,7 +308,7 @@ private <T> AuthResult<T> handleClientError(HttpURLConnection connection)
320308
321309 throw new AuthException (response .getMessage ());
322310 } catch (JsonParseException e ) {
323- throw new AuthException ("Invalid JSON response from API (http " + status + ")" );
311+ throw new AuthException ("Invalid JSON response from API (HTTP " + status + ")" , e );
324312 }
325313 }
326314}
0 commit comments