Skip to content

Commit

Permalink
Updated to latest Geocaching Java API [GH-103]
Browse files Browse the repository at this point in the history
  • Loading branch information
arcao committed May 28, 2018
1 parent b00674f commit 3913971
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.arcao.geocaching.api.configuration.GeocachingApiConfiguration;
import com.arcao.geocaching.api.configuration.impl.DefaultProductionGeocachingApiConfiguration;
import com.arcao.geocaching.api.configuration.impl.DefaultStagingGeocachingApiConfiguration;
import com.arcao.geocaching.api.downloader.JsonDownloader;
import com.arcao.geocaching.api.downloader.OkHttpClientJsonDownloader;
import com.arcao.geocaching.api.downloader.Downloader;
import com.arcao.geocaching.api.downloader.OkHttpClientDownloader;
import com.arcao.geocaching4locus.BuildConfig;

import java.util.concurrent.TimeUnit;
Expand All @@ -14,20 +14,20 @@
public class GeocachingApiFactory {
private static GeocachingApiConfiguration apiConfiguration;
private static OkHttpClient client;
private static JsonDownloader jsonDownloader;
private static Downloader downloader;

public static GeocachingApi create() {
return LiveGeocachingApi.builder()
.configuration(getApiConfiguration())
.downloader(getJsonDownloader())
.downloader(getDownloader())
.build();
}

public static JsonDownloader getJsonDownloader() {
if (jsonDownloader == null) {
jsonDownloader = new OkHttpClientJsonDownloader(getOkHttpClient());
public static Downloader getDownloader() {
if (downloader == null) {
downloader = new OkHttpClientDownloader(getOkHttpClient());
}
return jsonDownloader;
return downloader;
}

public static OkHttpClient getOkHttpClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.arcao.geocaching.api.exception.InvalidResponseException;
import com.arcao.geocaching.api.exception.NetworkException;
import com.arcao.geocaching.api.parser.JsonReader;
import com.arcao.geocaching4locus.BuildConfig;

import java.io.Reader;
Expand All @@ -16,19 +15,17 @@
import okhttp3.ResponseBody;
import timber.log.Timber;

public class OkHttpClientJsonDownloader implements JsonDownloader {
public class OkHttpClientDownloader implements Downloader {
private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

private final OkHttpClient client;

public OkHttpClientJsonDownloader(OkHttpClient client) {
public OkHttpClientDownloader(OkHttpClient client) {
this.client = client;
}

@Override
public JsonReader get(URL url) throws NetworkException, InvalidResponseException {
Reader reader;

public Reader get(URL url) throws NetworkException, InvalidResponseException {
try {
Request request = new Request.Builder()
.url(url)
Expand All @@ -49,8 +46,7 @@ public JsonReader get(URL url) throws NetworkException, InvalidResponseException
if (body == null)
throw new InvalidResponseException("Body is null!");

reader = body.charStream();
return new JsonReader(reader);
return body.charStream();
} catch (InvalidResponseException e) {
Timber.e(e);
throw e;
Expand All @@ -61,9 +57,7 @@ public JsonReader get(URL url) throws NetworkException, InvalidResponseException
}

@Override
public JsonReader post(URL url, byte[] postData) throws NetworkException, InvalidResponseException {
Reader reader;

public Reader post(URL url, byte[] postData) throws NetworkException, InvalidResponseException {
try {
Request request = new Request.Builder()
.url(url)
Expand All @@ -85,8 +79,7 @@ public JsonReader post(URL url, byte[] postData) throws NetworkException, Invali
if (body == null)
throw new InvalidResponseException("Body is null!");

reader = body.charStream();
return new JsonReader(reader);
return body.charStream();
} catch (InvalidResponseException e) {
Timber.e(e);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public final class WherigoApiFactory {
public static WherigoService create() {
return new WherigoServiceImpl(GeocachingApiFactory.getJsonDownloader());
return new WherigoServiceImpl(GeocachingApiFactory.getDownloader());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.arcao.wherigoservice.api;

import com.arcao.geocaching.api.downloader.JsonDownloader;
import com.arcao.geocaching.api.downloader.Downloader;
import com.arcao.geocaching.api.exception.InvalidResponseException;
import com.arcao.geocaching.api.exception.NetworkException;
import com.arcao.geocaching.api.parser.JsonReader;
import com.arcao.geocaching.api.util.DefaultValueJsonReader;
import com.arcao.wherigoservice.api.parser.WherigoJsonResultParser;
import com.arcao.wherigoservice.api.parser.WherigoJsonResultParser.Result;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.MalformedJsonException;

import org.slf4j.Logger;
Expand All @@ -22,9 +23,9 @@ public class WherigoServiceImpl implements WherigoService {
private static final Logger logger = LoggerFactory.getLogger(WherigoServiceImpl.class);

private static final String BASE_URL = "https://wherigo-service.appspot.com/api";
private final JsonDownloader downloader;
private final Downloader downloader;

public WherigoServiceImpl(JsonDownloader downloader) {
public WherigoServiceImpl(Downloader downloader) {
this.downloader = downloader;
}

Expand Down Expand Up @@ -145,7 +146,7 @@ private JsonReader callGet(String function) throws NetworkException, InvalidResp

try {
URL url = new URL(BASE_URL + "/" + function);
return downloader.get(url);
return new DefaultValueJsonReader(downloader.get(url));
} catch (MalformedURLException e) {
logger.error(e.toString(), e);
throw new NetworkException("Error while downloading data (" + e.getClass().getSimpleName() + ")", e);
Expand All @@ -160,7 +161,7 @@ protected JsonReader callPost(String function, String postBody) throws NetworkEx
byte[] postData = postBody.getBytes("UTF-8");
URL url = new URL(BASE_URL + "/" + function);

return downloader.post(url, postData);
return new DefaultValueJsonReader(downloader.post(url, postData));
} catch (MalformedURLException e) {
logger.error(e.toString(), e);
throw new NetworkException("Error while downloading data (" + e.getClass().getSimpleName() + ")", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.arcao.wherigoservice.api.parser;


import com.arcao.geocaching.api.parser.JsonReader;
import com.google.gson.stream.JsonReader;

import java.io.IOException;

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Versions {

const val support = "27.1.1"
const val constraint_layout = "1.1.0"
const val geocaching_api = "2.0.2"
const val geocaching_api = "2.1.0"
const val locus_api_android = "0.2.21"
const val slf4j_timber = "3.0"
const val timber = "4.7.0"
Expand Down

0 comments on commit 3913971

Please sign in to comment.