Search in sources :

Example 1 with AccessToken

use of com.jakdor.labday.common.model.AccessToken in project LabDayApp by jakdor.

the class ProjectRepository method getUpdate.

/**
 * Checks if update is necessary, then gets AppData from API or local db
 * @return {Single<RxResponse<AppData>>} appData wrapped with {@link RxResponse}
 */
public Observable<RxResponse<AppData>> getUpdate(String apiUrl, Context context) {
    if (accessToken == null) {
        if (!loadAccessToken(context)) {
            repositoryState = repositoryStates.ERROR;
            return Observable.just(RxResponse.loginError(new Throwable("no access token")));
        }
    } else if (accessToken.equals("-1")) {
        repositoryState = repositoryStates.ERROR;
        return Observable.just(RxResponse.loginError(new Throwable("bad access token")));
    }
    networkManager.configAuth(apiUrl, accessToken);
    networkManager.configAuth(apiUrl);
    return networkManager.getLastUpdate().subscribeOn(rxSchedulersFacade.io()).observeOn(rxSchedulersFacade.ui()).onErrorResumeNext(Observable.just(new LastUpdate("-1"))).onExceptionResumeNext(Observable.just(new LastUpdate("-1"))).flatMap(s -> isLocalDataCurrent(apiUpdateId = s.getUpdatedAt(), context) ? // load from local db
    localDbHandler.getAppDataFromDb() : // get appData from api
    apiRequest(networkManager.getAppData())).onErrorResumeNext(// last effort data retrieval
    localDbHandler.getAppDataFromDb()).flatMap(appDataRxResponse -> {
        // last effort data retrieval from local db
        if (appDataRxResponse.status == RxStatus.ERROR) {
            return localDbHandler.getAppDataFromDb();
        } else {
            return Observable.just(appDataRxResponse);
        }
    }).doOnNext(appDataRxResponse -> {
        if (appDataRxResponse.status == RxStatus.SUCCESS) {
            saveApiLastUpdateId(apiUpdateId, context);
        } else if (appDataRxResponse.status == RxStatus.SUCCESS_DB) {
            this.data = appDataRxResponse;
            this.repositoryState = repositoryStates.READY;
        }
    }).onErrorReturn(throwable -> {
        if (apiUpdateCurrent) {
            this.repositoryState = repositoryStates.NO_DB;
            return RxResponse.noDb(throwable);
        } else {
            this.repositoryState = repositoryStates.ERROR;
            return RxResponse.error(throwable);
        }
    });
}
Also used : Context(android.content.Context) Entity(com.facebook.crypto.Entity) R(com.jakdor.labday.R) MapPath(com.jakdor.labday.common.model.maps.MapPath) CryptoConfig(com.facebook.crypto.CryptoConfig) Singleton(javax.inject.Singleton) KeyChain(com.facebook.crypto.keychain.KeyChain) Inject(javax.inject.Inject) Crypto(com.facebook.crypto.Crypto) LocalDbHandler(com.jakdor.labday.common.localdb.LocalDbHandler) Charset(java.nio.charset.Charset) AccessToken(com.jakdor.labday.common.model.AccessToken) RxSchedulersFacade(com.jakdor.labday.rx.RxSchedulersFacade) MapService(com.jakdor.labday.common.network.MapService) Observable(io.reactivex.Observable) RxStatus(com.jakdor.labday.rx.RxStatus) AndroidConceal(com.facebook.android.crypto.keychain.AndroidConceal) AppData(com.jakdor.labday.common.model.AppData) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) LastUpdate(com.jakdor.labday.common.model.LastUpdate) FileInputStream(java.io.FileInputStream) File(java.io.File) Timber(timber.log.Timber) SharedPreferences(android.content.SharedPreferences) LastUpdate(com.jakdor.labday.common.model.LastUpdate)

Example 2 with AccessToken

use of com.jakdor.labday.common.model.AccessToken in project LabDayApp by jakdor.

the class NetworkManagerIntegrationTest method loginTest.

/**
 * {@link NetworkManager} integration test: login request to, local REST API server mock
 */
@Test
public void loginTest() throws Exception {
    networkManager.configAuth(dummyApiUrl);
    Gson gson = new Gson();
    AccessToken expectedAccessToken = gson.fromJson(readAssetFile(testContext, "api/login.json"), AccessToken.class);
    TestObserver<AccessToken> testObserver = networkManager.getAccessToken(dummyLogin, dummyPassword).test();
    testObserver.awaitCount(1);
    testObserver.assertSubscribed();
    testObserver.assertNoErrors();
    testObserver.assertValue(accessToken -> {
        Assert.assertEquals(expectedAccessToken, accessToken);
        Assert.assertEquals(expectedAccessToken.hashCode(), accessToken.hashCode());
        return true;
    });
    testObserver.onComplete();
}
Also used : AccessToken(com.jakdor.labday.common.model.AccessToken) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 3 with AccessToken

use of com.jakdor.labday.common.model.AccessToken in project LabDayApp by jakdor.

the class RetrofitAPICallsIntegrationTest method loginTest.

/**
 * Tests login API call
 */
@Test
public void loginTest() throws Exception {
    LabService labService = retrofitBuilder.createService(dummyApiUrl, LabService.class);
    TestObserver<AccessToken> testObserver = labService.getAccessToken(dummyLogin, dummyPassword).test();
    testObserver.assertSubscribed();
    testObserver.awaitCount(1);
    testObserver.assertNoErrors();
    testObserver.assertValue(accessToken1 -> {
        assertNotNull(accessToken1);
        assertNotNull(accessToken1.getAccessToken());
        return true;
    });
    testObserver.onComplete();
}
Also used : LabService(com.jakdor.labday.common.network.LabService) AccessToken(com.jakdor.labday.common.model.AccessToken) Test(org.junit.Test)

Aggregations

AccessToken (com.jakdor.labday.common.model.AccessToken)3 Test (org.junit.Test)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 AndroidConceal (com.facebook.android.crypto.keychain.AndroidConceal)1 SharedPrefsBackedKeyChain (com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)1 Crypto (com.facebook.crypto.Crypto)1 CryptoConfig (com.facebook.crypto.CryptoConfig)1 Entity (com.facebook.crypto.Entity)1 KeyChain (com.facebook.crypto.keychain.KeyChain)1 Gson (com.google.gson.Gson)1 R (com.jakdor.labday.R)1 LocalDbHandler (com.jakdor.labday.common.localdb.LocalDbHandler)1 AppData (com.jakdor.labday.common.model.AppData)1 LastUpdate (com.jakdor.labday.common.model.LastUpdate)1 MapPath (com.jakdor.labday.common.model.maps.MapPath)1 LabService (com.jakdor.labday.common.network.LabService)1 MapService (com.jakdor.labday.common.network.MapService)1 RxResponse (com.jakdor.labday.rx.RxResponse)1 RxSchedulersFacade (com.jakdor.labday.rx.RxSchedulersFacade)1