diff --git a/src/dorkbox/kloudflareApi/Kloudflare.kt b/src/dorkbox/kloudflareApi/Kloudflare.kt index 3e963ab..f24c318 100644 --- a/src/dorkbox/kloudflareApi/Kloudflare.kt +++ b/src/dorkbox/kloudflareApi/Kloudflare.kt @@ -34,6 +34,7 @@ import dorkbox.kloudflareApi.api.zone.RatePlan import dorkbox.kloudflareApi.api.zone.Zone import dorkbox.kloudflareApi.api.zone.settings.ZoneSetting import okhttp3.OkHttpClient +import okhttp3.Request import okhttp3.ResponseBody import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Call @@ -46,66 +47,95 @@ import java.io.IOException class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { companion object { private const val API_BASE_URL = "https://api.cloudflare.com/client/v4/" + } - val errorConverter: Converter - val cloudflare: CloudflareActions + private val errorConverter: Converter + private val cloudflare: CloudflareActions + private val client: OkHttpClient - val client: OkHttpClient + init { + // JSON mapping to java classes + val interceptor = HttpLoggingInterceptor() + interceptor.level = HttpLoggingInterceptor.Level.BODY - init { - // JSON mapping to java classes - val interceptor = HttpLoggingInterceptor() - interceptor.level = HttpLoggingInterceptor.Level.BODY + client = OkHttpClient.Builder() +// .addInterceptor(interceptor) // this is the raw HTTP interceptor + .build() - client = OkHttpClient.Builder() - // .addInterceptor(interceptor) // this is the raw HTTP interceptor - .build() - - val moshi = Moshi.Builder() - .add(ISO8601Adapter()) - .add(DnsRecordTypeAdapter()) - .build() + val moshi = Moshi.Builder() + .add(ISO8601Adapter()) + .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)) + .client(client) + .build() - val retrofit = Retrofit.Builder() - .baseUrl(API_BASE_URL) - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .client(client) - .build() + errorConverter = retrofit.responseBodyConverter(CfErrorResponse::class.java, CfErrorResponse::class.annotations.toTypedArray()) - errorConverter = retrofit.responseBodyConverter(CfErrorResponse::class.java, CfErrorResponse::class.annotations.toTypedArray()) - - cloudflare = retrofit.create(CloudflareActions::class.java) - } - - fun wrap(call: Call>): T { - val response = call.execute() - - val body = response.body() - if (response.isSuccessful && body != null && body.success) { - return body.result!! - } - - val errorResponse = errorConverter.convert(response.errorBody()!!) - throw IOException("HTTP call failed: " + errorResponse?.errors?.joinToString { error: Error -> "[${error.code} : ${error.message}]" }) - } + cloudflare = retrofit.create(CloudflareActions::class.java) } + /** + * @return the content of an http get to the requested URL + */ + fun get(url: String): String { + val request = Request.Builder() + .url(url) + .build() + val response = client.newCall(request).execute() + return response.body?.string()!! + } + + private fun wrap(call: Call>): T { + val response = call.execute() + + val body = response.body() + if (response.isSuccessful && body != null && body.success) { + return body.result!! + } + + val errorResponse = errorConverter.convert(response.errorBody()!!) + throw IOException("HTTP call failed: " + errorResponse?.errors?.joinToString { error: Error -> "[${error.code} : ${error.message}]" }) + } + + /** + * Gets the User details + * + * https://api.cloudflare.com/#user-properties + */ fun getUserDetails(): User { return wrap(cloudflare.getUserDetails(xAuthEmail, xAuthKey)) } + /** + * Gets the user's Billing Profile + * + * https://api.cloudflare.com/#user-billing-profile-billing-profile + */ fun getUserBillingProfile(): BillingProfile { return wrap(cloudflare.getUserBillingProfile(xAuthEmail, xAuthKey)) } + /** + * Gets the users Billing History + * + * https://api.cloudflare.com/#user-billing-history-billing-history + */ fun getUserBillingHistory(): BillingHistory { return wrap(cloudflare.getUserBillingHistory(xAuthEmail, xAuthKey)) } + /** + * Gets the list of Zone's owned by this user + * + * https://api.cloudflare.com/#zone-properties + */ fun listZones(options: Map = emptyMap()): List { val zones = wrap(cloudflare.listZones(xAuthEmail, xAuthKey, options)) zones.forEach { zone -> @@ -117,14 +147,29 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { return zones } + /** + * Gets the zone rate plan for the specified zone from the billing service + * + * https://api.cloudflare.com/#zone-rate-plan-properties + */ fun getZoneRatePlans(zone: Zone): RatePlan { return wrap(cloudflare.getZoneRatePlans(xAuthEmail, xAuthKey, zone.id)) } + /** + * Gets the zone settings for the specified zone + * + * https://api.cloudflare.com/#zone-settings-properties + */ fun getZoneSettings(zone: Zone): ZoneSetting { return wrap(cloudflare.getZoneSettings(xAuthEmail, xAuthKey, zone.id)) } + /** + * Lists the DNS records for a specified zone + * + * https://api.cloudflare.com/#dns-records-for-a-zone-properties + */ fun listDnsRecords(zone: Zone): List { val wrap = wrap(cloudflare.listDnsRecords(xAuthEmail, xAuthKey, zone.id)) @@ -134,6 +179,11 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { return wrap } + /** + * Creates a new DNS record in the specified zone + * + * https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record + */ fun createDnsRecord(dnsRecord: CreateDnsRecord): DnsRecord { val wrap = wrap(cloudflare.createDnsRecord(xAuthEmail, xAuthKey, dnsRecord.zone.id, dnsRecord)) @@ -141,6 +191,11 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { return wrap } + /** + * Updates a DNS record for the specified zone + dns record + * + * https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record + */ fun updateDnsRecord(updatedDnsRecord: UpdateDnsRecord): Any { return wrap(cloudflare.updateDnsRecord(xAuthEmail, xAuthKey, @@ -151,14 +206,27 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { ) } + /** + * Deletes a DNS record for the specified zone + dns record + * + * https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record + */ fun deleteDnsRecord(dnsRecord: DnsRecord): DeleteDnsRecord { return wrap(cloudflare.deleteDnsRecord(xAuthEmail, xAuthKey, dnsRecord.zone.id, dnsRecord.id)) } + /** + * Lists the access rules for the firewall. + * + * https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record + */ fun listAccessRules(): List { return wrap(cloudflare.listAccessRules(xAuthEmail, xAuthKey)) } + /** + * Shuts down the HTTP executors used. This is necessary if you want to be able to shutdown the JVM + */ fun shutdown() { // shutdown the http client stuff client.dispatcher.cancelAll()