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;
}
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);
}
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;
}
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);
}
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;
});
}
Aggregations