Search in sources :

Example 1 with ServerErrorType

use of me.proxer.library.api.ProxerException.ServerErrorType in project ProxerLibJava by proxer.

the class LoginTokenInterceptor method handleResponse.

private void handleResponse(final Response response) throws IOException {
    final String responseBody = peekResponseBody(response);
    final Matcher errorMatcher = ERROR_PATTERN.matcher(responseBody);
    final HttpUrl url = response.request().url();
    if (errorMatcher.find()) {
        final int errorCode = Integer.parseInt(errorMatcher.group(1));
        final ServerErrorType errorType = ServerErrorType.fromErrorCodeOrNull(errorCode);
        if (errorType != null && isLoginError(errorType)) {
            loginTokenManager.persist(null);
        }
    } else if (url.pathSegments().equals(LOGIN_PATH)) {
        final Matcher loginTokenMatcher = LOGIN_TOKEN_PATTERN.matcher(responseBody);
        if (loginTokenMatcher.find()) {
            loginTokenManager.persist(loginTokenMatcher.group(1).trim());
        } else {
            throw new JsonDataException("No token found after successful login.");
        }
    } else if (url.pathSegments().equals(LOGOUT_PATH)) {
        loginTokenManager.persist(null);
    }
}
Also used : Matcher(java.util.regex.Matcher) JsonDataException(com.squareup.moshi.JsonDataException) ServerErrorType(me.proxer.library.api.ProxerException.ServerErrorType) HttpUrl(okhttp3.HttpUrl)

Aggregations

JsonDataException (com.squareup.moshi.JsonDataException)1 Matcher (java.util.regex.Matcher)1 ServerErrorType (me.proxer.library.api.ProxerException.ServerErrorType)1 HttpUrl (okhttp3.HttpUrl)1