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