Skip to content

Commit

Permalink
Fix Json parse exception (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
blizniukov authored Oct 26, 2022
1 parent 3e5b553 commit bf1a840
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.github.redouane59.twitter.helpers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.scribejava.apis.TwitterApi;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth10aService;
import io.github.redouane59.twitter.TwitterClient;
import io.github.redouane59.twitter.signature.TwitterCredentials;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -104,13 +106,21 @@ public <T> Optional<T> makeRequest(OAuthRequest request, boolean signRequired, C
} else if (response.getCode() < 200 || response.getCode() > 299) {
logApiError(request.getVerb().name(), request.getUrl(), stringResponse, response.getCode());
}
result = JsonHelper.fromJson(stringResponse, classType);
result = convert(stringResponse, classType);
} catch (IOException ex) {
LOGGER.error("Error occupied on executing request", ex);
}
return Optional.ofNullable(result);
}

protected <T> T convert(String json, Class<? extends T> targetClass) throws JsonProcessingException {
if (targetClass.isInstance(json)) {
return (T) json;
} else {
return JsonHelper.fromJson(json, targetClass);
}
}

public abstract <T> Optional<T> getRequest(String url, Class<T> classType);

public abstract <T> Optional<T> getRequestWithParameters(String url, Map<String, String> parameters, Class<T> classType);
Expand Down

0 comments on commit bf1a840

Please sign in to comment.