Search in sources :

Example 16 with StepikClientException

use of org.stepik.api.exceptions.StepikClientException in project intellij-plugins by StepicOrg.

the class DownloadSubmission method getSubmissions.

@Nullable
private List<Submission> getSubmissions(@NotNull StepikApiClient stepikApiClient, @NotNull StepNode stepNode) {
    try {
        long stepId = stepNode.getId();
        long userId = getCurrentUser().getId();
        Submissions submissions = stepikApiClient.submissions().get().step(stepId).user(userId).order(Order.DESC).execute();
        return submissions.getSubmissions();
    } catch (StepikClientException e) {
        logger.warn("Failed get submissions", e);
        return null;
    }
}
Also used : Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with StepikClientException

use of org.stepik.api.exceptions.StepikClientException in project intellij-plugins by StepicOrg.

the class ProgrammingLanguageUtils method getOrCreateMainFile.

@Nullable
private static PsiFile getOrCreateMainFile(@NotNull Project project, @NotNull VirtualFile parent, @NotNull SupportedLanguages language, @NotNull StepNode stepNode) {
    String fileName = language.getMainFileName();
    final VirtualFile[] file = { parent.findChild(fileName) };
    Application application = ApplicationManager.getApplication();
    if (file[0] == null) {
        application.invokeAndWait(() -> application.runWriteAction(() -> {
            try {
                file[0] = parent.createChildData(null, fileName);
                String template = null;
                StepikApiClient stepikApiClient = authAndGetStepikApiClient();
                User user = getCurrentUser();
                if (!user.isGuest()) {
                    try {
                        Submissions submissions = stepikApiClient.submissions().get().user(user.getId()).order(Order.DESC).step(stepNode.getId()).execute();
                        if (!submissions.isEmpty()) {
                            Optional<Submission> lastSubmission = submissions.getItems().stream().filter(submission -> SupportedLanguages.langOfName(submission.getReply().getLanguage()) == language).limit(1).findFirst();
                            if (lastSubmission.isPresent()) {
                                template = lastSubmission.get().getReply().getCode();
                            }
                        }
                    } catch (StepikClientException e) {
                        logger.warn(e);
                    }
                }
                if (template == null) {
                    template = stepNode.getTemplate(language);
                }
                file[0].setBinaryContent(template.getBytes());
            } catch (IOException e) {
                file[0] = null;
            }
        }));
    }
    return application.runReadAction((Computable<PsiFile>) () -> PsiManager.getInstance(project).findFile(file[0]));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) Application(com.intellij.openapi.application.Application) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ProjectFilesUtils.getOrCreateSrcPsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcPsiDirectory) Computable(com.intellij.openapi.util.Computable) StepikClientException(org.stepik.api.exceptions.StepikClientException) User(org.stepik.api.objects.users.User) PsiManager(com.intellij.psi.PsiManager) ArrayList(java.util.ArrayList) ProjectView(com.intellij.ide.projectView.ProjectView) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ProjectFilesUtils.getOrCreatePsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreatePsiDirectory) Metrics(org.stepik.core.metrics.Metrics) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) SUCCESSFUL(org.stepik.core.metrics.MetricsStatus.SUCCESSFUL) Submission(org.stepik.api.objects.submissions.Submission) SupportedLanguages(org.stepik.core.SupportedLanguages) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) StepNode(org.stepik.core.courseFormat.StepNode) IOException(java.io.IOException) StudyUtils(org.stepik.core.StudyUtils) MoveFilesOrDirectoriesUtil(com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesUtil) Nullable(org.jetbrains.annotations.Nullable) EduNames(org.stepik.core.core.EduNames) Submissions(org.stepik.api.objects.submissions.Submissions) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Optional(java.util.Optional) PsiDirectory(com.intellij.psi.PsiDirectory) StepikApiClient(org.stepik.api.client.StepikApiClient) NotNull(org.jetbrains.annotations.NotNull) Order(org.stepik.api.queries.Order) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) User(org.stepik.api.objects.users.User) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Optional(java.util.Optional) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) Application(com.intellij.openapi.application.Application) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with StepikClientException

use of org.stepik.api.exceptions.StepikClientException in project intellij-plugins by StepicOrg.

the class StudyToolWindow method postView.

