Skip to content

Instantly share code, notes, and snippets.

@jderda
Created October 18, 2016 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jderda/3a9ddea19734c0e1a83cdc7fd07cbb3c to your computer and use it in GitHub Desktop.
Save jderda/3a9ddea19734c0e1a83cdc7fd07cbb3c to your computer and use it in GitHub Desktop.
Reworked version of data fetching code
package pl.kobietydokodu.wyzwanie;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class BaseDataFetch <T, RT> {
private CloseableHttpClient httpclient = HttpClients.createDefault();
protected Logger log = LoggerFactory.getLogger(getClass());
public final RT readRemoteData() {
HttpGet httpGet = new HttpGet(buildRequestURI());
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpGet);
log.debug("Received response: {}", response.getStatusLine());
HttpEntity entity = response.getEntity();
RT rawObject = readRawData(entity.getContent());
EntityUtils.consume(entity);
return rawObject;
} catch (IOException e) {
handleError(e);
} finally {
try {
response.close();
} catch (IOException e) {
log.warn("Cannot close stream", e);
}
}
throw new IllegalStateException("Fetching remote stream failed, cannot recover");
}
protected void handleError(Exception e) {
log.error("Unknown error during the process", e);
}
protected abstract URI buildRequestURI();
protected abstract RT readRawData(InputStream stream);
protected abstract T convert(RT intermediateType);
public T fetch() {
RT rawDataObject = readRemoteData();
T finalObject = convert(rawDataObject);
return finalObject;
}
}
package pl.kobietydokodu.wyzwanie;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public abstract class BaseJSONDataFetch <T> extends BaseDataFetch<T, JSONObject> {
@Override
protected JSONObject readRawData(InputStream stream) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new InputStreamReader(stream));
return (JSONObject) obj;
} catch (IOException e) {
handleError(e);
} catch (ParseException e) {
handleError(e);
}
throw new IllegalStateException("Cannot convert input stream to intermediate object");
}
}
package pl.kobietydokodu.wyzwanie;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.utils.URIBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class FetchWeather extends BaseJSONDataFetch<TimestampWithDescription> {
@Override
protected URI buildRequestURI() {
try {
URIBuilder requestUri = new URIBuilder("https://query.yahooapis.com/v1/public/yql");
requestUri.addParameter("format", "json");
requestUri.addParameter("q", "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"wrocław\")");
return requestUri.build();
} catch (URISyntaxException e) {
handleError(e);
throw new IllegalStateException("Cannot build URI", e);
}
}
@Override
protected TimestampWithDescription convert(JSONObject intermediateType) {
JSONObject queryObject = (JSONObject) intermediateType.get("query");
JSONObject resultsObject = (JSONObject) queryObject.get("results");
JSONObject channelObject = (JSONObject) resultsObject.get("channel");
JSONObject itemObject = (JSONObject) channelObject.get("item");
JSONArray forecastArray = (JSONArray) itemObject.get("forecast");
JSONObject forecastObject = (JSONObject) forecastArray.get(0);
String description = (String) forecastObject.get("text");
String date = (String) forecastObject.get("date");
return new TimestampWithDescription(date, description);
}
}
package pl.kobietydokodu.wyzwanie;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.utils.URIBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class FetchWikipedia extends BaseJSONDataFetch<TimestampWithDescription> {
@Override
protected URI buildRequestURI() {
try {
URIBuilder requestUri = new URIBuilder("https://en.wikipedia.org/w/api.php");
requestUri.addParameter("action", "query");
requestUri.addParameter("format", "json");
requestUri.addParameter("prop", "revisions");
requestUri.addParameter("titles", "Main Page");
return requestUri.build();
} catch (URISyntaxException e) {
handleError(e);
throw new IllegalStateException("Cannot build URI", e);
}
}
@Override
protected TimestampWithDescription convert(JSONObject intermediateType) {
JSONObject queryObject = (JSONObject) intermediateType.get("query");
JSONObject pagesObject = (JSONObject) queryObject.get("pages");
Object key = pagesObject.keySet().iterator().next();
JSONObject pageObject = (JSONObject) pagesObject.get(key);
JSONArray revisionsArray = (JSONArray) pageObject.get("revisions");
JSONObject revisionObject = (JSONObject) revisionsArray.get(0);
String description = (String) revisionObject.get("comment");
String date = (String) revisionObject.get("timestamp");
return new TimestampWithDescription(date, description);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment