Skip to content

Commit ce0df30

Browse files
authored
v1.3.6 (#27)
* fix request manager and refactor * fix common error * remove javadoc host link
1 parent 9e88c43 commit ce0df30

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ dependencies {
6666

6767
# Examples
6868

69-
To get an overview about all classes and methods, read the [docs](https://bytedream.org/docs/untis4j/).
70-
7169
**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.
7270
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`.
7371
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`

src/main/java/org/bytedream/untis4j/RequestManager.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
import org.json.JSONException;
66
import org.json.JSONObject;
77

8-
import java.io.BufferedReader;
9-
import java.io.DataOutputStream;
10-
import java.io.IOException;
11-
import java.io.InputStreamReader;
8+
import java.io.*;
129
import java.net.ConnectException;
1310
import java.net.HttpURLConnection;
1411
import java.net.URL;
12+
import java.nio.charset.StandardCharsets;
1513
import java.time.Duration;
1614
import java.util.HashMap;
1715
import java.util.Map;
@@ -61,6 +59,29 @@ public static Infos generateUserInfosAndLogin(String username, String password,
6159
put("password", password);
6260
put("client", userAgent);
6361
}});
62+
StringBuilder stringBuilder = getStringBuilder(userAgent, url, requestBody);
63+
64+
JSONObject jsonObject;
65+
66+
try {
67+
jsonObject = new JSONObject(stringBuilder.toString());
68+
69+
if (jsonObject.has("error")) {
70+
JSONObject errorObject = jsonObject.getJSONObject("error");
71+
throw new LoginException("The response contains an error (" + errorObject.getInt("errorObject") + "): " + errorObject.getString("message"));
72+
}
73+
} catch (JSONException e) {
74+
throw new IOException("An unexpected exception occurred: " + stringBuilder);
75+
}
76+
77+
JSONObject result = jsonObject.getJSONObject("result");
78+
79+
UntisUtils.ElementType elementType = UntisUtils.ElementType.of(result.getInt("personType"));
80+
81+
return new Infos(username, password, server, schoolName, userAgent, result.getString("sessionId"), result.getInt("personId"), elementType, result.getInt("klasseId"));
82+
}
83+
84+
private static StringBuilder getStringBuilder(String userAgent, URL url, String requestBody) throws IOException {
6485
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
6586

6687
connection.setRequestMethod("POST");
@@ -86,25 +107,7 @@ public static Infos generateUserInfosAndLogin(String username, String password,
86107
while ((line = input.readLine()) != null) {
87108
stringBuilder.append(line);
88109
}
89-
90-
JSONObject jsonObject;
91-
92-
try {
93-
jsonObject = new JSONObject(stringBuilder.toString());
94-
95-
if (jsonObject.has("error")) {
96-
JSONObject errorObject = jsonObject.getJSONObject("error");
97-
throw new LoginException("The response contains an error (" + errorObject.getInt("errorObject") + "): " + errorObject.getString("message"));
98-
}
99-
} catch (JSONException e) {
100-
throw new IOException("An unexpected exception occurred: " + stringBuilder);
101-
}
102-
103-
JSONObject result = jsonObject.getJSONObject("result");
104-
105-
UntisUtils.ElementType elementType = UntisUtils.ElementType.of(result.getInt("personType"));
106-
107-
return new Infos(username, password, server, schoolName, userAgent, result.getString("sessionId"), result.getInt("personId"), elementType, result.getInt("klasseId"));
110+
return stringBuilder;
108111
}
109112

110113
/**

src/main/java/org/bytedream/untis4j/Session.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,38 @@ public Timetable getTimetable(LocalDate start, LocalDate end, UntisUtils.Element
743743

744744
Timetable timetable = new Timetable();
745745

746-
Classes c = getClasses();
747-
Teachers t = getTeachers();
748-
Subjects s = getSubjects();
749-
Rooms r = getRooms();
746+
Classes c;
747+
Teachers t;
748+
Subjects s;
749+
Rooms r;
750+
751+
try {
752+
c = getClasses();
753+
} catch (IOException e) {
754+
e.printStackTrace();
755+
c = null;
756+
}
757+
try {
758+
t = getTeachers();
759+
} catch (IOException e) {
760+
e.printStackTrace();
761+
t = null;
762+
}
763+
try {
764+
s = getSubjects();
765+
} catch (IOException e) {
766+
e.printStackTrace();
767+
s = null;
768+
}
769+
try {
770+
r = getRooms();
771+
} catch (IOException e) {
772+
e.printStackTrace();
773+
r = null;
774+
}
775+
776+
777+
750778

751779
TimeUnits timeUnits = getTimegridUnits().get(0).getTimeUnits();
752780

0 commit comments

Comments
 (0)