use of com.gh4a.ApiRequestException in project gh4a by slapperwan.
the class FileViewerActivity method loadFile.
private void loadFile(boolean force) {
RepositoryContentService service = ServiceFactory.get(RepositoryContentService.class, force);
service.getContents(mRepoOwner, mRepoName, mPath, mRef).map(ApiHelpers::throwOnFailure).map(Optional::of).onErrorResumeNext(error -> {
if (error instanceof ApiRequestException) {
ClientErrorResponse response = ((ApiRequestException) error).getResponse();
List<ClientErrorResponse.FieldError> errors = response != null ? response.errors() : null;
if (errors != null) {
for (ClientErrorResponse.FieldError fe : errors) {
if (fe.reason() == ClientErrorResponse.FieldError.Reason.TooLarge) {
openUnsuitableFileAndFinish();
return Single.just(Optional.absent());
}
}
}
}
return Single.error(error);
}).compose(makeLoaderSingle(ID_LOADER_FILE, force)).subscribe(result -> {
if (result.isPresent()) {
mContent = result.get();
onDataReady();
setContentEmpty(false);
} else {
setContentEmpty(true);
setContentShown(true);
}
}, this::handleLoadFailure);
}
use of com.gh4a.ApiRequestException 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);
}
});
}
use of com.gh4a.ApiRequestException in project gh4a by slapperwan.
the class RefPathDisambiguationTask method resolve.
// returns ref, path
private Single<Optional<Pair<String, String>>> resolve() throws ApiRequestException {
// first check whether the path redirects to HEAD
if (mRefAndPath.startsWith("HEAD")) {
return Single.just(Optional.of(Pair.create("HEAD", mRefAndPath.startsWith("HEAD/") ? mRefAndPath.substring(5) : null)));
}
final RepositoryBranchService branchService = ServiceFactory.get(RepositoryBranchService.class, false);
final RepositoryService repoService = ServiceFactory.get(RepositoryService.class, false);
// then look for matching branches
return ApiHelpers.PageIterator.toSingle(page -> branchService.getBranches(mRepoOwner, mRepoName, page)).compose(this::matchBranch).flatMap(result -> result.orOptionalSingle(() -> ApiHelpers.PageIterator.toSingle(page -> repoService.getTags(mRepoOwner, mRepoName, page)).compose(this::matchBranch))).map(resultOpt -> resultOpt.orOptional(() -> {
// at this point, the first item may still be a SHA1 - check with a simple regex
int slashPos = mRefAndPath.indexOf('/');
String potentialSha = slashPos > 0 ? mRefAndPath.substring(0, slashPos) : mRefAndPath;
if (SHA1_PATTERN.matcher(potentialSha).matches()) {
return Optional.of(Pair.create(potentialSha, slashPos > 0 ? mRefAndPath.substring(slashPos + 1) : ""));
}
return Optional.absent();
}));
}
use of com.gh4a.ApiRequestException in project gh4a by slapperwan.
the class CommitCompareFragment method onCreateDataSingle.
@Override
protected Single<List<Commit>> onCreateDataSingle(boolean bypassCache) {
RepositoryCommitService service = ServiceFactory.get(RepositoryCommitService.class, bypassCache);
Single<CommitCompare> compareSingle = service.compareCommits(mRepoOwner, mRepoName, mBase, mHead).map(ApiHelpers::throwOnFailure).onErrorResumeNext(error -> {
if (error instanceof ApiRequestException) {
ApiRequestException are = (ApiRequestException) error;
if (are.getStatus() == HttpURLConnection.HTTP_NOT_FOUND && mBaseLabel != null && mHeadLabel != null) {
// We got a 404; likely the history of the base branch was rewritten. Try the labels.
return service.compareCommits(mRepoOwner, mRepoName, mBaseLabel, mHeadLabel).map(ApiHelpers::throwOnFailure);
}
}
return Single.error(error);
});
return compareSingle.map(CommitCompare::commits).compose(RxUtils.mapFailureToValue(HttpURLConnection.HTTP_NOT_FOUND, new ArrayList<>()));
}
Aggregations