Code cleanup. Added javadoc, added http.get() method (to make some http calls easier)

This commit is contained in:
nathan 2019-07-28 17:00:09 +02:00
parent 400e553a8f
commit 18ce99158b

View File

@ -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,11 +47,11 @@ 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<ResponseBody, CfErrorResponse>
val cloudflare: CloudflareActions
val client: OkHttpClient
private val errorConverter: Converter<ResponseBody, CfErrorResponse>
private val cloudflare: CloudflareActions
private val client: OkHttpClient
init {
// JSON mapping to java classes
@ -58,7 +59,7 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
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()
@ -68,7 +69,6 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
// val adapter = moshi.adapter<List<String>>(Types.newParameterizedType(List::class.java, String::class.java))
val retrofit = Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(MoshiConverterFactory.create(moshi))
@ -80,7 +80,19 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
cloudflare = retrofit.create(CloudflareActions::class.java)
}
fun <T> wrap(call: Call<CfResponse<T>>): T {
/**
* @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 <T> wrap(call: Call<CfResponse<T>>): T {
val response = call.execute()
val body = response.body()
@ -91,21 +103,39 @@ 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}]" })
}
}
/**
* 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<String, String> = emptyMap()): List<Zone> {
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<DnsRecord> {
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<AccessRule> {
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()