diff options
Diffstat (limited to 'main/src/cgeo/geocaching/connector/ec/ECApi.java')
-rw-r--r-- | main/src/cgeo/geocaching/connector/ec/ECApi.java | 101 |
1 files changed, 60 insertions, 41 deletions
diff --git a/main/src/cgeo/geocaching/connector/ec/ECApi.java b/main/src/cgeo/geocaching/connector/ec/ECApi.java index 421d112..86f7717 100644 --- a/main/src/cgeo/geocaching/connector/ec/ECApi.java +++ b/main/src/cgeo/geocaching/connector/ec/ECApi.java @@ -9,22 +9,25 @@ import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.files.GPX10Parser; -import cgeo.geocaching.geopoint.Geopoint; -import cgeo.geocaching.geopoint.Viewport; import cgeo.geocaching.list.StoredList; +import cgeo.geocaching.location.Geopoint; +import cgeo.geocaching.location.Viewport; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.utils.JsonUtils; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.SynchronizedDateFormat; import ch.boye.httpclientandroidlib.HttpResponse; +import com.fasterxml.jackson.databind.JsonNode; + import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; @@ -34,18 +37,27 @@ import java.util.List; import java.util.Locale; import java.util.TimeZone; -public class ECApi { +final class ECApi { + @NonNull private static final String API_HOST = "https://extremcaching.com/exports/"; + @NonNull private static final ECLogin ecLogin = ECLogin.getInstance(); + + @NonNull private static final SynchronizedDateFormat LOG_DATE_FORMAT = new SynchronizedDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", TimeZone.getTimeZone("UTC"), Locale.US); - public static String getIdFromGeocode(final String geocode) { + private ECApi() { + // utility class with static methods + } + + static String getIdFromGeocode(final String geocode) { return StringUtils.removeStartIgnoreCase(geocode, "EC"); } - public static Geocache searchByGeoCode(final String geocode) { + @Nullable + static Geocache searchByGeoCode(final String geocode) { final Parameters params = new Parameters("id", getIdFromGeocode(geocode)); final HttpResponse response = apiRequest("gpx.php", params); @@ -56,7 +68,8 @@ public class ECApi { return null; } - public static Collection<Geocache> searchByBBox(final Viewport viewport) { + @NonNull + static Collection<Geocache> searchByBBox(final Viewport viewport) { if (viewport.getLatitudeSpan() == 0 || viewport.getLongitudeSpan() == 0) { return Collections.emptyList(); @@ -72,8 +85,8 @@ public class ECApi { return importCachesFromJSON(response); } - - public static Collection<Geocache> searchByCenter(final Geopoint center) { + @NonNull + static Collection<Geocache> searchByCenter(final Geopoint center) { final Parameters params = new Parameters("fnc", "center"); params.add("distance", "20"); @@ -84,11 +97,13 @@ public class ECApi { return importCachesFromJSON(response); } - public static LogResult postLog(final Geocache cache, final LogType logType, final Calendar date, final String log) { + @NonNull + static LogResult postLog(@NonNull final Geocache cache, @NonNull final LogType logType, @NonNull final Calendar date, @NonNull final String log) { return postLog(cache, logType, date, log, false); } - public static LogResult postLog(final Geocache cache, final LogType logType, final Calendar date, final String log, boolean isRetry) { + @NonNull + private static LogResult postLog(@NonNull final Geocache cache, @NonNull final LogType logType, @NonNull final Calendar date, @NonNull final String log, final boolean isRetry) { final Parameters params = new Parameters("cache_id", cache.getGeocode()); params.add("type", logType.type); params.add("log", log); @@ -99,7 +114,7 @@ public class ECApi { final HttpResponse response = Network.postRequest(uri, params); if (response == null) { - return new LogResult(StatusCode.LOG_POST_ERROR_EC, ""); + return new LogResult(StatusCode.LOG_POST_ERROR, ""); } if (!isRetry && response.getStatusLine().getStatusCode() == 403) { if (ecLogin.login() == StatusCode.NO_ERROR) { @@ -107,7 +122,7 @@ public class ECApi { } } if (response.getStatusLine().getStatusCode() != 200) { - return new LogResult(StatusCode.LOG_POST_ERROR_EC, ""); + return new LogResult(StatusCode.LOG_POST_ERROR, ""); } final String data = Network.getResponseDataAlways(response); @@ -119,18 +134,20 @@ public class ECApi { return new LogResult(StatusCode.NO_ERROR, uid); } - return new LogResult(StatusCode.LOG_POST_ERROR_EC, ""); + return new LogResult(StatusCode.LOG_POST_ERROR, ""); } - + @Nullable private static HttpResponse apiRequest(final Parameters params) { return apiRequest("api.php", params); } + @Nullable private static HttpResponse apiRequest(final String uri, final Parameters params) { return apiRequest(uri, params, false); } + @Nullable private static HttpResponse apiRequest(final String uri, final Parameters params, final boolean isRetry) { // add session and cgeo marker on every request if (!isRetry) { @@ -155,64 +172,66 @@ public class ECApi { return response; } + @NonNull private static Collection<Geocache> importCachesFromGPXResponse(final HttpResponse response) { if (response == null) { return Collections.emptyList(); } try { - return new GPX10Parser(StoredList.TEMPORARY_LIST_ID).parse(response.getEntity().getContent(), null); - } catch (Exception e) { + return new GPX10Parser(StoredList.TEMPORARY_LIST.id).parse(response.getEntity().getContent(), null); + } catch (final Exception e) { Log.e("Error importing gpx from extremcaching.com", e); return Collections.emptyList(); } } + @NonNull private static List<Geocache> importCachesFromJSON(final HttpResponse response) { if (response != null) { try { - final String data = Network.getResponseDataAlways(response); - if (StringUtils.isBlank(data) || StringUtils.equals(data, "[]")) { + final JsonNode json = JsonUtils.reader.readTree(Network.getResponseDataAlways(response)); + if (!json.isArray()) { return Collections.emptyList(); } - final JSONArray json = new JSONArray(data); - final int len = json.length(); - final List<Geocache> caches = new ArrayList<>(len); - for (int i = 0; i < len; i++) { - final Geocache cache = parseCache(json.getJSONObject(i)); + final List<Geocache> caches = new ArrayList<>(json.size()); + for (final JsonNode node: json) { + final Geocache cache = parseCache(node); if (cache != null) { caches.add(cache); } } return caches; - } catch (final JSONException e) { - Log.w("JSONResult", e); + } catch (IOException | ClassCastException e) { + Log.w("importCachesFromJSON", e); } } return Collections.emptyList(); } - private static Geocache parseCache(final JSONObject response) { - final Geocache cache = new Geocache(); - cache.setReliableLatLon(true); + @Nullable + private static Geocache parseCache(final JsonNode response) { try { - cache.setGeocode("EC" + response.getString("cache_id")); - cache.setName(response.getString("title")); - cache.setCoords(new Geopoint(response.getString("lat"), response.getString("lon"))); - cache.setType(getCacheType(response.getString("type"))); - cache.setDifficulty((float) response.getDouble("difficulty")); - cache.setTerrain((float) response.getDouble("terrain")); - cache.setSize(CacheSize.getById(response.getString("size"))); - cache.setFound(response.getInt("found") == 1); + final Geocache cache = new Geocache(); + cache.setReliableLatLon(true); + cache.setGeocode("EC" + response.get("cache_id").asText()); + cache.setName(response.get("title").asText()); + cache.setCoords(new Geopoint(response.get("lat").asText(), response.get("lon").asText())); + cache.setType(getCacheType(response.get("type").asText())); + cache.setDifficulty((float) response.get("difficulty").asDouble()); + cache.setTerrain((float) response.get("terrain").asDouble()); + cache.setSize(CacheSize.getById(response.get("size").asText())); + cache.setFound(response.get("found").asInt() == 1); DataStore.saveCache(cache, EnumSet.of(SaveFlag.CACHE)); - } catch (final JSONException e) { + return cache; + } catch (final NullPointerException e) { Log.e("ECApi.parseCache", e); return null; } - return cache; } + @NonNull private static CacheType getCacheType(final String cacheType) { if (cacheType.equalsIgnoreCase("Tradi")) { return CacheType.TRADITIONAL; |