Skip to content

Webhooks

This example showcases the documentation of webhooks.

Webhooks.kt
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package io.github.smiley4.ktoropenapi.examples

import io.github.smiley4.ktoropenapi.OpenApi
import io.github.smiley4.ktoropenapi.openApi
import io.github.smiley4.ktoropenapi.post
import io.github.smiley4.ktoropenapi.webhook
import io.github.smiley4.ktorredoc.redoc
import io.github.smiley4.ktorswaggerui.swaggerUI
import io.ktor.http.ContentType
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.response.respond
import io.ktor.server.routing.route
import io.ktor.server.routing.routing

fun main() {
    embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true)
}

private fun Application.myModule() {

    // Install the "OpenApi"-Plugin
    install(OpenApi)

    routing {

        // add the routes for the api-spec, swagger-ui and redoc
        route("api.json") {
            openApi()
        }
        route("swagger") {
            swaggerUI("/api.json")
        }
        route("redoc") {
            redoc("/api.json")
        }

        // a "normal" documented route to register for notifications
        post("registerForAlert", {
            description = "Register an URL to be called when new concerts are scheduled."
            request {
                body<String> {
                    description = "The URL to be notified about approaching concerts."
                }
            }
        }) {
            call.respond(HttpStatusCode.NotImplemented, Unit)
        }

        // documentation of the webhook to notify
        webhook(HttpMethod.Post, "concertAlert") {
            description = "Notify the registered URL with details of an upcoming concert"
            request {
                body<String> {
                    mediaTypes(ContentType.Text.Plain)
                    required = true
                }
            }
        }

    }

}