private void postView(@NotNull StepNode stepNode, boolean isTheory) {
    executor.execute(() -> {
        Long assignment = stepNode.getAssignment();
        long stepId = stepNode.getId();
        if (assignment != null && assignment != 0) {
            StepikApiClient stepikApiClient = authAndGetStepikApiClient();
            if (!isAuthenticated()) {
                return;
            }
            try {
                stepikApiClient.views().post().assignment(assignment).step(stepId).execute();
            } catch (StepikClientException e) {
                logger.warn("Failed post view: stepId=" + stepId + "; assignment=" + assignment, e);
            }
        }
        if (isTheory) {
            stepNode.passed();
        }
        if (stepNode.getProject() == null) {
            stepNode.setProject(project);
        }
        if (!project.isDisposed()) {
            ApplicationManager.getApplication().invokeLater(() -> ProjectView.getInstance(project).refresh());
        }
    });
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Example 19 with StepikClientException

use of org.stepik.api.exceptions.StepikClientException in project intellij-plugins by StepicOrg.

the class SendAction method checkStepStatus.

public static void checkStepStatus(@NotNull Project project, @NotNull StepikApiClient stepikApiClient, @NotNull StepNode stepNode, final long submissionId, @NotNull ProgressIndicator indicator) {
    String stepIdString = "id=" + stepNode.getId();
    logger.info("Started check a status for step: " + stepIdString);
    String stepStatus = EVALUATION;
    int timer = 0;
    String hint;
    indicator.setIndeterminate(false);
    Submission currentSubmission = null;
    while (timer < FIVE_MINUTES) {
        try {
            Submissions submission = stepikApiClient.submissions().get().id(submissionId).execute();
            if (!submission.isEmpty()) {
                currentSubmission = submission.getFirst();
                ActionUtils.setupCheckProgress(indicator, currentSubmission, timer);
                stepStatus = currentSubmission.getStatus();
                if (!EVALUATION.equals(stepStatus)) {
                    break;
                }
            }
            Thread.sleep(PERIOD);
            if (Utils.isCanceled()) {
                Metrics.getStepStatusAction(project, stepNode, USER_CANCELED);
                return;
            }
            timer += PERIOD;
        } catch (StepikClientException | InterruptedException e) {
            ActionUtils.notifyError(project, "Error", "Get Status error");
            logger.info("Stop check a status for step: " + stepIdString, e);
            return;
        }
    }
    if (currentSubmission == null) {
        logger.info(String.format("Stop check a status for step: %s without result", stepIdString));
        return;
    }
    MetricsStatus actionStatus = EVALUATION.equals(stepStatus) ? TIME_OVER : SUCCESSFUL;
    Metrics.getStepStatusAction(project, stepNode, actionStatus);
    indicator.setIndeterminate(true);
    indicator.setText("");
    hint = currentSubmission.getHint();
    if (stepNode.getType() == StepType.CODE) {
        notify(project, stepNode, stepStatus, hint);
    }
    ApplicationManager.getApplication().invokeLater(() -> {
        if (!project.isDisposed()) {
            ProjectView.getInstance(project).refresh();
        }
        StepikProjectManager.updateSelection(project);
    });
    logger.info(String.format("Finish check a status for step: %s with status: %s", stepIdString, stepStatus));
}
Also used : Submission(org.stepik.api.objects.submissions.Submission) MetricsStatus(org.stepik.core.metrics.MetricsStatus) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Example 20 with StepikClientException

use of org.stepik.api.exceptions.StepikClientException in project intellij-plugins by StepicOrg.

the class LessonNode method getCourseId.

@Override
public long getCourseId(@NotNull StepikApiClient stepikApiClient) {
    StudyNode parent = getParent();
    if (parent != null) {
        return parent.getCourseId(stepikApiClient);
    }
    if (courseId != 0) {
        return courseId;
    }
    CompoundUnitLesson data = getData();
    int sectionId = data != null ? data.getUnit().getSection() : 0;
    if (sectionId == 0) {
        return 0;
    }
    try {
        Sections sections = stepikApiClient.sections().get().id(sectionId).execute();
        if (sections.isEmpty()) {
            return 0;
        }
        courseId = sections.getFirst().getCourse();
        return courseId;
    } catch (StepikClientException ignored) {
    }
    return 0;
}
Also used : CompoundUnitLesson(org.stepik.api.objects.lessons.CompoundUnitLesson) Sections(org.stepik.api.objects.sections.Sections) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Aggregations

StepikClientException (org.stepik.api.exceptions.StepikClientException)28 StepikApiClient (org.stepik.api.client.StepikApiClient)12 NotNull (org.jetbrains.annotations.NotNull)10 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)9 CompoundUnitLesson (org.stepik.api.objects.lessons.CompoundUnitLesson)6 Submissions (org.stepik.api.objects.submissions.Submissions)5 HashMap (java.util.HashMap)4 Nullable (org.jetbrains.annotations.Nullable)4 Step (org.stepik.api.objects.steps.Step)4 ArrayList (java.util.ArrayList)3 StudyObject (org.stepik.api.objects.StudyObject)3 TokenInfo (org.stepik.api.objects.auth.TokenInfo)3 Course (org.stepik.api.objects.courses.Course)3 Sections (org.stepik.api.objects.sections.Sections)3 Steps (org.stepik.api.objects.steps.Steps)3 User (org.stepik.api.objects.users.User)3 StepikAuthManager.getCurrentUser (org.stepik.core.stepik.StepikAuthManager.getCurrentUser)3 IOException (java.io.IOException)2 ClientResponse (org.stepik.api.client.ClientResponse)2 TransportClient (org.stepik.api.client.TransportClient)2