add more stuff

This commit is contained in:
gowthaman.b
2024-01-05 12:08:27 +05:30
parent 82fb57bd85
commit d506078804
7 changed files with 306 additions and 22 deletions

View File

@@ -0,0 +1,190 @@
package com.restapi.integ
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.restapi.config.AppConfig
import io.minio.MinioClient
import io.minio.UploadObjectArgs
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.utils.URIBuilder
import org.apache.http.impl.client.HttpClients
import org.apache.http.message.BasicNameValuePair
import org.apache.http.util.EntityUtils
import org.slf4j.LoggerFactory
import java.io.File
val logger = LoggerFactory.getLogger("ExternalAPI")
object S3 {
private val s3Url = AppConfig.appConfig.s3Url().orElse("s3.amazonaws.com")
private val minioClient: MinioClient by lazy {
MinioClient.builder()
.endpoint("https://$s3Url")
.credentials(
AppConfig.appConfig.s3AccessKey().orElseThrow(),
AppConfig.appConfig.s3SecretKey().orElseThrow()
).build()
}
fun uploadFilesToS3(f: File, name: String): Result<String> {
val objStore: String = AppConfig.appConfig.s3Bucket().orElseThrow()
logger.warn("Trying to upload Object ${f.absolutePath} -> $objStore")
try {
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(objStore)
.`object`(name)
.filename(f.absolutePath)
.build()
)
return Result.success("https://${objStore}.${s3Url}/$name")
} catch (e: Exception) {
logger.warn("Error in file Upload ${f.absolutePath} ==> ${e.message} => Objstore[${objStore}]")
return Result.failure(e)
}
}
}
data class LoqateFullAddrResponse(
@JsonProperty("Items")
val items: List<Item>
) {
data class Item(
@JsonProperty("AdminAreaCode")
val adminAreaCode: String,
@JsonProperty("AdminAreaName")
val adminAreaName: String,
@JsonProperty("Barcode")
val barcode: String,
@JsonProperty("Block")
val block: String,
@JsonProperty("BuildingName")
val buildingName: String,
@JsonProperty("BuildingNumber")
val buildingNumber: String,
@JsonProperty("City")
val city: String,
@JsonProperty("Company")
val company: String,
@JsonProperty("CountryIso2")
val countryIso2: String,
@JsonProperty("CountryIso3")
val countryIso3: String,
@JsonProperty("CountryIsoNumber")
val countryIsoNumber: String,
@JsonProperty("CountryName")
val countryName: String,
@JsonProperty("DataLevel")
val dataLevel: String,
@JsonProperty("Department")
val department: String,
@JsonProperty("District")
val district: String,
@JsonProperty("DomesticId")
val domesticId: String,
@JsonProperty("Field1")
val field1: String,
@JsonProperty("Field10")
val field10: String,
@JsonProperty("Field11")
val field11: String,
@JsonProperty("Field12")
val field12: String,
@JsonProperty("Field13")
val field13: String,
@JsonProperty("Field14")
val field14: String,
@JsonProperty("Field15")
val field15: String,
@JsonProperty("Field16")
val field16: String,
@JsonProperty("Field17")
val field17: String,
@JsonProperty("Field18")
val field18: String,
@JsonProperty("Field19")
val field19: String,
@JsonProperty("Field2")
val field2: String,
@JsonProperty("Field20")
val field20: String,
@JsonProperty("Field3")
val field3: String,
@JsonProperty("Field4")
val field4: String,
@JsonProperty("Field5")
val field5: String,
@JsonProperty("Field6")
val field6: String,
@JsonProperty("Field7")
val field7: String,
@JsonProperty("Field8")
val field8: String,
@JsonProperty("Field9")
val field9: String,
@JsonProperty("Id")
val id: String,
@JsonProperty("Label")
val label: String,
@JsonProperty("Language")
val language: String,
@JsonProperty("LanguageAlternatives")
val languageAlternatives: String,
@JsonProperty("Line1")
val line1: String,
@JsonProperty("Line2")
val line2: String,
@JsonProperty("Line3")
val line3: String,
@JsonProperty("Line4")
val line4: String,
@JsonProperty("Line5")
val line5: String,
@JsonProperty("Neighbourhood")
val neighbourhood: String,
@JsonProperty("POBoxNumber")
val pOBoxNumber: String,
@JsonProperty("PostalCode")
val postalCode: String,
@JsonProperty("Province")
val province: String,
@JsonProperty("ProvinceCode")
val provinceCode: String,
@JsonProperty("ProvinceName")
val provinceName: String,
@JsonProperty("SecondaryStreet")
val secondaryStreet: String,
@JsonProperty("SortingNumber1")
val sortingNumber1: String,
@JsonProperty("SortingNumber2")
val sortingNumber2: String,
@JsonProperty("Street")
val street: String,
@JsonProperty("SubBuilding")
val subBuilding: String,
@JsonProperty("Type")
val type: String
)
}
object LoqateAPI {
//todo: add search?
fun findFullAddr(id: String): LoqateFullAddrResponse {
//https://api.addressy.com/Capture/Interactive/Retrieve/v1.2/
return HttpClients.createDefault().use { h ->
val key = AppConfig.appConfig.locateApiKey().orElseThrow()
val ub = URIBuilder("https://api.addressy.com/Capture/Interactive/Retrieve/v1.2/json3.ws")
.setParameters(
BasicNameValuePair("Key", key),
BasicNameValuePair("Id", id)
).build()
h.execute(HttpGet(ub)).use {
jacksonObjectMapper().readValue(EntityUtils.toString(it.entity))
}
}
}
}