From dade689e22eabc8513bd86b2e26127f8c6ca3091 Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Sat, 18 Jan 2020 17:29:14 +0100 Subject: [PATCH] Auto get URL --- src/main/java/ley/untis/UntisClient.java | 25 ++++++++++++++----- .../java/ley/untis/data/SchoolParams.java | 10 ++++++++ .../java/ley/untis/data/SchoolResponse.java | 22 ++++++++++++++++ 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 src/main/java/ley/untis/data/SchoolParams.java create mode 100644 src/main/java/ley/untis/data/SchoolResponse.java diff --git a/src/main/java/ley/untis/UntisClient.java b/src/main/java/ley/untis/UntisClient.java index 027c92c..24f37ab 100644 --- a/src/main/java/ley/untis/UntisClient.java +++ b/src/main/java/ley/untis/UntisClient.java @@ -14,18 +14,32 @@ public class UntisClient { public AuthResponse auth; Map header; + public static String getURLBySchoolname(String schoolname) throws APIRequestException { + try { + JsonRpcHttpClient client = new JsonRpcHttpClient(new URL("https://mobile.webuntis.com/ms/schoolquery2")); + SchoolParams[] params = new SchoolParams[1]; + params[0] = new SchoolParams(schoolname); + SchoolResponse res = client.invoke("searchSchool", params, SchoolResponse.class); + + String url = "https://" + res.schools[0].server + "/WebUntis/jsonrpc.do"; + + return url; + } catch (Throwable e) { + throw new APIRequestException(e); + } + } + /** * Creates an UntisClient and starts a session. * @param username The username of the user * @param password The password of the user - * @param url The URL of the server including jsonrpc.do * @param school The name of the school * @param appname The name of your app * @throws APIRequestException */ - public UntisClient(String username, String password, String url, String school, String appname) throws APIRequestException{ + public UntisClient(String username, String password, String school, String appname) throws APIRequestException{ try { - client = new JsonRpcHttpClient(new URL(url + "?school=" + school)); + client = new JsonRpcHttpClient(new URL(getURLBySchoolname(school) + "?school=" + school)); auth = client.invoke("authenticate", new AuthParams(username, password, appname), AuthResponse.class); header = new HashMap<>(); @@ -41,12 +55,11 @@ public class UntisClient { * Creates an UntisClient and starts a session. * @param username The username of the user * @param password The password of the user - * @param url The URL of the server including jsonrpc.do * @param school The name of the school * @throws APIRequestException */ - public UntisClient(String username, String password, String url, String school) throws APIRequestException{ - this(username, password, url, school, "JavaUntis"); + public UntisClient(String username, String password, String school) throws APIRequestException{ + this(username, password, school, "JavaUntis"); } /** diff --git a/src/main/java/ley/untis/data/SchoolParams.java b/src/main/java/ley/untis/data/SchoolParams.java new file mode 100644 index 0000000..505de69 --- /dev/null +++ b/src/main/java/ley/untis/data/SchoolParams.java @@ -0,0 +1,10 @@ +package ley.untis.data; + +public class SchoolParams { + + public String search; + + public SchoolParams(String search) { + this.search = search; + } +} diff --git a/src/main/java/ley/untis/data/SchoolResponse.java b/src/main/java/ley/untis/data/SchoolResponse.java new file mode 100644 index 0000000..37f4d6c --- /dev/null +++ b/src/main/java/ley/untis/data/SchoolResponse.java @@ -0,0 +1,22 @@ +package ley.untis.data; + +import java.util.ArrayList; + +public class SchoolResponse { + + public int size; + public School[] schools; + + public static class School { + public String server; + public boolean useMobileServiceUrlAndroid; + public String address; + public String loginName; + public int schoolId; + public boolean useMobileServiceUrlIos; + public String mobileServiceUrl; + public String displayName; + public String serverUrl; + } + +}