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