Skip to content

Commit

Permalink
Fixed #12
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Oct 14, 2022
1 parent 8cedc7c commit d46b2ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A Java implementation of [Deezer API](https://developers.deezer.com/api).
<dependency>
<groupId>com.github.yvasyliev</groupId>
<artifactId>deezer-api</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.github.yvasyliev</groupId>
<artifactId>deezer-api</artifactId>
<packaging>jar</packaging>
<version>1.1.0</version>
<version>1.1.1</version>
<name>Deezer API Java Library</name>
<description>A Java implementation of Deezer API.</description>
<url>https://github.com/yvasyliev/deezer-api</url>
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/api/deezer/validators/DeezerResponseValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,27 @@
* Validates Deezer response.
*/
public class DeezerResponseValidator implements Predicate<String> {
/**
* <code>wrong code</code> body content.
*/
private static final String WRONG_CODE = "wrong code";

/**
* Converts Deezer response to {@link JsonElement}.
*/
private static final Converter<String, JsonElement> JSON_ELEMENT_CONVERTER = new JsonElementConverter();

@Override
public boolean test(String responseBody) {
return !isWrongCode(responseBody) && !hasErrorField(responseBody);
}

private boolean isWrongCode(String responseBody) {
return WRONG_CODE.equals(responseBody);
}

private boolean hasErrorField(String responseBody) {
JsonElement jsonElement = JSON_ELEMENT_CONVERTER.convert(responseBody);
return !jsonElement.isJsonObject() || !jsonElement.getAsJsonObject().has("error");
return jsonElement.isJsonObject() && jsonElement.getAsJsonObject().has("error");
}
}

0 comments on commit d46b2ff

Please sign in to comment.