Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
49 changes: 26 additions & 23 deletions src/main/java/org/bytedream/untis4j/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}

/**
Expand Down
36 changes: 32 additions & 4 deletions src/main/java/org/bytedream/untis4j/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down