Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions android/app/src/main/java/com/neph/core/network/JsonHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.json.JSONException
import org.json.JSONObject
import java.io.IOException
import java.net.HttpURLConnection
import java.net.SocketTimeoutException
import java.net.URL
import java.net.UnknownHostException

Expand All @@ -19,12 +20,17 @@ object JsonHttpClient {
method: String = "GET",
body: JSONObject? = null,
token: String? = null,
headers: Map<String, String> = emptyMap()
headers: Map<String, String> = emptyMap(),
connectTimeoutMillis: Int = ConnectTimeoutMillis,
readTimeoutMillis: Int = ReadTimeoutMillis,
timeoutMessage: String = "Something went wrong while connecting to the server. Please try again.",
timeoutStatus: Int = 0,
timeoutCode: String = "NETWORK_ERROR"
): JSONObject = withContext(Dispatchers.IO) {
val connection = (URL(buildUrl(path)).openConnection() as HttpURLConnection).apply {
requestMethod = method
connectTimeout = ConnectTimeoutMillis
readTimeout = ReadTimeoutMillis
connectTimeout = connectTimeoutMillis
readTimeout = readTimeoutMillis
doInput = true
setRequestProperty("Accept", "application/json")

Expand Down Expand Up @@ -75,6 +81,12 @@ object JsonHttpClient {
}
} catch (error: ApiException) {
throw error
} catch (_: SocketTimeoutException) {
throw ApiException(
message = timeoutMessage,
status = timeoutStatus,
code = timeoutCode
)
} catch (_: UnknownHostException) {
throw ApiException(
message = "Something went wrong while connecting to the server. Please try again.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ object GatheringAreasRepository {
internal const val DefaultLimit = 50
private const val MaxRadiusMeters = 10000
private const val MaxLimit = 50
private const val NearbyRequestTimeoutMillis = 8000L
private const val NearbyRequestTimeoutMillis = 12_000L
private const val NearbyRequestHttpTimeoutMillis = 12_000
private const val NearbyRequestTimeoutMessage = "Gathering areas request timed out."
private const val NearbyRequestTimeoutCode = "OVERPASS_TIMEOUT"
private const val DebugLogTag = "GatheringAreasRepo"

suspend fun fetchNearbyGatheringAreas(
Expand All @@ -74,12 +77,17 @@ object GatheringAreasRepository {
longitude,
normalizedRadius,
normalizedLimit
)
),
connectTimeoutMillis = NearbyRequestHttpTimeoutMillis,
readTimeoutMillis = NearbyRequestHttpTimeoutMillis,
timeoutMessage = NearbyRequestTimeoutMessage,
timeoutStatus = 504,
timeoutCode = NearbyRequestTimeoutCode
)
} ?: throw ApiException(
message = "Gathering areas request timed out.",
message = NearbyRequestTimeoutMessage,
status = 504,
code = "OVERPASS_TIMEOUT"
code = NearbyRequestTimeoutCode
)

return parseNearbyGatheringAreasResponse(
Expand All @@ -105,12 +113,17 @@ object GatheringAreasRepository {

val response = withTimeoutOrNull(NearbyRequestTimeoutMillis) {
JsonHttpClient.request(
path = "/gathering-areas/viewport?bbox=${urlEncode(bbox)}&limit=$normalizedLimit"
path = "/gathering-areas/viewport?bbox=${urlEncode(bbox)}&limit=$normalizedLimit",
connectTimeoutMillis = NearbyRequestHttpTimeoutMillis,
readTimeoutMillis = NearbyRequestHttpTimeoutMillis,
timeoutMessage = NearbyRequestTimeoutMessage,
timeoutStatus = 504,
timeoutCode = NearbyRequestTimeoutCode
)
} ?: throw ApiException(
message = "Gathering areas request timed out.",
message = NearbyRequestTimeoutMessage,
status = 504,
code = "OVERPASS_TIMEOUT"
code = NearbyRequestTimeoutCode
)

val parsed = parseNearbyGatheringAreasResponse(
Expand Down
Loading
Loading