```java AmpacheApi.INSTANCE.initSession(this); ``` ```java if (AmpacheSession.INSTANCE.isUserAuthenticated()) { AmpacheApi.INSTANCE.initUser() .subscribe(aVoid -> { // user initialized successfully }, throwable -> { // there was a problem initializing the user, check the message in throwable }); } ``` ```java String url = "yourAmpacheInstanceURL"; String username = "yourAmpacheUsername"; String password = "yourAmpacheUserPassword";
AmpacheApi.INSTANCE.initUser(url,username,password) .subscribe(aVoid -> { // this is a valid user, it's possible to initiate the handshake }, throwable -> { // not a valid user });
<h4>after initializing a user, do the handshake to login:</h4>
```java
AmpacheApi.INSTANCE.handshake()
.subscribe(handshakeResponse -> {
// handshake successful, user logged in
}, throwable -> {
// error handshake, check throwable
});
AmpacheApi.INSTANCE.initUser(url,username,password) .flatMap(aVoid -> AmpacheApi.INSTANCE.handshake()) .subscribe(handshakeResponse -> { // handshake successful, user logged in }, throwable -> { // error handshake, check throwable }); });
<h4>request all the artists:</h4>
```java
AmpacheApi.INSTANCE.getArtists())
.subscribe(List<Artists> artists -> ....)
the ampache session will expire, the expiration time is stored inside the handshake response, to avoid the expiration of the session ping the server periodically: ```java AmpacheApi.INSTANCE.ping()) .subscribe(PingResponse pingResponse -> ....) ```
for every API request, in the request fails check if the throwable instance returned is instance of AmpacheApiException, in that case you can cast to it and get other info about the error ```java public void onError(Throwable throwable) { String message; if (throwable instanceof AmpacheApiException) { message = "Ampache error\ncode:" + ((AmpacheApiException) throwable).getAmpacheError().getCode() + "\nerror: " + ((AmpacheApiException) throwable).getAmpacheError().getError(); } else if (throwable.getLocalizedMessage()!=null) { message = throwable.getLocalizedMessage(); } else { message = "Undefined error"; } } ```