Search in sources :

Example 6 with StepikApiClient

use of org.stepik.api.client.StepikApiClient in project intellij-plugins by StepicOrg.

the class StepikAbstractQuery method execute.

@NotNull
public T execute() {
    StepikApiClient stepikApi = stepikAction.getStepikApiClient();
    TransportClient transportClient = stepikApi.getTransportClient();
    String url = getUrl();
    Map<String, String> headers = new HashMap<>();
    TokenInfo tokenInfo = stepikApi.getTokenInfo();
    String accessToken = tokenInfo.getAccessToken();
    if (accessToken != null) {
        String tokenType = tokenInfo.getTokenType();
        headers.put(HttpHeaders.AUTHORIZATION, tokenType + " " + accessToken);
    }
    headers.put(HttpHeaders.CONTENT_TYPE, getContentType());
    ClientResponse response = null;
    switch(method) {
        case GET:
            url += "?" + mapToGetString();
            response = transportClient.get(stepikApi, url, headers);
            break;
        case POST:
            response = transportClient.post(stepikApi, url, getBody(), headers);
            break;
    }
    if (response.getStatusCode() / 100 != 2) {
        String message = "Failed query to " + getUrl() + " returned the status code " + response.getStatusCode();
        logger.warn(message);
        if (response.getStatusCode() == StatusCodes.SC_UNAUTHORIZED) {
            throw new StepikUnauthorizedException(message);
        } else {
            throw new StepikClientException(message);
        }
    }
    T result = response.getBody(responseClass);
    if (responseClass == VoidResult.class) {
        //noinspection unchecked
        return (T) new VoidResult();
    }
    if (result == null) {
        throw new StepikClientException("Request successfully but the response body is null: " + url);
    }
    return result;
}
Also used : ClientResponse(org.stepik.api.client.ClientResponse) StepikApiClient(org.stepik.api.client.StepikApiClient) StepikUnauthorizedException(org.stepik.api.exceptions.StepikUnauthorizedException) TransportClient(org.stepik.api.client.TransportClient) HashMap(java.util.HashMap) TokenInfo(org.stepik.api.objects.auth.TokenInfo) StepikClientException(org.stepik.api.exceptions.StepikClientException) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with StepikApiClient

use of org.stepik.api.client.StepikApiClient in project intellij-plugins by StepicOrg.

the class Metrics method postMetrics.

private static void postMetrics(@NotNull Project project, @NotNull Metric metric, @NotNull MetricsStatus status) {
    executor.schedule(() -> {
        StepikApiClient stepikApiClient;
        stepikApiClient = authAndGetStepikApiClient();
        if (!isAuthenticated()) {
            return;
        }
        final StepikMetricsPostQuery query = stepikApiClient.metrics().post().timestamp(System.currentTimeMillis() / 1000L).tags(metric.getTags()).data(metric.getData()).name("ide_plugin").tags("name", "S_Union").tags("ide_name", ApplicationInfo.getInstance().getVersionName()).data("ide_version", ApplicationInfo.getInstance().getBuild().toString()).data("plugin_version", PluginUtils.getVersion()).data("session", session).tags("status", status);
        StepikProjectManager projectManager = StepikProjectManager.getInstance(project);
        if (projectManager != null) {
            query.data("project_id", projectManager.getUuid()).tags("project_programming_language", projectManager.getDefaultLang().getName()).data("project_manager_version", projectManager.getVersion());
            StudyNode projectRoot = projectManager.getProjectRoot();
            if (projectRoot != null) {
                Class<? extends StudyNode> projectRootClass = projectRoot.getClass();
                query.tags("project_root_class", projectRootClass.getSimpleName()).data("project_root_id", projectRoot.getId());
                query.data("course_id", projectRoot.getCourseId(stepikApiClient));
            }
        }
        query.executeAsync().exceptionally((e) -> {
            String message = String.format("Failed post metric: %s", query.toString());
            logger.warn(message, e);
            return null;
        });
    }, 500, TimeUnit.MILLISECONDS);
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) StepikProjectManager(org.stepik.core.StepikProjectManager) StepikMetricsPostQuery(org.stepik.api.queries.metrics.StepikMetricsPostQuery) StudyNode(org.stepik.core.courseFormat.StudyNode)

