use of com.gh4a.ServiceFactory.LoginService in project gh4a by slapperwan.
the class UserPasswordLoginDialogFragment method makeRequestSingle.
private Single<LoginService.AuthorizationRequest> makeRequestSingle() {
String description = "Octodroid - " + Build.MANUFACTURER + " " + Build.MODEL;
String fingerprint = getHashedDeviceId();
LoginService service = getService();
String scopes = getArguments().getString("scopes");
return service.getAuthorizations().map(ApiHelpers::throwOnFailure).compose(RxUtils::doInBackground).retryWhen(handler -> handler.flatMap(error -> {
if (error instanceof ApiRequestException) {
ApiRequestException are = (ApiRequestException) error;
if (are.getStatus() == 401 && are.getResponse().message().contains("OTP code")) {
mWaitingForOtpCode = true;
mHandler.post(() -> updateContainerVisibility(false));
// getAuthorizations() doesn't trigger the OTP SMS for whatever reason,
// so make a dummy create request (which we know will fail) just to
// actually trigger SMS sending
LoginService.AuthorizationRequest dummyRequest = new LoginService.AuthorizationRequest("", "dummy", "");
service.createAuthorization(dummyRequest).compose(RxUtils::doInBackground).subscribe(ignoredResponse -> {
}, ignoredError -> {
});
}
}
if (!mWaitingForOtpCode) {
mRetryProcessor.onError(error);
}
return mRetryProcessor;
})).compose(RxUtils.filter(authorization -> {
String note = authorization.note();
return note != null && note.startsWith(description);
})).flatMap(existingAuthorizations -> {
Single<Void> deleteSingle = null;
Iterator<LoginService.AuthorizationResponse> iter = existingAuthorizations.iterator();
while (iter.hasNext()) {
LoginService.AuthorizationResponse auth = iter.next();
if (fingerprint.equals(auth.fingerprint())) {
deleteSingle = service.deleteAuthorization(auth.id()).map(ApiHelpers::throwOnFailure).compose(RxUtils::doInBackground);
iter.remove();
}
}
String finalDescription = description;
if (!existingAuthorizations.isEmpty()) {
finalDescription += " #" + (existingAuthorizations.size() + 1);
}
LoginService.AuthorizationRequest request = new LoginService.AuthorizationRequest(scopes, finalDescription, fingerprint);
if (deleteSingle != null) {
return deleteSingle.map(response -> request);
} else {
return Single.just(request);
}
});
}
Aggregations