All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.sonallux.spotify.api.http.AuthorizedApiCall Maven / Gradle / Ivy

The newest version!
package de.sonallux.spotify.api.http;

import com.fasterxml.jackson.core.type.TypeReference;
import de.sonallux.spotify.api.authorization.ApiAuthorizationProvider;
import lombok.AllArgsConstructor;

import java.io.IOException;

public class AuthorizedApiCall extends ApiCall {
    private final ApiAuthorizationProvider authProvider;

    public AuthorizedApiCall(ApiAuthorizationProvider authProvider, ApiClient apiClient, Request request, TypeReference responseType) {
        super(apiClient, request, responseType);
        this.authProvider = authProvider;
    }

    @Override
    protected Response parseResponse(okhttp3.Response rawResponse) throws IOException {
        if (rawResponse.code() == 401) { //Unauthorized
            if (authProvider.refreshAccessToken()) {
                rawResponse = getRawCall().clone().execute();
            }
        }

        return super.parseResponse(rawResponse);
    }

    @AllArgsConstructor
    public static class Factory extends ApiCall.Factory {
        private final ApiAuthorizationProvider authProvider;

        @Override
        public  ApiCall createApiCall(ApiClient apiClient, Request request, TypeReference responseType) {
            return new AuthorizedApiCall<>(authProvider, apiClient, request, responseType);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy