Skip to content

Commit 6fc9712

Browse files
committed
server
1 parent 29aff9a commit 6fc9712

File tree

3 files changed

+359
-359
lines changed

3 files changed

+359
-359
lines changed

samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/PetApi.kt

Lines changed: 176 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,80 @@
1-
/**
2-
* OpenAPI Petstore
3-
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
4-
*
5-
* The version of the OpenAPI document: 1.0.0
6-
*
7-
*
8-
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9-
* https://openapi-generator.tech
10-
* Do not edit the class manually.
11-
*/
12-
package org.openapitools.server.apis
13-
14-
import com.google.gson.Gson
15-
import io.ktor.application.call
16-
import io.ktor.auth.UserIdPrincipal
17-
import io.ktor.auth.authentication
18-
import io.ktor.auth.authenticate
19-
import io.ktor.auth.OAuthAccessTokenResponse
20-
import io.ktor.auth.OAuthServerSettings
21-
import io.ktor.http.ContentType
22-
import io.ktor.http.HttpStatusCode
23-
import io.ktor.locations.KtorExperimentalLocationsAPI
24-
import io.ktor.locations.delete
25-
import io.ktor.locations.get
26-
import io.ktor.response.respond
27-
import io.ktor.response.respondText
28-
import io.ktor.routing.Route
29-
import io.ktor.routing.post
30-
import io.ktor.routing.put
31-
import io.ktor.routing.route
32-
33-
import org.openapitools.server.Paths
34-
import org.openapitools.server.infrastructure.ApiPrincipal
35-
36-
37-
import org.openapitools.server.models.ApiResponse
38-
import org.openapitools.server.models.Pet
39-
40-
@KtorExperimentalLocationsAPI
41-
fun Route.PetApi() {
42-
val gson = Gson()
43-
val empty = mutableMapOf<String, Any?>()
44-
45-
route("/pet") {
46-
authenticate("petstore_auth") {
47-
post {
48-
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
49-
50-
if (principal == null) {
51-
call.respond(HttpStatusCode.Unauthorized)
52-
} else {
53-
call.respond(HttpStatusCode.NotImplemented)
54-
}
55-
}
56-
}
57-
}
58-
59-
60-
delete<Paths.deletePet> { _: Paths.deletePet ->
61-
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
62-
63-
if (principal == null) {
64-
call.respond(HttpStatusCode.Unauthorized)
65-
} else {
66-
call.respond(HttpStatusCode.NotImplemented)
67-
}
68-
}
69-
70-
71-
get<Paths.findPetsByStatus> { _: Paths.findPetsByStatus ->
72-
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
73-
74-
if (principal == null) {
75-
call.respond(HttpStatusCode.Unauthorized)
76-
} else {
77-
val exampleContentType = "application/json"
1+
/**
2+
* OpenAPI Petstore
3+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
4+
*
5+
* The version of the OpenAPI document: 1.0.0
6+
*
7+
*
8+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9+
* https://openapi-generator.tech
10+
* Do not edit the class manually.
11+
*/
12+
package org.openapitools.server.apis
13+
14+
import com.google.gson.Gson
15+
import io.ktor.application.call
16+
import io.ktor.auth.UserIdPrincipal
17+
import io.ktor.auth.authentication
18+
import io.ktor.auth.authenticate
19+
import io.ktor.auth.OAuthAccessTokenResponse
20+
import io.ktor.auth.OAuthServerSettings
21+
import io.ktor.http.ContentType
22+
import io.ktor.http.HttpStatusCode
23+
import io.ktor.locations.KtorExperimentalLocationsAPI
24+
import io.ktor.locations.delete
25+
import io.ktor.locations.get
26+
import io.ktor.response.respond
27+
import io.ktor.response.respondText
28+
import io.ktor.routing.Route
29+
import io.ktor.routing.post
30+
import io.ktor.routing.put
31+
import io.ktor.routing.route
32+
33+
import org.openapitools.server.Paths
34+
import org.openapitools.server.infrastructure.ApiPrincipal
35+
36+
37+
import org.openapitools.server.models.ApiResponse
38+
import org.openapitools.server.models.Pet
39+
40+
@KtorExperimentalLocationsAPI
41+
fun Route.PetApi() {
42+
val gson = Gson()
43+
val empty = mutableMapOf<String, Any?>()
44+
45+
route("/pet") {
46+
authenticate("petstore_auth") {
47+
post {
48+
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
49+
50+
if (principal == null) {
51+
call.respond(HttpStatusCode.Unauthorized)
52+
} else {
53+
call.respond(HttpStatusCode.NotImplemented)
54+
}
55+
}
56+
}
57+
}
58+
59+
60+
delete<Paths.deletePet> { _: Paths.deletePet ->
61+
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
62+
63+
if (principal == null) {
64+
call.respond(HttpStatusCode.Unauthorized)
65+
} else {
66+
call.respond(HttpStatusCode.NotImplemented)
67+
}
68+
}
69+
70+
71+
get<Paths.findPetsByStatus> { _: Paths.findPetsByStatus ->
72+
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
73+
74+
if (principal == null) {
75+
call.respond(HttpStatusCode.Unauthorized)
76+
} else {
77+
val exampleContentType = "application/json"
7878
val exampleContentString = """{
7979
"photoUrls" : [ "photoUrls", "photoUrls" ],
8080
"name" : "doggie",
@@ -91,24 +91,24 @@ fun Route.PetApi() {
9191
"id" : 1
9292
} ],
9393
"status" : "available"
94-
}"""
95-
96-
when(exampleContentType) {
97-
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
98-
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
99-
else -> call.respondText(exampleContentString)
100-
}
101-
}
102-
}
103-
104-
105-
get<Paths.findPetsByTags> { _: Paths.findPetsByTags ->
106-
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
107-
108-
if (principal == null) {
109-
call.respond(HttpStatusCode.Unauthorized)
110-
} else {
111-
val exampleContentType = "application/json"
94+
}"""
95+
96+
when(exampleContentType) {
97+
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
98+
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
99+
else -> call.respondText(exampleContentString)
100+
}
101+
}
102+
}
103+
104+
105+
get<Paths.findPetsByTags> { _: Paths.findPetsByTags ->
106+
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
107+
108+
if (principal == null) {
109+
call.respond(HttpStatusCode.Unauthorized)
110+
} else {
111+
val exampleContentType = "application/json"
112112
val exampleContentString = """{
113113
"photoUrls" : [ "photoUrls", "photoUrls" ],
114114
"name" : "doggie",
@@ -125,24 +125,24 @@ fun Route.PetApi() {
125125
"id" : 1
126126
} ],
127127
"status" : "available"
128-
}"""
129-
130-
when(exampleContentType) {
131-
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
132-
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
133-
else -> call.respondText(exampleContentString)
134-
}
135-
}
136-
}
137-
138-
139-
get<Paths.getPetById> { _: Paths.getPetById ->
140-
val principal = call.authentication.principal<ApiPrincipal>()
141-
142-
if (principal == null) {
143-
call.respond(HttpStatusCode.Unauthorized)
144-
} else {
145-
val exampleContentType = "application/json"
128+
}"""
129+
130+
when(exampleContentType) {
131+
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
132+
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
133+
else -> call.respondText(exampleContentString)
134+
}
135+
}
136+
}
137+
138+
139+
get<Paths.getPetById> { _: Paths.getPetById ->
140+
val principal = call.authentication.principal<ApiPrincipal>()
141+
142+
if (principal == null) {
143+
call.respond(HttpStatusCode.Unauthorized)
144+
} else {
145+
val exampleContentType = "application/json"
146146
val exampleContentString = """{
147147
"photoUrls" : [ "photoUrls", "photoUrls" ],
148148
"name" : "doggie",
@@ -159,70 +159,70 @@ fun Route.PetApi() {
159159
"id" : 1
160160
} ],
161161
"status" : "available"
162-
}"""
163-
164-
when(exampleContentType) {
165-
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
166-
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
167-
else -> call.respondText(exampleContentString)
168-
}
169-
}
170-
}
171-
172-
173-
route("/pet") {
174-
authenticate("petstore_auth") {
175-
put {
176-
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
177-
178-
if (principal == null) {
179-
call.respond(HttpStatusCode.Unauthorized)
180-
} else {
181-
call.respond(HttpStatusCode.NotImplemented)
182-
}
183-
}
184-
}
185-
}
186-
187-
188-
route("/pet/{petId}") {
189-
authenticate("petstore_auth") {
190-
post {
191-
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
192-
193-
if (principal == null) {
194-
call.respond(HttpStatusCode.Unauthorized)
195-
} else {
196-
call.respond(HttpStatusCode.NotImplemented)
197-
}
198-
}
199-
}
200-
}
201-
202-
203-
route("/pet/{petId}/uploadImage") {
204-
authenticate("petstore_auth") {
205-
post {
206-
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
207-
208-
if (principal == null) {
209-
call.respond(HttpStatusCode.Unauthorized)
210-
} else {
211-
val exampleContentType = "application/json"
162+
}"""
163+
164+
when(exampleContentType) {
165+
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
166+
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
167+
else -> call.respondText(exampleContentString)
168+
}
169+
}
170+
}
171+
172+
173+
route("/pet") {
174+
authenticate("petstore_auth") {
175+
put {
176+
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
177+
178+
if (principal == null) {
179+
call.respond(HttpStatusCode.Unauthorized)
180+
} else {
181+
call.respond(HttpStatusCode.NotImplemented)
182+
}
183+
}
184+
}
185+
}
186+
187+
188+
route("/pet/{petId}") {
189+
authenticate("petstore_auth") {
190+
post {
191+
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
192+
193+
if (principal == null) {
194+
call.respond(HttpStatusCode.Unauthorized)
195+
} else {
196+
call.respond(HttpStatusCode.NotImplemented)
197+
}
198+
}
199+
}
200+
}
201+
202+
203+
route("/pet/{petId}/uploadImage") {
204+
authenticate("petstore_auth") {
205+
post {
206+
val principal = call.authentication.principal<OAuthAccessTokenResponse>()
207+
208+
if (principal == null) {
209+
call.respond(HttpStatusCode.Unauthorized)
210+
} else {
211+
val exampleContentType = "application/json"
212212
val exampleContentString = """{
213213
"code" : 0,
214214
"type" : "type",
215215
"message" : "message"
216-
}"""
217-
218-
when(exampleContentType) {
219-
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
220-
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
221-
else -> call.respondText(exampleContentString)
222-
}
223-
}
224-
}
225-
}
226-
}
227-
228-
}
216+
}"""
217+
218+
when(exampleContentType) {
219+
"application/json" -> call.respond(gson.fromJson(exampleContentString, empty::class.java))
220+
"application/xml" -> call.respondText(exampleContentString, ContentType.Text.Xml)
221+
else -> call.respondText(exampleContentString)
222+
}
223+
}
224+
}
225+
}
226+
}
227+
228+
}

0 commit comments

Comments
 (0)