Filter data.gov records by pin code
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s

This commit is contained in:
androidlover5842
2026-01-31 11:55:18 +05:30
parent e9e39e645c
commit 1b45a38c78

View File

@@ -29,7 +29,7 @@ class DataGovPincodeClient(
.toUriString()
val response = restTemplate.getForEntity(url, String::class.java)
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "data.gov.in")
val resolved = parseCityState(body)
val resolved = parseCityState(body, pinCode)
val status = if (resolved == null) "ZERO_RESULTS" else "OK"
PincodeLookupResult(resolved, body, status, "data.gov.in")
} catch (ex: Exception) {
@@ -38,11 +38,16 @@ class DataGovPincodeClient(
}
}
private fun parseCityState(body: String): String? {
private fun parseCityState(body: String, pinCode: String): String? {
val root = objectMapper.readTree(body)
val records = root.path("records")
if (!records.isArray || records.isEmpty) return null
val chosen = chooseRecord(records) ?: return null
val filtered = records.filter { record ->
val recordPin = record.path("pincode").asText(null)
recordPin?.trim() == pinCode
}
if (filtered.isEmpty()) return null
val chosen = chooseRecord(filtered) ?: return null
val district = chosen.path("district").asText(null)
val state = chosen.path("statename").asText(null)
val districtName = district?.let { toTitleCase(it) }
@@ -51,7 +56,7 @@ class DataGovPincodeClient(
return listOfNotNull(districtName?.ifBlank { null }, stateName?.ifBlank { null }).joinToString(", ")
}
private fun chooseRecord(records: JsonNode): JsonNode? {
private fun chooseRecord(records: List<JsonNode>): JsonNode? {
val delivery = records.firstOrNull { it.path("delivery").asText("").equals("Delivery", true) }
return delivery ?: records.firstOrNull()
}