Record pincode request URLs and harden fetches
All checks were successful
build-and-deploy / build-deploy (push) Successful in 33s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 33s
This commit is contained in:
@@ -41,17 +41,23 @@ class DataGovPincodeClient(
|
||||
.queryParam("api-key", apiKey)
|
||||
.queryParam("format", "json")
|
||||
.queryParam("filters[pincode]", pinCodeValue)
|
||||
.queryParam("offset", "0")
|
||||
.queryParam("limit", "100")
|
||||
.toUriString()
|
||||
val response = restTemplate.getForEntity(url, String::class.java)
|
||||
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "data.gov.in")
|
||||
val parsed = parseCityState(body, pinCodeValue)
|
||||
val status = when {
|
||||
parsed != null -> "OK"
|
||||
isFilterMismatch(body, pinCodeValue) -> "FILTER_MISMATCH"
|
||||
else -> "ZERO_RESULTS"
|
||||
return try {
|
||||
val response = restTemplate.getForEntity(url, String::class.java)
|
||||
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "data.gov.in", requestUrl = url)
|
||||
val parsed = parseCityState(body, pinCodeValue)
|
||||
val status = when {
|
||||
parsed != null -> "OK"
|
||||
isFilterMismatch(body, pinCodeValue) -> "FILTER_MISMATCH"
|
||||
else -> "ZERO_RESULTS"
|
||||
}
|
||||
val error = if (status == "FILTER_MISMATCH") "Records did not match pin filter" else null
|
||||
PincodeLookupResult(parsed, body, status, "data.gov.in", error, url)
|
||||
} catch (ex: Exception) {
|
||||
PincodeLookupResult(null, null, "ERROR", "data.gov.in", ex.message, url)
|
||||
}
|
||||
val error = if (status == "FILTER_MISMATCH") "Records did not match pin filter" else null
|
||||
return PincodeLookupResult(parsed, body, status, "data.gov.in", error)
|
||||
}
|
||||
|
||||
private fun parseCityState(body: String, pinCodeValue: String): String? {
|
||||
|
||||
@@ -438,17 +438,20 @@ class DocumentExtractionService(
|
||||
primary.status?.let { results["geoPrimaryStatus"] = it }
|
||||
primary.rawResponse?.let { results["geoPrimaryResponse"] = it.take(4000) }
|
||||
primary.errorMessage?.let { results["geoPrimaryError"] = it.take(300) }
|
||||
primary.requestUrl?.let { results["geoPrimaryUrl"] = it.take(500) }
|
||||
resolvedResult.secondary?.let { secondary ->
|
||||
results["geoSecondarySource"] = secondary.source
|
||||
secondary.status?.let { results["geoSecondaryStatus"] = it }
|
||||
secondary.rawResponse?.let { results["geoSecondaryResponse"] = it.take(4000) }
|
||||
secondary.errorMessage?.let { results["geoSecondaryError"] = it.take(300) }
|
||||
secondary.requestUrl?.let { results["geoSecondaryUrl"] = it.take(500) }
|
||||
}
|
||||
resolvedResult.tertiary?.let { tertiary ->
|
||||
results["geoTertiarySource"] = tertiary.source
|
||||
tertiary.status?.let { results["geoTertiaryStatus"] = it }
|
||||
tertiary.rawResponse?.let { results["geoTertiaryResponse"] = it.take(4000) }
|
||||
tertiary.errorMessage?.let { results["geoTertiaryError"] = it.take(300) }
|
||||
tertiary.requestUrl?.let { results["geoTertiaryUrl"] = it.take(500) }
|
||||
}
|
||||
val resolved = resolvedResult.resolved()?.resolvedCityState ?: return
|
||||
results["geoResolved"] = resolved
|
||||
|
||||
@@ -105,5 +105,6 @@ data class PincodeLookupResult(
|
||||
val rawResponse: String?,
|
||||
val status: String?,
|
||||
val source: String,
|
||||
val errorMessage: String? = null
|
||||
val errorMessage: String? = null,
|
||||
val requestUrl: String? = null
|
||||
)
|
||||
|
||||
@@ -18,20 +18,15 @@ class PostalPincodeClient(
|
||||
private val logger = LoggerFactory.getLogger(PostalPincodeClient::class.java)
|
||||
|
||||
fun resolve(pinCode: String): PincodeLookupResult {
|
||||
return try {
|
||||
val first = fetch(baseUrl, pinCode)
|
||||
if (first.resolvedCityState != null) return first
|
||||
if (first.status == "ERROR" && baseUrl.startsWith("https://")) {
|
||||
val httpUrl = baseUrl.replaceFirst("https://", "http://")
|
||||
val second = fetch(httpUrl, pinCode)
|
||||
if (second.resolvedCityState != null) return second
|
||||
return second
|
||||
}
|
||||
first
|
||||
} catch (ex: Exception) {
|
||||
logger.warn("Postalpincode lookup failed: {}", ex.message)
|
||||
PincodeLookupResult(null, null, "ERROR", "postalpincode.in", ex.message)
|
||||
val first = fetch(baseUrl, pinCode)
|
||||
if (first.resolvedCityState != null) return first
|
||||
if (first.status == "ERROR" && baseUrl.startsWith("https://")) {
|
||||
val httpUrl = baseUrl.replaceFirst("https://", "http://")
|
||||
val second = fetch(httpUrl, pinCode)
|
||||
if (second.resolvedCityState != null) return second
|
||||
return second
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
private fun fetch(base: String, pinCode: String): PincodeLookupResult {
|
||||
@@ -39,11 +34,16 @@ class PostalPincodeClient(
|
||||
.path("/pincode/{pin}")
|
||||
.buildAndExpand(pinCode)
|
||||
.toUriString()
|
||||
val response = restTemplate.getForEntity(url, String::class.java)
|
||||
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "postalpincode.in")
|
||||
val resolved = parseCityState(body)
|
||||
val status = if (resolved == null) "ZERO_RESULTS" else "OK"
|
||||
return PincodeLookupResult(resolved, body, status, "postalpincode.in")
|
||||
return try {
|
||||
val response = restTemplate.getForEntity(url, String::class.java)
|
||||
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "postalpincode.in", requestUrl = url)
|
||||
val resolved = parseCityState(body)
|
||||
val status = if (resolved == null) "ZERO_RESULTS" else "OK"
|
||||
PincodeLookupResult(resolved, body, status, "postalpincode.in", requestUrl = url)
|
||||
} catch (ex: Exception) {
|
||||
logger.warn("Postalpincode lookup failed: {}", ex.message)
|
||||
PincodeLookupResult(null, null, "ERROR", "postalpincode.in", ex.message, url)
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseCityState(body: String): String? {
|
||||
|
||||
Reference in New Issue
Block a user