@@ -5,7 +5,11 @@ import com.google.gson.JsonObject
5
5
import khttp.responses.Response
6
6
import org.awaitility.Durations
7
7
import org.awaitility.core.ConditionTimeoutException
8
- import org.awaitility.kotlin.*
8
+ import org.awaitility.kotlin.atMost
9
+ import org.awaitility.kotlin.await
10
+ import org.awaitility.kotlin.ignoreException
11
+ import org.awaitility.kotlin.until
12
+ import org.awaitility.kotlin.withPollInterval
9
13
import org.gradle.api.DefaultTask
10
14
import org.gradle.api.GradleException
11
15
import org.gradle.api.file.DirectoryProperty
@@ -20,71 +24,84 @@ import java.time.temporal.ChronoUnit.SECONDS
20
24
21
25
22
26
open class OpenApiGeneratorTask : DefaultTask () {
23
- @get:Input
24
- val apiDocsUrl: Property <String > = project.objects.property(String ::class .java)
25
- @get:Input
26
- val outputFileName: Property <String > = project.objects.property(String ::class .java)
27
- @get:Input
28
- val groupedApiMappings: MapProperty <String , String > = project.objects.mapProperty(String ::class .java, String ::class .java)
29
- @get:OutputDirectory
30
- val outputDir: DirectoryProperty = project.objects.directoryProperty()
31
- private val waitTimeInSeconds: Property <Int > = project.objects.property(Int ::class .java)
27
+ @get:Input
28
+ val apiDocsUrl: Property <String > = project.objects.property(String ::class .java)
32
29
33
- init {
34
- description = OPEN_API_TASK_DESCRIPTION
35
- group = GROUP_NAME
36
- // load my extensions
37
- val extension: OpenApiExtension = project.extensions.run {
38
- getByName(EXTENSION_NAME ) as OpenApiExtension
39
- }
30
+ @get:Input
31
+ val outputFileName: Property <String > = project.objects.property(String ::class .java)
40
32
41
- // set a default value if not provided
42
- val defaultOutputDir = project.objects.directoryProperty()
43
- defaultOutputDir.set(project.buildDir )
33
+ @get:Input
34
+ val groupedApiMappings : MapProperty < String , String > =
35
+ project.objects.mapProperty( String :: class .java, String :: class .java )
44
36
45
- apiDocsUrl.set(extension.apiDocsUrl.getOrElse(DEFAULT_API_DOCS_URL ))
46
- outputFileName.set(extension.outputFileName.getOrElse(DEFAULT_OPEN_API_FILE_NAME ))
47
- groupedApiMappings.set(extension.groupedApiMappings.getOrElse(emptyMap()))
48
- outputDir.set(extension.outputDir.getOrElse(defaultOutputDir.get()))
49
- waitTimeInSeconds.set(extension.waitTimeInSeconds.getOrElse(DEFAULT_WAIT_TIME_IN_SECONDS ))
50
- }
37
+ @get:OutputDirectory
38
+ val outputDir: DirectoryProperty = project.objects.directoryProperty()
39
+ private val waitTimeInSeconds: Property <Int > =
40
+ project.objects.property(Int ::class .java)
51
41
52
- @TaskAction
53
- fun execute () {
54
- if (groupedApiMappings.isPresent && groupedApiMappings.get().isNotEmpty()) {
55
- groupedApiMappings.get().forEach(this ::generateApiDocs)
56
- } else {
57
- generateApiDocs(apiDocsUrl.get(), outputFileName.get())
58
- }
59
- }
42
+ init {
43
+ description = OPEN_API_TASK_DESCRIPTION
44
+ group = GROUP_NAME
45
+ // load my extensions
46
+ val extension: OpenApiExtension = project.extensions.run {
47
+ getByName(EXTENSION_NAME ) as OpenApiExtension
48
+ }
60
49
61
- fun generateApiDocs (url : String , fileName : String ) {
62
- try {
63
- await ignoreException ConnectException ::class withPollInterval Durations .ONE_SECOND atMost Duration .of(
64
- waitTimeInSeconds.get().toLong(),
65
- SECONDS
66
- ) until {
67
- val statusCode = khttp.get(url).statusCode
68
- logger.trace(" apiDocsUrl = {} status code = {}" , url, statusCode)
69
- statusCode < 299
70
- }
71
- logger.info(" Generating OpenApi Docs.." )
72
- val response: Response = khttp.get(url)
50
+ // set a default value if not provided
51
+ val defaultOutputDir = project.objects.directoryProperty()
52
+ defaultOutputDir.set(project.buildDir)
73
53
74
- val isYaml = url.toLowerCase().contains(" .yaml" )
75
- val apiDocs = if (isYaml) response.text else prettifyJson(response)
54
+ apiDocsUrl.set(extension.apiDocsUrl.getOrElse(DEFAULT_API_DOCS_URL ))
55
+ outputFileName.set(extension.outputFileName.getOrElse(DEFAULT_OPEN_API_FILE_NAME ))
56
+ groupedApiMappings.set(extension.groupedApiMappings.getOrElse(emptyMap()))
57
+ outputDir.set(extension.outputDir.getOrElse(defaultOutputDir.get()))
58
+ waitTimeInSeconds.set(
59
+ extension.waitTimeInSeconds.getOrElse(
60
+ DEFAULT_WAIT_TIME_IN_SECONDS
61
+ )
62
+ )
63
+ }
76
64
77
- val outputFile = outputDir.file(fileName).get().asFile
78
- outputFile.writeText(apiDocs)
79
- } catch (e: ConditionTimeoutException ) {
80
- this .logger.error(" Unable to connect to ${url} waited for ${waitTimeInSeconds.get()} seconds" , e)
81
- throw GradleException (" Unable to connect to ${url} waited for ${waitTimeInSeconds.get()} seconds" )
82
- }
83
- }
65
+ @TaskAction
66
+ fun execute () {
67
+ if (groupedApiMappings.isPresent && groupedApiMappings.get().isNotEmpty()) {
68
+ groupedApiMappings.get().forEach(this ::generateApiDocs)
69
+ } else {
70
+ generateApiDocs(apiDocsUrl.get(), outputFileName.get())
71
+ }
72
+ }
84
73
85
- private fun prettifyJson (response : Response ): String {
86
- val gson = GsonBuilder ().setPrettyPrinting().create();
87
- val googleJsonObject = gson.fromJson(response.jsonObject.toString(), JsonObject ::class .java)
88
- return gson.toJson(googleJsonObject)
89
- }
74
+ fun generateApiDocs (url : String , fileName : String ) {
75
+ try {
76
+ await ignoreException ConnectException ::class withPollInterval Durations .ONE_SECOND atMost Duration .of(
77
+ waitTimeInSeconds.get().toLong(),
78
+ SECONDS
79
+ ) until {
80
+ val statusCode = khttp.get(url).statusCode
81
+ logger.trace(" apiDocsUrl = {} status code = {}" , url, statusCode)
82
+ statusCode < 299
83
+ }
84
+ logger.info(" Generating OpenApi Docs.." )
85
+ val response: Response = khttp.get(url)
86
+
87
+ val isYaml = url.toLowerCase().contains(" .yaml" )
88
+ val apiDocs = if (isYaml) response.text else prettifyJson(response)
89
+
90
+ val outputFile = outputDir.file(fileName).get().asFile
91
+ outputFile.writeText(apiDocs)
92
+ } catch (e: ConditionTimeoutException ) {
93
+ this .logger.error(
94
+ " Unable to connect to ${url} waited for ${waitTimeInSeconds.get()} seconds" ,
95
+ e
96
+ )
97
+ throw GradleException (" Unable to connect to ${url} waited for ${waitTimeInSeconds.get()} seconds" )
98
+ }
99
+ }
100
+
101
+ private fun prettifyJson (response : Response ): String {
102
+ val gson = GsonBuilder ().setPrettyPrinting().create()
103
+ val googleJsonObject =
104
+ gson.fromJson(response.jsonObject.toString(), JsonObject ::class .java)
105
+ return gson.toJson(googleJsonObject)
106
+ }
90
107
}
0 commit comments