From 9d65c8516fd006678cd368f62f89db25e4bf8370 Mon Sep 17 00:00:00 2001 From: maxmielchen Date: Mon, 24 Mar 2025 23:25:28 +0100 Subject: [PATCH 1/3] fix request manager and refactor --- .../org/bytedream/untis4j/RequestManager.java | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/bytedream/untis4j/RequestManager.java b/src/main/java/org/bytedream/untis4j/RequestManager.java index 042f2df..dc95fc1 100755 --- a/src/main/java/org/bytedream/untis4j/RequestManager.java +++ b/src/main/java/org/bytedream/untis4j/RequestManager.java @@ -5,13 +5,11 @@ import org.json.JSONException; import org.json.JSONObject; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.HashMap; import java.util.Map; @@ -61,6 +59,29 @@ public static Infos generateUserInfosAndLogin(String username, String password, put("password", password); put("client", userAgent); }}); + StringBuilder stringBuilder = getStringBuilder(userAgent, url, requestBody); + + JSONObject jsonObject; + + try { + jsonObject = new JSONObject(stringBuilder.toString()); + + if (jsonObject.has("error")) { + JSONObject errorObject = jsonObject.getJSONObject("error"); + throw new LoginException("The response contains an error (" + errorObject.getInt("errorObject") + "): " + errorObject.getString("message")); + } + } catch (JSONException e) { + throw new IOException("An unexpected exception occurred: " + stringBuilder); + } + + JSONObject result = jsonObject.getJSONObject("result"); + + UntisUtils.ElementType elementType = UntisUtils.ElementType.of(result.getInt("personType")); + + return new Infos(username, password, server, schoolName, userAgent, result.getString("sessionId"), result.getInt("personId"), elementType, result.getInt("klasseId")); + } + + private static StringBuilder getStringBuilder(String userAgent, URL url, String requestBody) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); @@ -86,25 +107,7 @@ public static Infos generateUserInfosAndLogin(String username, String password, while ((line = input.readLine()) != null) { stringBuilder.append(line); } - - JSONObject jsonObject; - - try { - jsonObject = new JSONObject(stringBuilder.toString()); - - if (jsonObject.has("error")) { - JSONObject errorObject = jsonObject.getJSONObject("error"); - throw new LoginException("The response contains an error (" + errorObject.getInt("errorObject") + "): " + errorObject.getString("message")); - } - } catch (JSONException e) { - throw new IOException("An unexpected exception occurred: " + stringBuilder); - } - - JSONObject result = jsonObject.getJSONObject("result"); - - UntisUtils.ElementType elementType = UntisUtils.ElementType.of(result.getInt("personType")); - - return new Infos(username, password, server, schoolName, userAgent, result.getString("sessionId"), result.getInt("personId"), elementType, result.getInt("klasseId")); + return stringBuilder; } /** From 5a3f442ab8eda126301d4909662b6a645447aea2 Mon Sep 17 00:00:00 2001 From: maxmielchen Date: Mon, 24 Mar 2025 23:38:24 +0100 Subject: [PATCH 2/3] fix common error --- .../java/org/bytedream/untis4j/Session.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/bytedream/untis4j/Session.java b/src/main/java/org/bytedream/untis4j/Session.java index a45885a..a5b0e42 100755 --- a/src/main/java/org/bytedream/untis4j/Session.java +++ b/src/main/java/org/bytedream/untis4j/Session.java @@ -743,10 +743,38 @@ public Timetable getTimetable(LocalDate start, LocalDate end, UntisUtils.Element Timetable timetable = new Timetable(); - Classes c = getClasses(); - Teachers t = getTeachers(); - Subjects s = getSubjects(); - Rooms r = getRooms(); + Classes c; + Teachers t; + Subjects s; + Rooms r; + + try { + c = getClasses(); + } catch (IOException e) { + e.printStackTrace(); + c = null; + } + try { + t = getTeachers(); + } catch (IOException e) { + e.printStackTrace(); + t = null; + } + try { + s = getSubjects(); + } catch (IOException e) { + e.printStackTrace(); + s = null; + } + try { + r = getRooms(); + } catch (IOException e) { + e.printStackTrace(); + r = null; + } + + + TimeUnits timeUnits = getTimegridUnits().get(0).getTimeUnits(); From 56e113d0114fb6b28687001cc29f5aa5c724cd91 Mon Sep 17 00:00:00 2001 From: maxmielchen Date: Mon, 24 Mar 2025 23:45:30 +0100 Subject: [PATCH 3/3] remove javadoc host link --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 158b982..7720008 100755 --- a/README.md +++ b/README.md @@ -66,8 +66,6 @@ dependencies { # Examples -To get an overview about all classes and methods, read the [docs](https://bytedream.org/docs/untis4j/). - **Note:** For the `Session.login(...)` method a server and a school name is required. To gain these you have to go to [webuntis.com](https://webuntis.com/), type in your school and choose it. Then you will be redirected to the untis login page. The url of this page is, for example `https://example.webuntis.com/WebUntis/?school=myschool#/basic/main`. The server is the beginning of the url `https://example.webuntis.com` and the school name is the parameter after the `?school=`, in this case it is `myschool`