use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.
the class IssueFragmentBase method onEditorDoSend.
@Override
public Single<?> onEditorDoSend(String comment) {
IssueCommentService service = ServiceFactory.get(IssueCommentService.class, false);
CommentRequest request = CommentRequest.builder().body(comment).build();
return service.createIssueComment(mRepoOwner, mRepoName, mIssue.number(), request).map(ApiHelpers::throwOnFailure);
}
use of com.gh4a.utils.ApiHelpers 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.utils.ApiHelpers in project gh4a by slapperwan.
the class PullRequestDiffCommentLoadTask method getSingle.
@Override
protected Single<Optional<Intent>> getSingle() {
PullRequestService service = ServiceFactory.get(PullRequestService.class, false);
Single<PullRequest> pullRequestSingle = service.getPullRequest(mRepoOwner, mRepoName, mPullRequestNumber).map(ApiHelpers::throwOnFailure);
final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, false);
Single<List<ReviewComment>> commentsSingle = ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(mRepoOwner, mRepoName, mPullRequestNumber, page)).compose(RxUtils.filter(c -> c.position() != null)).cache();
Single<List<GitHubFile>> filesSingle = ApiHelpers.PageIterator.toSingle(page -> service.getPullRequestFiles(mRepoOwner, mRepoName, mPullRequestNumber, page));
return commentsSingle.compose(RxUtils.filterAndMapToFirst(c -> mMarker.matches(c.id(), c.createdAt()))).zipWith(filesSingle, (commentOpt, files) -> commentOpt.map(comment -> {
for (GitHubFile commitFile : files) {
if (commitFile.filename().equals(comment.path())) {
return Pair.create(true, commitFile);
}
}
return Pair.create(comment != null, (GitHubFile) null);
})).flatMap(result -> {
if (result.isPresent()) {
boolean foundComment = result.get().first;
GitHubFile file = result.get().second;
if (foundComment && file != null && !FileUtils.isImage(file.filename())) {
return Single.zip(pullRequestSingle, commentsSingle, (pr, comments) -> {
// noinspection CodeBlock2Expr
return Optional.of(PullRequestDiffViewerActivity.makeIntent(mActivity, mRepoOwner, mRepoName, mPullRequestNumber, pr.head().sha(), file.filename(), file.patch(), comments, -1, -1, -1, false, mMarker));
});
}
if (foundComment && file == null) {
return Single.just(Optional.of(PullRequestActivity.makeIntent(mActivity, mRepoOwner, mRepoName, mPullRequestNumber, mPage, mMarker)));
}
}
return Single.just(Optional.absent());
});
}
use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.
the class PullRequestReviewCommentLoadTask method load.
public static Single<Optional<Intent>> load(Context context, String repoOwner, String repoName, int pullRequestNumber, IntentUtils.InitialCommentMarker marker) {
final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, false);
final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, false);
return ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(repoOwner, repoName, pullRequestNumber, page)).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).flatMap(comments -> {
Map<String, ReviewComment> commentsByDiffHunkId = new HashMap<>();
for (ReviewComment comment : comments) {
String id = TimelineItem.Diff.getDiffHunkId(comment);
if (!commentsByDiffHunkId.containsKey(id)) {
// Because the comment we are looking for could be a reply to another
// review we have to keep track of initial comments for each diff hunk
commentsByDiffHunkId.put(id, comment);
}
if (marker.matches(comment.id(), null)) {
// Once found the comment we are looking for get a correct review id from
// the initial diff hunk comment
ReviewComment initialComment = commentsByDiffHunkId.get(id);
long reviewId = initialComment.pullRequestReviewId();
return reviewService.getReview(repoOwner, repoName, pullRequestNumber, reviewId).map(ApiHelpers::throwOnFailure).map(Optional::of);
}
}
return Single.just(Optional.<Review>absent());
}).map(reviewOpt -> reviewOpt.map(review -> ReviewActivity.makeIntent(context, repoOwner, repoName, pullRequestNumber, review, marker)));
}
use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.
the class CommitNoteFragment method onEditorDoSend.
@Override
public Single<?> onEditorDoSend(String comment) {
RepositoryCommentService service = ServiceFactory.get(RepositoryCommentService.class, false);
CreateCommitComment request = CreateCommitComment.builder().body(comment).build();
return service.createCommitComment(mRepoOwner, mRepoName, mObjectSha, request).map(ApiHelpers::throwOnFailure);
}
Aggregations