Skip to content

Commit 6966cf6

Browse files
zhongnansuzelinh
authored andcommitted
add method type in CustomWebhook data model (#39)
Signed-off-by: Zhongnan Su <szhongna@amazon.com> Signed-off-by: Zelin Hao <zelinhao@amazon.com>
1 parent b6980f2 commit 6966cf6

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.opensearch.commons.notifications.model
2+
3+
import org.opensearch.commons.utils.EnumParser
4+
5+
enum class HttpMethodType(val tag: String) {
6+
POST("POST") {
7+
override fun toString(): String {
8+
return tag
9+
}
10+
},
11+
PUT("PUT") {
12+
override fun toString(): String {
13+
return tag
14+
}
15+
},
16+
PATCH("PATCH") {
17+
override fun toString(): String {
18+
return tag
19+
}
20+
};
21+
22+
companion object {
23+
private val tagMap = values().associateBy { it.tag }
24+
25+
val enumParser = EnumParser { fromTagOrDefault(it) }
26+
27+
/**
28+
* Get HttpMethodType from tag or POST if not found
29+
* @param tag the tag
30+
* @return MethodType corresponding to tag. POST if invalid tag.
31+
*/
32+
fun fromTagOrDefault(tag: String): HttpMethodType {
33+
return tagMap[tag] ?: POST
34+
}
35+
}
36+
}

src/main/kotlin/org/opensearch/commons/notifications/model/Webhook.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import org.opensearch.common.xcontent.XContentBuilder
3535
import org.opensearch.common.xcontent.XContentParser
3636
import org.opensearch.common.xcontent.XContentParserUtils
3737
import org.opensearch.commons.notifications.NotificationConstants.HEADER_PARAMS_TAG
38+
import org.opensearch.commons.notifications.NotificationConstants.METHOD_TAG
3839
import org.opensearch.commons.notifications.NotificationConstants.URL_TAG
3940
import org.opensearch.commons.utils.STRING_READER
4041
import org.opensearch.commons.utils.STRING_WRITER
@@ -47,7 +48,8 @@ import java.io.IOException
4748
*/
4849
data class Webhook(
4950
val url: String,
50-
val headerParams: Map<String, String> = mapOf()
51+
val headerParams: Map<String, String> = mapOf(),
52+
val method: HttpMethodType = HttpMethodType.POST
5153
) : BaseConfigData {
5254

5355
init {
@@ -77,6 +79,7 @@ data class Webhook(
7779
fun parse(parser: XContentParser): Webhook {
7880
var url: String? = null
7981
var headerParams: Map<String, String> = mapOf()
82+
var method = HttpMethodType.POST
8083

8184
XContentParserUtils.ensureExpectedToken(
8285
XContentParser.Token.START_OBJECT,
@@ -89,14 +92,15 @@ data class Webhook(
8992
when (fieldName) {
9093
URL_TAG -> url = parser.text()
9194
HEADER_PARAMS_TAG -> headerParams = parser.mapStrings()
95+
METHOD_TAG -> method = HttpMethodType.fromTagOrDefault(parser.text())
9296
else -> {
9397
parser.skipChildren()
9498
log.info("Unexpected field: $fieldName, while parsing Webhook destination")
9599
}
96100
}
97101
}
98102
url ?: throw IllegalArgumentException("$URL_TAG field absent")
99-
return Webhook(url, headerParams)
103+
return Webhook(url, headerParams, method)
100104
}
101105
}
102106

@@ -108,6 +112,7 @@ data class Webhook(
108112
return builder.startObject()
109113
.field(URL_TAG, url)
110114
.field(HEADER_PARAMS_TAG, headerParams)
115+
.field(METHOD_TAG, method.tag)
111116
.endObject()
112117
}
113118

@@ -117,7 +122,8 @@ data class Webhook(
117122
*/
118123
constructor(input: StreamInput) : this(
119124
url = input.readString(),
120-
headerParams = input.readMap(STRING_READER, STRING_READER)
125+
headerParams = input.readMap(STRING_READER, STRING_READER),
126+
method = input.readEnum(HttpMethodType::class.java)
121127
)
122128

123129
/**
@@ -126,5 +132,6 @@ data class Webhook(
126132
override fun writeTo(output: StreamOutput) {
127133
output.writeString(url)
128134
output.writeMap(headerParams, STRING_WRITER, STRING_WRITER)
135+
output.writeEnum(method)
129136
}
130137
}

src/test/kotlin/org/opensearch/commons/notifications/model/WebhookTests.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,30 @@ internal class WebhookTests {
4646

4747
@Test
4848
fun `Webhook serialize and deserialize using json object should be equal`() {
49-
val sampleWebhook = Webhook("https://domain.com/sample_url#1234567890", mapOf(Pair("key", "value")))
49+
val sampleWebhook = Webhook(
50+
"https://domain.com/sample_url#1234567890",
51+
mapOf(Pair("key", "value")),
52+
HttpMethodType.PUT
53+
)
5054
val jsonString = getJsonString(sampleWebhook)
5155
val recreatedObject = createObjectFromJsonString(jsonString) { Webhook.parse(it) }
5256
assertEquals(sampleWebhook, recreatedObject)
5357
}
5458

5559
@Test
5660
fun `Webhook should deserialize json object using parser`() {
57-
val sampleWebhook = Webhook("https://domain.com/sample_url#1234567890", mapOf(Pair("key", "value")))
61+
val sampleWebhook = Webhook(
62+
"https://domain.com/sample_url#1234567890",
63+
mapOf(Pair("key", "value")),
64+
HttpMethodType.PATCH
65+
)
5866
val jsonString = """
5967
{
6068
"url":"${sampleWebhook.url}",
6169
"header_params":{
6270
"key":"value"
63-
}
71+
},
72+
"method":"PATCH"
6473
}
6574
""".trimIndent()
6675
val recreatedObject = createObjectFromJsonString(jsonString) { Webhook.parse(it) }

0 commit comments

Comments
 (0)