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,9 +41,12 @@ class DataGovPincodeClient(
|
|||||||
.queryParam("api-key", apiKey)
|
.queryParam("api-key", apiKey)
|
||||||
.queryParam("format", "json")
|
.queryParam("format", "json")
|
||||||
.queryParam("filters[pincode]", pinCodeValue)
|
.queryParam("filters[pincode]", pinCodeValue)
|
||||||
|
.queryParam("offset", "0")
|
||||||
|
.queryParam("limit", "100")
|
||||||
.toUriString()
|
.toUriString()
|
||||||
|
return try {
|
||||||
val response = restTemplate.getForEntity(url, String::class.java)
|
val response = restTemplate.getForEntity(url, String::class.java)
|
||||||
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "data.gov.in")
|
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "data.gov.in", requestUrl = url)
|
||||||
val parsed = parseCityState(body, pinCodeValue)
|
val parsed = parseCityState(body, pinCodeValue)
|
||||||
val status = when {
|
val status = when {
|
||||||
parsed != null -> "OK"
|
parsed != null -> "OK"
|
||||||
@@ -51,7 +54,10 @@ class DataGovPincodeClient(
|
|||||||
else -> "ZERO_RESULTS"
|
else -> "ZERO_RESULTS"
|
||||||
}
|
}
|
||||||
val error = if (status == "FILTER_MISMATCH") "Records did not match pin filter" else null
|
val error = if (status == "FILTER_MISMATCH") "Records did not match pin filter" else null
|
||||||
return PincodeLookupResult(parsed, body, status, "data.gov.in", error)
|
PincodeLookupResult(parsed, body, status, "data.gov.in", error, url)
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
PincodeLookupResult(null, null, "ERROR", "data.gov.in", ex.message, url)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseCityState(body: String, pinCodeValue: String): String? {
|
private fun parseCityState(body: String, pinCodeValue: String): String? {
|
||||||
|
|||||||
@@ -438,17 +438,20 @@ class DocumentExtractionService(
|
|||||||
primary.status?.let { results["geoPrimaryStatus"] = it }
|
primary.status?.let { results["geoPrimaryStatus"] = it }
|
||||||
primary.rawResponse?.let { results["geoPrimaryResponse"] = it.take(4000) }
|
primary.rawResponse?.let { results["geoPrimaryResponse"] = it.take(4000) }
|
||||||
primary.errorMessage?.let { results["geoPrimaryError"] = it.take(300) }
|
primary.errorMessage?.let { results["geoPrimaryError"] = it.take(300) }
|
||||||
|
primary.requestUrl?.let { results["geoPrimaryUrl"] = it.take(500) }
|
||||||
resolvedResult.secondary?.let { secondary ->
|
resolvedResult.secondary?.let { secondary ->
|
||||||
results["geoSecondarySource"] = secondary.source
|
results["geoSecondarySource"] = secondary.source
|
||||||
secondary.status?.let { results["geoSecondaryStatus"] = it }
|
secondary.status?.let { results["geoSecondaryStatus"] = it }
|
||||||
secondary.rawResponse?.let { results["geoSecondaryResponse"] = it.take(4000) }
|
secondary.rawResponse?.let { results["geoSecondaryResponse"] = it.take(4000) }
|
||||||
secondary.errorMessage?.let { results["geoSecondaryError"] = it.take(300) }
|
secondary.errorMessage?.let { results["geoSecondaryError"] = it.take(300) }
|
||||||
|
secondary.requestUrl?.let { results["geoSecondaryUrl"] = it.take(500) }
|
||||||
}
|
}
|
||||||
resolvedResult.tertiary?.let { tertiary ->
|
resolvedResult.tertiary?.let { tertiary ->
|
||||||
results["geoTertiarySource"] = tertiary.source
|
results["geoTertiarySource"] = tertiary.source
|
||||||
tertiary.status?.let { results["geoTertiaryStatus"] = it }
|
tertiary.status?.let { results["geoTertiaryStatus"] = it }
|
||||||
tertiary.rawResponse?.let { results["geoTertiaryResponse"] = it.take(4000) }
|
tertiary.rawResponse?.let { results["geoTertiaryResponse"] = it.take(4000) }
|
||||||
tertiary.errorMessage?.let { results["geoTertiaryError"] = it.take(300) }
|
tertiary.errorMessage?.let { results["geoTertiaryError"] = it.take(300) }
|
||||||
|
tertiary.requestUrl?.let { results["geoTertiaryUrl"] = it.take(500) }
|
||||||
}
|
}
|
||||||
val resolved = resolvedResult.resolved()?.resolvedCityState ?: return
|
val resolved = resolvedResult.resolved()?.resolvedCityState ?: return
|
||||||
results["geoResolved"] = resolved
|
results["geoResolved"] = resolved
|
||||||
|
|||||||
@@ -105,5 +105,6 @@ data class PincodeLookupResult(
|
|||||||
val rawResponse: String?,
|
val rawResponse: String?,
|
||||||
val status: String?,
|
val status: String?,
|
||||||
val source: String,
|
val source: String,
|
||||||
val errorMessage: String? = null
|
val errorMessage: String? = null,
|
||||||
|
val requestUrl: String? = null
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ class PostalPincodeClient(
|
|||||||
private val logger = LoggerFactory.getLogger(PostalPincodeClient::class.java)
|
private val logger = LoggerFactory.getLogger(PostalPincodeClient::class.java)
|
||||||
|
|
||||||
fun resolve(pinCode: String): PincodeLookupResult {
|
fun resolve(pinCode: String): PincodeLookupResult {
|
||||||
return try {
|
|
||||||
val first = fetch(baseUrl, pinCode)
|
val first = fetch(baseUrl, pinCode)
|
||||||
if (first.resolvedCityState != null) return first
|
if (first.resolvedCityState != null) return first
|
||||||
if (first.status == "ERROR" && baseUrl.startsWith("https://")) {
|
if (first.status == "ERROR" && baseUrl.startsWith("https://")) {
|
||||||
@@ -27,11 +26,7 @@ class PostalPincodeClient(
|
|||||||
if (second.resolvedCityState != null) return second
|
if (second.resolvedCityState != null) return second
|
||||||
return second
|
return second
|
||||||
}
|
}
|
||||||
first
|
return first
|
||||||
} catch (ex: Exception) {
|
|
||||||
logger.warn("Postalpincode lookup failed: {}", ex.message)
|
|
||||||
PincodeLookupResult(null, null, "ERROR", "postalpincode.in", ex.message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetch(base: String, pinCode: String): PincodeLookupResult {
|
private fun fetch(base: String, pinCode: String): PincodeLookupResult {
|
||||||
@@ -39,11 +34,16 @@ class PostalPincodeClient(
|
|||||||
.path("/pincode/{pin}")
|
.path("/pincode/{pin}")
|
||||||
.buildAndExpand(pinCode)
|
.buildAndExpand(pinCode)
|
||||||
.toUriString()
|
.toUriString()
|
||||||
|
return try {
|
||||||
val response = restTemplate.getForEntity(url, String::class.java)
|
val response = restTemplate.getForEntity(url, String::class.java)
|
||||||
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "postalpincode.in")
|
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "postalpincode.in", requestUrl = url)
|
||||||
val resolved = parseCityState(body)
|
val resolved = parseCityState(body)
|
||||||
val status = if (resolved == null) "ZERO_RESULTS" else "OK"
|
val status = if (resolved == null) "ZERO_RESULTS" else "OK"
|
||||||
return PincodeLookupResult(resolved, body, status, "postalpincode.in")
|
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? {
|
private fun parseCityState(body: String): String? {
|
||||||
|
|||||||
Reference in New Issue
Block a user