Code cleanup. Added javadoc, added http.get() method (to make some http calls easier)
This commit is contained in:
parent
400e553a8f
commit
18ce99158b
@ -34,6 +34,7 @@ import dorkbox.kloudflareApi.api.zone.RatePlan
|
|||||||
import dorkbox.kloudflareApi.api.zone.Zone
|
import dorkbox.kloudflareApi.api.zone.Zone
|
||||||
import dorkbox.kloudflareApi.api.zone.settings.ZoneSetting
|
import dorkbox.kloudflareApi.api.zone.settings.ZoneSetting
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
import okhttp3.ResponseBody
|
import okhttp3.ResponseBody
|
||||||
import okhttp3.logging.HttpLoggingInterceptor
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
import retrofit2.Call
|
import retrofit2.Call
|
||||||
@ -46,11 +47,11 @@ import java.io.IOException
|
|||||||
class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
|
class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
|
||||||
companion object {
|
companion object {
|
||||||
private const val API_BASE_URL = "https://api.cloudflare.com/client/v4/"
|
private const val API_BASE_URL = "https://api.cloudflare.com/client/v4/"
|
||||||
|
}
|
||||||
|
|
||||||
val errorConverter: Converter<ResponseBody, CfErrorResponse>
|
private val errorConverter: Converter<ResponseBody, CfErrorResponse>
|
||||||
val cloudflare: CloudflareActions
|
private val cloudflare: CloudflareActions
|
||||||
|
private val client: OkHttpClient
|
||||||
val client: OkHttpClient
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
// JSON mapping to java classes
|
// JSON mapping to java classes
|
||||||
@ -58,7 +59,7 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
|
|||||||
interceptor.level = HttpLoggingInterceptor.Level.BODY
|
interceptor.level = HttpLoggingInterceptor.Level.BODY
|
||||||
|
|
||||||
client = OkHttpClient.Builder()
|
client = OkHttpClient.Builder()
|
||||||
// .addInterceptor(interceptor) // this is the raw HTTP interceptor
|
// .addInterceptor(interceptor) // this is the raw HTTP interceptor
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
val moshi = Moshi.Builder()
|
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 adapter = moshi.adapter<List<String>>(Types.newParameterizedType(List::class.java, String::class.java))
|
||||||
|
|
||||||
|
|
||||||
val retrofit = Retrofit.Builder()
|
val retrofit = Retrofit.Builder()
|
||||||
.baseUrl(API_BASE_URL)
|
.baseUrl(API_BASE_URL)
|
||||||
.addConverterFactory(MoshiConverterFactory.create(moshi))
|
.addConverterFactory(MoshiConverterFactory.create(moshi))
|
||||||
@ -80,7 +80,19 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
|
|||||||
cloudflare = retrofit.create(CloudflareActions::class.java)
|
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 response = call.execute()
|
||||||
|
|
||||||
val body = response.body()
|
val body = response.body()
|
||||||
@ -91,21 +103,39 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
|
|||||||
val errorResponse = errorConverter.convert(response.errorBody()!!)
|
val errorResponse = errorConverter.convert(response.errorBody()!!)
|
||||||
throw IOException("HTTP call failed: " + errorResponse?.errors?.joinToString { error: Error -> "[${error.code} : ${error.message}]" })
|
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 {
|
fun getUserDetails(): User {
|
||||||
return wrap(cloudflare.getUserDetails(xAuthEmail, xAuthKey))
|
return wrap(cloudflare.getUserDetails(xAuthEmail, xAuthKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the user's Billing Profile
|
||||||
|
*
|
||||||
|
* https://api.cloudflare.com/#user-billing-profile-billing-profile
|
||||||
|
*/
|
||||||
fun getUserBillingProfile(): BillingProfile {
|
fun getUserBillingProfile(): BillingProfile {
|
||||||
return wrap(cloudflare.getUserBillingProfile(xAuthEmail, xAuthKey))
|
return wrap(cloudflare.getUserBillingProfile(xAuthEmail, xAuthKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the users Billing History
|
||||||
|
*
|
||||||
|
* https://api.cloudflare.com/#user-billing-history-billing-history
|
||||||
|
*/
|
||||||
fun getUserBillingHistory(): BillingHistory {
|
fun getUserBillingHistory(): BillingHistory {
|
||||||
return wrap(cloudflare.getUserBillingHistory(xAuthEmail, xAuthKey))
|
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> {
|
fun listZones(options: Map<String, String> = emptyMap()): List<Zone> {
|
||||||
val zones = wrap(cloudflare.listZones(xAuthEmail, xAuthKey, options))
|
val zones = wrap(cloudflare.listZones(xAuthEmail, xAuthKey, options))
|
||||||
zones.forEach { zone ->
|
zones.forEach { zone ->
|
||||||
@ -117,14 +147,29 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
|
|||||||
return zones
|
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 {
|
fun getZoneRatePlans(zone: Zone): RatePlan {
|
||||||
return wrap(cloudflare.getZoneRatePlans(xAuthEmail, xAuthKey, zone.id))
|
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 {
|
fun getZoneSettings(zone: Zone): ZoneSetting {
|
||||||
return wrap(cloudflare.getZoneSettings(xAuthEmail, xAuthKey, zone.id))
|
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> {
|
fun listDnsRecords(zone: Zone): List<DnsRecord> {
|
||||||
val wrap =
|
val wrap =
|
||||||
wrap(cloudflare.listDnsRecords(xAuthEmail, xAuthKey, zone.id))
|
wrap(cloudflare.listDnsRecords(xAuthEmail, xAuthKey, zone.id))
|
||||||
@ -134,6 +179,11 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
|
|||||||
return wrap
|
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 {
|
fun createDnsRecord(dnsRecord: CreateDnsRecord): DnsRecord {
|
||||||
val wrap =
|
val wrap =
|
||||||
wrap(cloudflare.createDnsRecord(xAuthEmail, xAuthKey, dnsRecord.zone.id, dnsRecord))
|
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
|
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 {
|
fun updateDnsRecord(updatedDnsRecord: UpdateDnsRecord): Any {
|
||||||
return wrap(cloudflare.updateDnsRecord(xAuthEmail,
|
return wrap(cloudflare.updateDnsRecord(xAuthEmail,
|
||||||
xAuthKey,
|
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 {
|
fun deleteDnsRecord(dnsRecord: DnsRecord): DeleteDnsRecord {
|
||||||
return wrap(cloudflare.deleteDnsRecord(xAuthEmail, xAuthKey, dnsRecord.zone.id, dnsRecord.id))
|
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> {
|
fun listAccessRules(): List<AccessRule> {
|
||||||
return wrap(cloudflare.listAccessRules(xAuthEmail, xAuthKey))
|
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() {
|
fun shutdown() {
|
||||||
// shutdown the http client stuff
|
// shutdown the http client stuff
|
||||||
client.dispatcher.cancelAll()
|
client.dispatcher.cancelAll()
|
||||||
|
Loading…
Reference in New Issue
Block a user