Auto get URL

This commit is contained in:
Timo Ley 2020-01-18 17:29:14 +01:00
parent 6e06cc4c08
commit dade689e22
3 changed files with 51 additions and 6 deletions

View file

@ -14,18 +14,32 @@ public class UntisClient {
public AuthResponse auth;
Map<String , String> 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");
}
/**

View file

@ -0,0 +1,10 @@
package ley.untis.data;
public class SchoolParams {
public String search;
public SchoolParams(String search) {
this.search = search;
}
}

View file

@ -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;
}
}