Basic Client with login and logout

This commit is contained in:
Timo Ley 2020-01-18 12:52:47 +01:00
parent 197d1525fe
commit 6390c2ae97
4 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,45 @@
package ley.untis;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import ley.untis.data.AuthParams;
import ley.untis.data.AuthResponse;
import ley.untis.exception.APIRequestException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class UntisClient {
JsonRpcHttpClient client;
public AuthResponse auth;
Map<String , String> header;
public UntisClient(String username, String password, String url, String school, String appname) throws APIRequestException{
try {
client = new JsonRpcHttpClient(new URL(url + "?school=" + school));
auth = client.invoke("authenticate", new AuthParams(username, password, appname), AuthResponse.class);
header = new HashMap<>();
header.put("cookie", "JSESSIONID=" + auth.sessionId);
client.setHeaders(header);
} catch (Throwable e) {
throw new APIRequestException(e);
}
}
public UntisClient(String username, String password, String url, String school) throws APIRequestException{
this(username, password, url, school, "JavaUntis");
}
public void logout() throws APIRequestException {
try {
client.invoke("logout", new Object[0], Object.class);
} catch (Throwable e) {
throw new APIRequestException(e);
}
}
}

View file

@ -0,0 +1,15 @@
package ley.untis.data;
public class AuthParams {
public String user;
public String password;
public String client;
public AuthParams(String user, String password, String client) {
this.user = user;
this.password = password;
this.client = client;
}
}

View file

@ -0,0 +1,10 @@
package ley.untis.data;
public class AuthResponse {
public String sessionId;
public int personType;
public int personId;
public int klasseId;
}

View file

@ -0,0 +1,20 @@
package ley.untis.exception;
public class APIRequestException extends Exception {
Throwable source;
public APIRequestException(Throwable error) {
source = error;
}
@Override
public String getMessage() {
return source.getMessage();
}
@Override
public String getLocalizedMessage() {
return source.getLocalizedMessage();
}
}