From b9a2910a49532a6bd3c269c56d1c335ffadd0474 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 6 Jan 2020 14:36:39 +0100 Subject: [PATCH] Added Conscrypt as SSL library, Fixed issues with kapt for build+test. Fixed unit test (wrapped in try block) --- build.gradle.kts | 15 ++++--- src/dorkbox/kloudflareApi/Kloudflare.kt | 30 +++++++++---- test/dorkbox/kloudflareApi/KloudflareTest.kt | 44 ++++++++------------ 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2f5d56b..f2e81b4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,20 +11,21 @@ plugins { // publish on sonatype id("de.marcphilipp.nexus-publish") version "0.4.0" // close and release on sonatype - id("io.codearte.nexus-staging") version "0.21.1" + id("io.codearte.nexus-staging") version "0.21.2" id("com.dorkbox.Licensing") version "1.4.2" id("com.dorkbox.VersionUpdate") version "1.6.1" id("com.dorkbox.GradleUtils") version "1.2.8" kotlin("jvm") version "1.3.61" + kotlin("kapt") version "1.3.61" } object Extras { // set for the project const val description = "Cloudflare API v4 for Kotlin" const val group = "com.dorkbox" - const val version = "1.0" + const val version = "1.1" // set as project.ext const val name = "KloudflareAPI" @@ -146,11 +147,10 @@ dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.jetbrains.kotlin:kotlin-stdlib") implementation("org.jetbrains.kotlin:kotlin-stdlib-common") - implementation("org.jetbrains.kotlin:kotlin-reflect") val moshiVer = "1.9.2" - val okHttpVer = "4.2.2" - val retroVer = "2.7.0" + val okHttpVer = "4.3.0" + val retroVer = "2.7.1" implementation("com.squareup.okhttp3:okhttp:$okHttpVer") implementation("com.squareup.okhttp3:logging-interceptor:$okHttpVer") // Log Network Calls @@ -161,8 +161,13 @@ dependencies { // For serialization. THESE ARE NOT TRANSITIVE because it screws up the kotlin version implementation("com.squareup.retrofit2:retrofit:$retroVer") implementation("com.squareup.retrofit2:converter-moshi:$retroVer") + implementation ("com.squareup.moshi:moshi:$moshiVer") implementation ("com.squareup.moshi:moshi-kotlin:$moshiVer") + + // for AUTOMATIC kotlin reflective serialization of json classes + kapt ("com.squareup.moshi:moshi-kotlin-codegen:$moshiVer") + kaptTest ("com.squareup.moshi:moshi-kotlin-codegen:$moshiVer") } /////////////////////////////// diff --git a/src/dorkbox/kloudflareApi/Kloudflare.kt b/src/dorkbox/kloudflareApi/Kloudflare.kt index f24c318..4fecca3 100644 --- a/src/dorkbox/kloudflareApi/Kloudflare.kt +++ b/src/dorkbox/kloudflareApi/Kloudflare.kt @@ -16,6 +16,8 @@ package dorkbox.kloudflareApi import com.squareup.moshi.Moshi +import com.squareup.moshi.Types +import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory import dorkbox.kloudflareApi.api.CloudflareActions import dorkbox.kloudflareApi.api.core.CfErrorResponse import dorkbox.kloudflareApi.api.core.CfResponse @@ -37,16 +39,31 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.ResponseBody import okhttp3.logging.HttpLoggingInterceptor +import org.conscrypt.Conscrypt import retrofit2.Call import retrofit2.Converter import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory import java.io.IOException +import java.security.Security +import java.time.LocalDateTime +import java.util.Collections.* +import kotlin.reflect.full.defaultType class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { companion object { private const val API_BASE_URL = "https://api.cloudflare.com/client/v4/" + + + init { + try { + Security.insertProviderAt(Conscrypt.newProvider(), 1); + } + catch (e: Throwable) { + e.printStackTrace(); + } + } } private val errorConverter: Converter @@ -55,11 +72,12 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { init { // JSON mapping to java classes - val interceptor = HttpLoggingInterceptor() - interceptor.level = HttpLoggingInterceptor.Level.BODY + + // val interceptor = HttpLoggingInterceptor() + // interceptor.level = HttpLoggingInterceptor.Level.BODY client = OkHttpClient.Builder() -// .addInterceptor(interceptor) // this is the raw HTTP interceptor + // .addInterceptor(interceptor) // this is the raw HTTP interceptor .build() val moshi = Moshi.Builder() @@ -67,8 +85,6 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { .add(DnsRecordTypeAdapter()) .build() -// val adapter = moshi.adapter>(Types.newParameterizedType(List::class.java, String::class.java)) - val retrofit = Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(MoshiConverterFactory.create(moshi)) @@ -76,7 +92,6 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { .build() errorConverter = retrofit.responseBodyConverter(CfErrorResponse::class.java, CfErrorResponse::class.annotations.toTypedArray()) - cloudflare = retrofit.create(CloudflareActions::class.java) } @@ -92,6 +107,7 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { return response.body?.string()!! } + @Throws(IOException::class) private fun wrap(call: Call>): T { val response = call.execute() @@ -101,7 +117,7 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { } val errorResponse = errorConverter.convert(response.errorBody()!!) - throw IOException("HTTP call failed: " + errorResponse?.errors?.joinToString { error: Error -> "[${error.code} : ${error.message}]" }) + throw IOException("Call failed: " + errorResponse?.errors?.joinToString { error: Error -> "[${error.code} : ${error.message}]" }) } /** diff --git a/test/dorkbox/kloudflareApi/KloudflareTest.kt b/test/dorkbox/kloudflareApi/KloudflareTest.kt index 3b72d64..fe8f237 100644 --- a/test/dorkbox/kloudflareApi/KloudflareTest.kt +++ b/test/dorkbox/kloudflareApi/KloudflareTest.kt @@ -46,37 +46,29 @@ object KloudflareTest { val kloudflare = Kloudflare(email, token) - println(kloudflare.getUserDetails()) - println(kloudflare.getUserBillingProfile()) - println(kloudflare.getUserBillingHistory()) + try { + + println(kloudflare.getUserDetails()) +// println(kloudflare.getUserBillingProfile()) +// println(kloudflare.getUserBillingHistory()) - val zones = kloudflare.listZones() + val zones = kloudflare.listZones() + zones.forEach { + println(it) + } -// println(kloudflare.getZoneRatePlans("123")) -// println(kloudflare.getZoneRatePlans("123")) -// println(kloudflare.getZoneSettings("123")) -// println(kloudflare.listDnsRecords("123")) -// println(kloudflare.listAccessRules()) + // println(kloudflare.getZoneRatePlans("123")) + // println(kloudflare.getZoneRatePlans("123")) + // println(kloudflare.getZoneSettings("123")) + // println(kloudflare.listDnsRecords("123")) + println(kloudflare.listAccessRules()) -// val history = cloudflare.getUserBillingHistory(email, token).execute() -// println("user: ${history.body()?.result}") -// -// val zones = cloudflare.listZones(email, token).execute().body()?.result -// println("user: ${zones.body()?.result}") - -// val zoneRatePlans = cloudflare.getZoneRatePlans(email, token, "123").execute() -// println("user: ${zoneRatePlans.body()?.result}") -// -// val zoneSettings = cloudflare.getZoneSettings(email, token, "123").execute() -// println("user: ${zoneSettings.body()?.result}") -// -// val dnsRecords = cloudflare.listDnsRecords(email, token, "123").execute() -// println("user: ${dnsRecords.body()?.result}") - - - kloudflare.shutdown() + } + finally { + kloudflare.shutdown() + } } }