Example 8 with StepikApiClient

use of org.stepik.api.client.StepikApiClient in project intellij-plugins by StepicOrg.

the class FreeAnswerQuizHelper method isFrozen.

private boolean isFrozen() {
    if (!needReview()) {
        return true;
    }
    Step data = getStepNode().getData();
    if (data == null) {
        return true;
    }
    int instructionId = data.getInstruction();
    if (instructionId == 0) {
        return true;
    }
    StepikApiClient stepikClient = authAndGetStepikApiClient();
    if (!isAuthenticated()) {
        return true;
    }
    try {
        Instructions instructions = stepikClient.instructions().get().id(instructionId).execute();
        if (!instructions.isEmpty()) {
            Instruction instruction = instructions.getFirst();
            return instruction.isFrozen();
        }
    } catch (StepikClientException e) {
        logger.warn(e);
    }
    return true;
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) Instructions(org.stepik.api.objects.instructions.Instructions) Step(org.stepik.api.objects.steps.Step) Instruction(org.stepik.api.objects.instructions.Instruction) StepikClientException(org.stepik.api.exceptions.StepikClientException)

Example 9 with StepikApiClient

use of org.stepik.api.client.StepikApiClient in project intellij-plugins by StepicOrg.

the class StepikProjectGenerator method createCourseNodeUnderProgress.

public void createCourseNodeUnderProgress(@NotNull final Project project, @NotNull StudyObject data) {
    ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
        if (data.getId() == 0) {
            logger.warn("Failed to get a course");
            Metrics.createProject(project, DATA_NOT_LOADED);
            return;
        }
        ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
        indicator.setIndeterminate(true);
        StepikApiClient stepikApiClient = authAndGetStepikApiClient();
        projectRoot = StudyNodeFactory.createTree(project, stepikApiClient, data);
    }, "Creating Project", true, project);
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 10 with StepikApiClient

use of org.stepik.api.client.StepikApiClient in project intellij-plugins by StepicOrg.

the class StepikProjectGenerator method getCourses.

@NotNull
public static CompletableFuture<List<StudyObject>> getCourses(@NotNull SupportedLanguages programmingLanguage) {
    return CompletableFuture.supplyAsync(() -> {
        List<StudyObject> courses = new ArrayList<>();
        List<Long> coursesIds = getHardcodedCoursesId(programmingLanguage);
        if (!coursesIds.isEmpty()) {
            StepikApiClient stepikApiClient = authAndGetStepikApiClient();
            try {
                courses = stepikApiClient.courses().get().id(coursesIds).execute().getCourses().stream().map(course -> (StudyObject) course).collect(Collectors.toList());
            } catch (StepikClientException e) {
                logger.warn("Failed get courses", e);
            }
        }
        courses.sort((course1, course2) -> {
            long id1 = course1.getId();
            long id2 = course2.getId();
            int index1 = coursesIds.indexOf(id1);
            int index2 = coursesIds.indexOf(id2);
            return Integer.compare(index1, index2);
        });
        return courses;
    });
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) ArrayList(java.util.ArrayList) StudyObject(org.stepik.api.objects.StudyObject) StepikClientException(org.stepik.api.exceptions.StepikClientException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StepikApiClient (org.stepik.api.client.StepikApiClient)22 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)18 StepikClientException (org.stepik.api.exceptions.StepikClientException)12 NotNull (org.jetbrains.annotations.NotNull)9 StudyNode (org.stepik.core.courseFormat.StudyNode)5 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)4 StepNode (org.stepik.core.courseFormat.StepNode)4 Task (com.intellij.openapi.progress.Task)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Nullable (org.jetbrains.annotations.Nullable)3 StudyObject (org.stepik.api.objects.StudyObject)3 TokenInfo (org.stepik.api.objects.auth.TokenInfo)3 Submission (org.stepik.api.objects.submissions.Submission)3 User (org.stepik.api.objects.users.User)3 StepikAuthManager.getCurrentUser (org.stepik.core.stepik.StepikAuthManager.getCurrentUser)3 Application (com.intellij.openapi.application.Application)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Project (com.intellij.openapi.project.Project)2