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 {
|
||||
if (apiKey.isBlank()) return PincodeLookupResult(null, null, "NO_API_KEY", "data.gov.in", "Missing API key")
|
||||
return try {
|
||||
val first = 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
|
||||
fetch(pinCode)
|
||||
} catch (ex: Exception) {
|
||||
logger.warn("Data.gov.in lookup failed: {}", 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.ObjectMapper
|
||||
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.stereotype.Component
|
||||
import org.springframework.web.client.RestTemplate
|
||||
@@ -34,16 +37,34 @@ class PostalPincodeClient(
|
||||
.path("/pincode/{pin}")
|
||||
.buildAndExpand(pinCode)
|
||||
.toUriString()
|
||||
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)
|
||||
val headers = HttpHeaders().apply {
|
||||
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 resolved = parseCityState(body)
|
||||
val status = if (resolved == null) "ZERO_RESULTS" else "OK"
|
||||
return PincodeLookupResult(resolved, body, status, "postalpincode.in", requestUrl = url)
|
||||
} catch (ex: Exception) {
|
||||
lastError = ex
|
||||
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? {
|
||||
|
||||
Reference in New Issue
Block a user