Tweak data.gov query and add postal retries
All checks were successful
build-and-deploy / build-deploy (push) Successful in 18s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 18s
This commit is contained in:
@@ -22,14 +22,7 @@ class DataGovPincodeClient(
|
|||||||
fun resolve(pinCode: String): PincodeLookupResult {
|
fun resolve(pinCode: String): PincodeLookupResult {
|
||||||
if (apiKey.isBlank()) return PincodeLookupResult(null, null, "NO_API_KEY", "data.gov.in", "Missing API key")
|
if (apiKey.isBlank()) return PincodeLookupResult(null, null, "NO_API_KEY", "data.gov.in", "Missing API key")
|
||||||
return try {
|
return try {
|
||||||
val first = fetch(pinCode)
|
fetch(pinCode)
|
||||||
if (first.resolvedCityState != null) return first
|
|
||||||
val second = fetch("${pinCode}.0")
|
|
||||||
if (second.resolvedCityState != null) return second
|
|
||||||
// Prefer more explicit mismatch status if records exist but don't match pin.
|
|
||||||
if (first.status == "FILTER_MISMATCH") return first
|
|
||||||
if (second.status == "FILTER_MISMATCH") return second
|
|
||||||
first
|
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
logger.warn("Data.gov.in lookup failed: {}", ex.message)
|
logger.warn("Data.gov.in lookup failed: {}", ex.message)
|
||||||
PincodeLookupResult(null, null, "ERROR", "data.gov.in", ex.message)
|
PincodeLookupResult(null, null, "ERROR", "data.gov.in", ex.message)
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ package com.android.trisolarisserver.component
|
|||||||
import com.fasterxml.jackson.databind.JsonNode
|
import com.fasterxml.jackson.databind.JsonNode
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.springframework.http.HttpEntity
|
||||||
|
import org.springframework.http.HttpHeaders
|
||||||
|
import org.springframework.http.HttpMethod
|
||||||
import org.springframework.beans.factory.annotation.Value
|
import org.springframework.beans.factory.annotation.Value
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
import org.springframework.web.client.RestTemplate
|
import org.springframework.web.client.RestTemplate
|
||||||
@@ -34,17 +37,35 @@ class PostalPincodeClient(
|
|||||||
.path("/pincode/{pin}")
|
.path("/pincode/{pin}")
|
||||||
.buildAndExpand(pinCode)
|
.buildAndExpand(pinCode)
|
||||||
.toUriString()
|
.toUriString()
|
||||||
return try {
|
val headers = HttpHeaders().apply {
|
||||||
val response = restTemplate.getForEntity(url, String::class.java)
|
set("User-Agent", "Mozilla/5.0 (TrisolarisServer)")
|
||||||
|
set("Accept", "application/json")
|
||||||
|
set("Connection", "close")
|
||||||
|
}
|
||||||
|
val entity = HttpEntity<Unit>(headers)
|
||||||
|
var lastError: Exception? = null
|
||||||
|
repeat(2) {
|
||||||
|
try {
|
||||||
|
val response = restTemplate.exchange(url, HttpMethod.GET, entity, String::class.java)
|
||||||
val body = response.body ?: return PincodeLookupResult(null, null, "EMPTY_BODY", "postalpincode.in", requestUrl = url)
|
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"
|
||||||
PincodeLookupResult(resolved, body, status, "postalpincode.in", requestUrl = url)
|
return PincodeLookupResult(resolved, body, status, "postalpincode.in", requestUrl = url)
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
logger.warn("Postalpincode lookup failed: {}", ex.message)
|
lastError = ex
|
||||||
PincodeLookupResult(null, null, "ERROR", "postalpincode.in", ex.message, url)
|
try {
|
||||||
|
Thread.sleep(200)
|
||||||
|
} catch (_: InterruptedException) {
|
||||||
|
Thread.currentThread().interrupt()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
val errorMessage = lastError?.message
|
||||||
|
if (errorMessage != null) {
|
||||||
|
logger.warn("Postalpincode lookup failed: {}", errorMessage)
|
||||||
|
}
|
||||||
|
return PincodeLookupResult(null, null, "ERROR", "postalpincode.in", errorMessage, url)
|
||||||
|
}
|
||||||
|
|
||||||
private fun parseCityState(body: String): String? {
|
private fun parseCityState(body: String): String? {
|
||||||
val root = objectMapper.readTree(body)
|
val root = objectMapper.readTree(body)
|
||||||
|
|||||||
Reference in New Issue
Block a user