Skip to content

Commit

Permalink
add method type in CustomWebhook data model (#39)
Browse files Browse the repository at this point in the history
Signed-off-by: Zhongnan Su <[email protected]>
  • Loading branch information
zhongnansu authored Jul 16, 2021
1 parent d27d52c commit 62de872
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.opensearch.commons.notifications.model

import org.opensearch.commons.utils.EnumParser

enum class HttpMethodType(val tag: String) {
POST("POST") {
override fun toString(): String {
return tag
}
},
PUT("PUT") {
override fun toString(): String {
return tag
}
},
PATCH("PATCH") {
override fun toString(): String {
return tag
}
};

companion object {
private val tagMap = values().associateBy { it.tag }

val enumParser = EnumParser { fromTagOrDefault(it) }

/**
* Get HttpMethodType from tag or POST if not found
* @param tag the tag
* @return MethodType corresponding to tag. POST if invalid tag.
*/
fun fromTagOrDefault(tag: String): HttpMethodType {
return tagMap[tag] ?: POST
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.opensearch.common.xcontent.XContentBuilder
import org.opensearch.common.xcontent.XContentParser
import org.opensearch.common.xcontent.XContentParserUtils
import org.opensearch.commons.notifications.NotificationConstants.HEADER_PARAMS_TAG
import org.opensearch.commons.notifications.NotificationConstants.METHOD_TAG
import org.opensearch.commons.notifications.NotificationConstants.URL_TAG
import org.opensearch.commons.utils.STRING_READER
import org.opensearch.commons.utils.STRING_WRITER
Expand All @@ -47,7 +48,8 @@ import java.io.IOException
*/
data class Webhook(
val url: String,
val headerParams: Map<String, String> = mapOf()
val headerParams: Map<String, String> = mapOf(),
val method: HttpMethodType = HttpMethodType.POST
) : BaseConfigData {

init {
Expand Down Expand Up @@ -77,6 +79,7 @@ data class Webhook(
fun parse(parser: XContentParser): Webhook {
var url: String? = null
var headerParams: Map<String, String> = mapOf()
var method = HttpMethodType.POST

XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_OBJECT,
Expand All @@ -89,14 +92,15 @@ data class Webhook(
when (fieldName) {
URL_TAG -> url = parser.text()
HEADER_PARAMS_TAG -> headerParams = parser.mapStrings()
METHOD_TAG -> method = HttpMethodType.fromTagOrDefault(parser.text())
else -> {
parser.skipChildren()
log.info("Unexpected field: $fieldName, while parsing Webhook destination")
}
}
}
url ?: throw IllegalArgumentException("$URL_TAG field absent")
return Webhook(url, headerParams)
return Webhook(url, headerParams, method)
}
}

Expand All @@ -108,6 +112,7 @@ data class Webhook(
return builder.startObject()
.field(URL_TAG, url)
.field(HEADER_PARAMS_TAG, headerParams)
.field(METHOD_TAG, method.tag)
.endObject()
}

Expand All @@ -117,7 +122,8 @@ data class Webhook(
*/
constructor(input: StreamInput) : this(
url = input.readString(),
headerParams = input.readMap(STRING_READER, STRING_READER)
headerParams = input.readMap(STRING_READER, STRING_READER),
method = input.readEnum(HttpMethodType::class.java)
)

/**
Expand All @@ -126,5 +132,6 @@ data class Webhook(
override fun writeTo(output: StreamOutput) {
output.writeString(url)
output.writeMap(headerParams, STRING_WRITER, STRING_WRITER)
output.writeEnum(method)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,30 @@ internal class WebhookTests {

@Test
fun `Webhook serialize and deserialize using json object should be equal`() {
val sampleWebhook = Webhook("https://domain.com/sample_url#1234567890", mapOf(Pair("key", "value")))
val sampleWebhook = Webhook(
"https://domain.com/sample_url#1234567890",
mapOf(Pair("key", "value")),
HttpMethodType.PUT
)
val jsonString = getJsonString(sampleWebhook)
val recreatedObject = createObjectFromJsonString(jsonString) { Webhook.parse(it) }
assertEquals(sampleWebhook, recreatedObject)
}

@Test
fun `Webhook should deserialize json object using parser`() {
val sampleWebhook = Webhook("https://domain.com/sample_url#1234567890", mapOf(Pair("key", "value")))
val sampleWebhook = Webhook(
"https://domain.com/sample_url#1234567890",
mapOf(Pair("key", "value")),
HttpMethodType.PATCH
)
val jsonString = """
{
"url":"${sampleWebhook.url}",
"header_params":{
"key":"value"
}
},
"method":"PATCH"
}
""".trimIndent()
val recreatedObject = createObjectFromJsonString(jsonString) { Webhook.parse(it) }
Expand Down

0 comments on commit 62de872

Please sign in to comment.