diff --git a/python/educational/src/com/jetbrains/edu/stepic/EduStepicConnector.java b/python/educational/src/com/jetbrains/edu/stepic/EduStepicConnector.java index 56bd969aa9f8..564226d9a06c 100644 --- a/python/educational/src/com/jetbrains/edu/stepic/EduStepicConnector.java +++ b/python/educational/src/com/jetbrains/edu/stepic/EduStepicConnector.java @@ -47,18 +47,21 @@ public class EduStepicConnector { private EduStepicConnector() { } + private static T getFromStepic(String link, final Class container) throws IOException { + return HttpRequests.request(stepicApiUrl + link).connect(new HttpRequests.RequestProcessor() { + @Override + public T process(@NotNull HttpRequests.Request request) throws IOException { + final BufferedReader reader = request.getReader(); + Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); + return gson.fromJson(reader, container); + } + }); + } + @NotNull public static List getCourses() { try { - return HttpRequests.request(stepicApiUrl + "courses/99").connect(new HttpRequests.RequestProcessor>() { - - @Override - public List process(@NotNull HttpRequests.Request request) throws IOException { - final BufferedReader reader = request.getReader(); - Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); - return gson.fromJson(reader, CoursesContainer.class).courses; - } - }); + return getFromStepic("courses/99", CoursesContainer.class).courses; } catch (IOException e) { LOG.error("IOException " + e.getMessage()); @@ -102,76 +105,49 @@ public class EduStepicConnector { } public static List getLessons(int sectionId) throws IOException { + final SectionWrapper sectionWrapper = getFromStepic("sections/" + String.valueOf(sectionId), SectionWrapper.class); + List unitIds = sectionWrapper.sections.get(0).units; + final List lessons = new ArrayList(); + for (Integer unitId : unitIds) { + UnitWrapper unit = getFromStepic("units/" + String.valueOf(unitId), UnitWrapper.class); + int lessonID = unit.units.get(0).lesson; + LessonContainer lesson = getFromStepic("lessons/" + String.valueOf(lessonID), LessonContainer.class); + Lesson realLesson = lesson.lessons.get(0); + lessons.add(realLesson); + } - final SectionWrapper sectionWrapper = HttpRequests.request(stepicApiUrl + "sections/" + String.valueOf(sectionId)) - .connect(new HttpRequests.RequestProcessor() { - - @Override - public SectionWrapper process(@NotNull HttpRequests.Request request) throws IOException { - final BufferedReader reader = request.getReader(); - Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); - return gson.fromJson(reader, SectionWrapper.class); - } - }); - final List lessons = getSortedLessons(sectionWrapper); for (Lesson lesson : lessons) { lesson.taskList = new ArrayList(); for (Integer s : lesson.steps) { - final Step step = getStep(s); - final Task task = new Task(); - task.setName(step.name); - task.setText(step.text); - for (TestFileWrapper wrapper : step.options.test) { - task.setTestsTexts(wrapper.name, wrapper.text); - } - - task.taskFiles = new HashMap(); // TODO: it looks like we don't need taskFiles as map anymore - if (step.options.files != null) { - for (TaskFile taskFile : step.options.files) { - task.taskFiles.put(taskFile.name, taskFile); - } - } - lesson.taskList.add(task); + createTask(lesson, s); } } return lessons; } - @NotNull - private static List getSortedLessons(SectionWrapper sectionWrapper) { - final List lessons = sectionWrapper.lessons; - - final List units = sectionWrapper.sections.get(0).units; - final List wrapperUnits = sectionWrapper.units; - - final HashMap unitsMap = new HashMap(); - for (SectionWrapper.Unit unit : wrapperUnits) { - unitsMap.put(unit.lesson, unit.id); + private static void createTask(Lesson lesson, Integer s) throws IOException { + final Step step = getStep(s); + final Task task = new Task(); + task.setName(step.name); + task.setText(step.text); + for (TestFileWrapper wrapper : step.options.test) { + task.setTestsTexts(wrapper.name, wrapper.text); } - Collections.sort(lessons, new Comparator() { - @Override - public int compare(Lesson l1, Lesson l2) { - return units.indexOf(unitsMap.get(l1.id)) - units.indexOf(unitsMap.get(l2.id)); + task.taskFiles = new HashMap(); // TODO: it looks like we don't need taskFiles as map anymore + if (step.options.files != null) { + for (TaskFile taskFile : step.options.files) { + task.taskFiles.put(taskFile.name, taskFile); } - }); - return lessons; + } + lesson.taskList.add(task); } public static Step getStep(Integer step) throws IOException { - return HttpRequests.request(stepicApiUrl + "steps/" + String.valueOf(step)).connect(new HttpRequests.RequestProcessor() { - - @Override - public Step process(@NotNull HttpRequests.Request request) throws IOException { - final BufferedReader reader = request.getReader(); - Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); - return gson.fromJson(reader, StepContainer.class).steps.get(0).block; - } - }); + return getFromStepic("steps/" + String.valueOf(step), StepContainer.class).steps.get(0).block; } - public static boolean postLesson(Project project, @NotNull final Lesson lesson) { final HttpPost request = new HttpPost(stepicApiUrl + "lessons"); final ArrayList
headers = getHeaders(request); @@ -324,6 +300,10 @@ public class EduStepicConnector { } } + static class LessonContainer { + List lessons; + } + static class StepSource { @Expose Step block; @Expose int position = 0; @@ -360,6 +340,13 @@ public class EduStepicConnector { } List units; + } + static class UnitWrapper { + static class Unit { + int lesson; + } + + List units; } }