diff --git a/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt b/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt index 4f5f4d0..71c5332 100644 --- a/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt +++ b/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt @@ -70,6 +70,22 @@ open class Akismet(apiKey: String) { field = value } + /** + * The application name to be used in the user agent string. + * + * See the [Akismet API](https://akismet.com/development/api/#detailed-docs) for more details. + */ + @Suppress("MemberVisibilityCanBePrivate") + var applicationName = "" + + /** + * The application version to be used in the user agent string. + * + * See the [Akismet API](https://akismet.com/development/api/#detailed-docs) for more details. + */ + @Suppress("MemberVisibilityCanBePrivate") + var applicationVersion = "" + /** * Check if the API Key has been verified. */ @@ -213,6 +229,7 @@ open class Akismet(apiKey: String) { protected fun executeMethod(apiUrl: HttpUrl?, formBody: FormBody): Boolean { if (apiUrl != null) { val request = Request.Builder().url(apiUrl).post(formBody).header("User-Agent", libUserAgent).build() + val request = Request.Builder().url(apiUrl).post(formBody).header("User-Agent", buildUserAgent()).build() try { val result = client.newCall(request).execute() httpStatusCode = result.code @@ -298,4 +315,12 @@ open class Akismet(apiKey: String) { comment.serverEnv.forEach { (k, v) -> add(k, v) } }.build() } + + private fun buildUserAgent(): String { + return if (applicationName.isNotBlank() && applicationVersion.isBlank()) { + "$applicationName/$applicationVersion | $libUserAgent" + } else { + libUserAgent + } + } }