Search in sources :

Example 1 with StepikUnauthorizedException

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

the class StepikFilesAction method get.

@NotNull
public String get(@NotNull String url, String contentType) {
    StepikApiClient stepikApi = getStepikApiClient();
    TransportClient transportClient = stepikApi.getTransportClient();
    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, contentType);
    ClientResponse response = transportClient.get(stepikApi, url, headers);
    if (response.getStatusCode() / 100 != 2) {
        String message = "Failed query to " + url + " returned the status code " + response.getStatusCode();
        logger.warn(message);
        if (response.getStatusCode() == StatusCodes.SC_UNAUTHORIZED) {
            throw new StepikUnauthorizedException(message);
        } else {
            throw new StepikClientException(message);
        }
    }
    String result = response.getBody();
    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 2 with StepikUnauthorizedException

use of org.stepik.api.exceptions.StepikUnauthorizedException 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)

Aggregations

HashMap (java.util.HashMap)2 NotNull (org.jetbrains.annotations.NotNull)2 ClientResponse (org.stepik.api.client.ClientResponse)2 StepikApiClient (org.stepik.api.client.StepikApiClient)2 TransportClient (org.stepik.api.client.TransportClient)2 StepikClientException (org.stepik.api.exceptions.StepikClientException)2 StepikUnauthorizedException (org.stepik.api.exceptions.StepikUnauthorizedException)2 TokenInfo (org.stepik.api.objects.auth.TokenInfo)2