fix pdns api clients (retry 17x)

This commit is contained in:
dalbodeule
2024-06-07 13:57:07 +09:00
parent bc537299e5
commit f3857bae59
2 changed files with 36 additions and 1 deletions

View File

@@ -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()
}
}

View File

@@ -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<String>
)
class PowerDNSAPIErrorDeserializer : JsonDeserializer<PowerDNSAPIError?> {
@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<String> = 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<String>
get() = error.errors