From f3857bae5928140159a2dca8c70db54287d42703 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Fri, 7 Jun 2024 13:57:07 +0900 Subject: [PATCH] fix pdns api clients (retry 17x) --- .../kotlin/space/mori/dnsapi/GsonConfig.kt | 18 ++++++++++++++++++ .../space/mori/dnsapi/PowerDNSAPIClient.kt | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/space/mori/dnsapi/GsonConfig.kt diff --git a/src/main/kotlin/space/mori/dnsapi/GsonConfig.kt b/src/main/kotlin/space/mori/dnsapi/GsonConfig.kt new file mode 100644 index 0000000..a361a1c --- /dev/null +++ b/src/main/kotlin/space/mori/dnsapi/GsonConfig.kt @@ -0,0 +1,18 @@ +package space.mori.dnsapi + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + + +@Configuration +class GsonConfig { + @Bean + fun gson(): Gson { + return GsonBuilder() + .setDateFormat("yyyy-MM-dd HH:mm:ss") + .registerTypeAdapter(PowerDNSAPIError::class.java, PowerDNSAPIErrorDeserializer()) + .create() + } +} \ No newline at end of file diff --git a/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt b/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt index 3ebaba0..9f06041 100644 --- a/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt +++ b/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt @@ -1,6 +1,6 @@ package space.mori.dnsapi -import com.google.gson.Gson +import com.google.gson.* import com.google.gson.annotations.SerializedName import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient @@ -9,6 +9,8 @@ import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response import org.springframework.beans.factory.annotation.Value import org.springframework.stereotype.Service +import java.lang.reflect.Type + @Service class PowerDNSAPIClient() { @@ -132,6 +134,21 @@ data class PowerDNSAPIError( @SerializedName("error") val error: String, @SerializedName("errors") val errors: List ) + +class PowerDNSAPIErrorDeserializer : JsonDeserializer { + @Throws(JsonParseException::class) + override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext?): PowerDNSAPIError { + val jsonObject = json.asJsonObject + val error = jsonObject["error"].asString + val errorsJson = jsonObject["errors"].asJsonArray + val errors: MutableList = ArrayList() + for (element in errorsJson) { + errors.add(element.asString) + } + return PowerDNSAPIError(error, errors) + } +} + class PowerDNSAPIException(private val error: PowerDNSAPIError): RuntimeException(error.error) { val errors: List get() = error.errors