Search in sources :

Example 1 with ApiRequestException

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);
}
Also used : ApiRequestException(com.gh4a.ApiRequestException) ClientErrorResponse(com.meisolsson.githubsdk.model.ClientErrorResponse) RepositoryContentService(com.meisolsson.githubsdk.service.repositories.RepositoryContentService) ApiHelpers(com.gh4a.utils.ApiHelpers)

Example 2 with ApiRequestException

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);
        }
    });
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) MessageDigest(java.security.MessageDigest) Dialog(android.app.Dialog) NonNull(android.support.annotation.NonNull) DialogFragment(android.support.v4.app.DialogFragment) Single(io.reactivex.Single) Editable(android.text.Editable) TextInputLayout(android.support.design.widget.TextInputLayout) User(com.meisolsson.githubsdk.model.User) Locale(java.util.Locale) Handler(android.os.Handler) R(com.gh4a.R) View(android.view.View) Button(android.widget.Button) Settings(android.provider.Settings) ApiRequestException(com.gh4a.ApiRequestException) Build(android.os.Build) DialogInterface(android.content.DialogInterface) ApiHelpers(com.gh4a.utils.ApiHelpers) Iterator(java.util.Iterator) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) UserService(com.meisolsson.githubsdk.service.users.UserService) IdRes(android.support.annotation.IdRes) AlertDialog(android.support.v7.app.AlertDialog) RxUtils(com.gh4a.utils.RxUtils) Pair(android.support.v4.util.Pair) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextInputEditText(android.support.design.widget.TextInputEditText) ServiceFactory(com.gh4a.ServiceFactory) PublishProcessor(io.reactivex.processors.PublishProcessor) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LoginService(com.gh4a.ServiceFactory.LoginService) TextWatcher(android.text.TextWatcher) RxUtils(com.gh4a.utils.RxUtils) LoginService(com.gh4a.ServiceFactory.LoginService) ApiRequestException(com.gh4a.ApiRequestException) ApiHelpers(com.gh4a.utils.ApiHelpers)

Example 3 with ApiRequestException

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();
    }));
}
Also used : RepositoryBranchService(com.meisolsson.githubsdk.service.repositories.RepositoryBranchService) FileViewerActivity(com.gh4a.activities.FileViewerActivity) ApiHelpers(com.gh4a.utils.ApiHelpers) Pair(android.util.Pair) RepositoryService(com.meisolsson.githubsdk.service.repositories.RepositoryService) Intent(android.content.Intent) TextUtils(android.text.TextUtils) Single(io.reactivex.Single) VisibleForTesting(android.support.annotation.VisibleForTesting) List(java.util.List) RepositoryActivity(com.gh4a.activities.RepositoryActivity) FragmentActivity(android.support.v4.app.FragmentActivity) Optional(com.gh4a.utils.Optional) Branch(com.meisolsson.githubsdk.model.Branch) ApiRequestException(com.gh4a.ApiRequestException) Pattern(java.util.regex.Pattern) ServiceFactory(com.gh4a.ServiceFactory) RepositoryBranchService(com.meisolsson.githubsdk.service.repositories.RepositoryBranchService) RepositoryService(com.meisolsson.githubsdk.service.repositories.RepositoryService)

Example 4 with ApiRequestException

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<>()));
}
Also used : ApiRequestException(com.gh4a.ApiRequestException) ArrayList(java.util.ArrayList) ApiHelpers(com.gh4a.utils.ApiHelpers) CommitCompare(com.meisolsson.githubsdk.model.CommitCompare) RepositoryCommitService(com.meisolsson.githubsdk.service.repositories.RepositoryCommitService)

Aggregations

ApiRequestException (com.gh4a.ApiRequestException)4 ApiHelpers (com.gh4a.utils.ApiHelpers)4 TextUtils (android.text.TextUtils)2 ServiceFactory (com.gh4a.ServiceFactory)2 Single (io.reactivex.Single)2 Dialog (android.app.Dialog)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Settings (android.provider.Settings)1 IdRes (android.support.annotation.IdRes)1 NonNull (android.support.annotation.NonNull)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 TextInputEditText (android.support.design.widget.TextInputEditText)1 TextInputLayout (android.support.design.widget.TextInputLayout)1 DialogFragment (android.support.v4.app.DialogFragment)1 FragmentActivity (android.support.v4.app.FragmentActivity)1