mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-22 07:38:09 +00:00
[AB-258] improving handling of request failures for urban dictionary
This commit is contained in:
@@ -2,9 +2,11 @@ package dev.sheldan.abstracto.webservices.urban.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import dev.sheldan.abstracto.webservices.urban.exception.NoUrbanDefinitionFoundException;
|
||||
import dev.sheldan.abstracto.webservices.urban.exception.UrbanDictionaryRequestException;
|
||||
import dev.sheldan.abstracto.webservices.urban.model.UrbanDefinition;
|
||||
import dev.sheldan.abstracto.webservices.urban.model.UrbanResponse;
|
||||
import dev.sheldan.abstracto.webservices.urban.model.UrbanResponseDefinition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
@@ -15,6 +17,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class UrbanServiceBean implements UrbanService {
|
||||
|
||||
@Autowired
|
||||
@@ -30,6 +33,13 @@ public class UrbanServiceBean implements UrbanService {
|
||||
public UrbanDefinition getUrbanDefinition(String query) throws IOException {
|
||||
Request request = new Request.Builder().url(String.format(requestUrl, query)).get().build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
if(!response.isSuccessful()) {
|
||||
if(log.isDebugEnabled()) {
|
||||
log.error("Failed to retrieve urban dictionary definition. Response had code {} with body {}.",
|
||||
response.code(), response.body());
|
||||
}
|
||||
throw new UrbanDictionaryRequestException(response.code());
|
||||
}
|
||||
UrbanResponse urbanResponse = gson.fromJson(response.body().string(), UrbanResponse.class);
|
||||
if(urbanResponse.getList().isEmpty()) {
|
||||
throw new NoUrbanDefinitionFoundException();
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package dev.sheldan.abstracto.webservices.urban.exception;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import dev.sheldan.abstracto.core.templating.Templatable;
|
||||
import dev.sheldan.abstracto.webservices.urban.model.exception.UrbanDictionaryRequestExceptionModel;
|
||||
|
||||
public class UrbanDictionaryRequestException extends AbstractoRunTimeException implements Templatable {
|
||||
|
||||
private final UrbanDictionaryRequestExceptionModel model;
|
||||
|
||||
public UrbanDictionaryRequestException(Integer responseCode) {
|
||||
super(String.format("Request failure towards urban dictionary %s.", responseCode));
|
||||
this.model = UrbanDictionaryRequestExceptionModel
|
||||
.builder()
|
||||
.responseCode(responseCode)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return "urban_dictionary_request_exception";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
return model;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package dev.sheldan.abstracto.webservices.urban.model.exception;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class UrbanDictionaryRequestExceptionModel implements Serializable {
|
||||
private Integer responseCode;
|
||||
}
|
||||
Reference in New Issue
Block a user