Search in sources :

Example 21 with StepikClientException

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

the class Node method getStatus.

@NotNull
@Override
public StudyStatus getStatus() {
    if (isUnknownStatus()) {
        status = UNCHECKED;
        executor.execute(() -> {
            StepikApiClient stepikApiClient = authAndGetStepikApiClient();
            if (!isAuthenticated()) {
                return;
            }
            Map<String, StudyNode> progressMap = new HashMap<>();
            Node.this.getChildren().stream().filter(StudyNode::isUnknownStatus).forEach(child -> {
                DC data = child.getData();
                if (data != null) {
                    progressMap.put(data.getProgress(), child);
                }
            });
            D data = Node.this.getData();
            if (data != null) {
                String progressId = data.getProgress();
                if (progressId != null) {
                    progressMap.put(progressId, Node.this);
                }
                Set<String> progressIds = progressMap.keySet();
                if (!progressIds.isEmpty()) {
                    int size = progressIds.size();
                    String[] list = progressIds.toArray(new String[size]);
                    int start = 0;
                    int end;
                    while (start < size) {
                        end = start + 20;
                        if (end > size) {
                            end = size;
                        }
                        String[] part = Arrays.copyOfRange(list, start, end);
                        start = end;
                        Progresses progresses;
                        try {
                            progresses = stepikApiClient.progresses().get().id(part).execute();
                        } catch (StepikClientException e) {
                            logger.warn(e);
                            return;
                        }
                        progresses.getItems().forEach(progress -> {
                            String id = progress.getId();
                            StudyNode node = progressMap.get(id);
                            if (progress.isPassed()) {
                                node.setRawStatus(SOLVED);
                            }
                        });
                    }
                }
            }
            ApplicationManager.getApplication().invokeLater(() -> {
                if (!project.isDisposed()) {
                    ProjectView.getInstance(project).refresh();
                }
            });
        });
    }
    return status;
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) SOLVED(org.stepik.core.courseFormat.StudyStatus.SOLVED) UNCHECKED(org.stepik.core.courseFormat.StudyStatus.UNCHECKED) HashMap(java.util.HashMap) Progresses(org.stepik.api.objects.progresses.Progresses) StepikClientException(org.stepik.api.exceptions.StepikClientException) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with StepikClientException

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

the class SectionNode method getChildDataList.

@Override
protected List<CompoundUnitLesson> getChildDataList(@NotNull StepikApiClient stepikApiClient) {
    List<CompoundUnitLesson> objects = new ArrayList<>();
    try {
        List<Long> unitsIds;
        Section data = getData();
        unitsIds = data != null ? data.getUnits() : Collections.emptyList();
        if (!unitsIds.isEmpty()) {
            int size = unitsIds.size();
            Long[] list = unitsIds.toArray(new Long[size]);
            int start = 0;
            int end;
            while (start < size) {
                end = start + 20;
                if (end > size) {
                    end = size;
                }
                Long[] part = Arrays.copyOfRange(list, start, end);
                start = end;
                Units units = stepikApiClient.units().get().id(part).execute();
                Map<Long, Unit> unitsMap = new HashMap<>();
                List<Long> lessonsIds = new ArrayList<>();
                units.getUnits().forEach(unit -> {
                    long lessonId = unit.getLesson();
                    lessonsIds.add(lessonId);
                    unitsMap.put(lessonId, unit);
                });
                Lessons lessons = stepikApiClient.lessons().get().id(lessonsIds).execute();
                lessons.getLessons().forEach(lesson -> objects.add(new CompoundUnitLesson(unitsMap.get(lesson.getId()), lesson)));
            }
        }
    } catch (StepikClientException logged) {
        logger.warn("A section initialization don't is fully", logged);
    }
    return objects;
}
Also used : HashMap(java.util.HashMap) Lessons(org.stepik.api.objects.lessons.Lessons) ArrayList(java.util.ArrayList) Unit(org.stepik.api.objects.units.Unit) Section(org.stepik.api.objects.sections.Section) StepikClientException(org.stepik.api.exceptions.StepikClientException) Units(org.stepik.api.objects.units.Units) CompoundUnitLesson(org.stepik.api.objects.lessons.CompoundUnitLesson)

Example 23 with StepikClientException

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

the class SectionNode method loadData.

@Override
protected boolean loadData(@NotNull StepikApiClient stepikApiClient, long id) {
    try {
        Sections sections = stepikApiClient.sections().get().id(id).execute();
        Section data;
        if (!sections.isEmpty()) {
            data = sections.getFirst();
        } else {
            data = new Section();
            data.setId(id);
        }
        Section oldData = this.getData();
        setData(data);
        return oldData == null || !oldData.getUpdateDate().equals(data.getUpdateDate());
    } catch (StepikClientException logged) {
        logger.warn(String.format("Failed load section data id=%d", id), logged);
    }
    return true;
}
Also used : Sections(org.stepik.api.objects.sections.Sections) Section(org.stepik.api.objects.sections.Section) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Example 24 with StepikClientException

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

the class StudyUtils method getRecommendation.

@Nullable
public static StudyNode<?, ?> getRecommendation(@NotNull StudyNode root) {
    StudyObject data = root.getData();
    if (data == null || !data.isAdaptive()) {
        return null;
    }
    StudyNode studyNode = null;
    StepikApiClient stepikClient = authAndGetStepikApiClient();
    if (!isAuthenticated()) {
        return null;
    }
    try {
        Recommendations recommendations = stepikClient.recommendations().get().course(root.getId()).execute();
        if (!recommendations.isEmpty()) {
            Recommendation recommendation = recommendations.getFirst();
            long lesson = recommendation.getLesson();
            Steps steps = stepikClient.steps().get().lesson(lesson).execute();
            if (!steps.isEmpty()) {
                long stepId = steps.getFirst().getId();
                studyNode = root.getChildByClassAndId(Step.class, stepId);
            }
        }
    } catch (StepikClientException e) {
        logger.warn(e);
    }
    return studyNode;
}
Also used : Recommendations(org.stepik.api.objects.recommendations.Recommendations) Steps(org.stepik.api.objects.steps.Steps) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) StudyObject(org.stepik.api.objects.StudyObject) Step(org.stepik.api.objects.steps.Step) StepikClientException(org.stepik.api.exceptions.StepikClientException) Recommendation(org.stepik.api.objects.recommendations.Recommendation) StudyNode(org.stepik.core.courseFormat.StudyNode) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with StepikClientException

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

the class CourseNode method getChildDataList.

@Override
protected List<Section> getChildDataList(@NotNull StepikApiClient stepikApiClient) {
    Sections sections = new Sections();
    try {
        Course data = getData();
        List<Long> sectionsIds = data != null ? getData().getSections() : Collections.emptyList();
        if (!sectionsIds.isEmpty()) {
            sections = stepikApiClient.sections().get().id(sectionsIds).execute();
        }
    } catch (StepikClientException logged) {
        logger.warn("A course initialization don't is fully", logged);
    }
    return sections.getSections();
}
Also used : Sections(org.stepik.api.objects.sections.Sections) Course(org.stepik.api.objects.courses.Course) 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