-
Notifications
You must be signed in to change notification settings - Fork 91
/
ContentTypesServer.swift
168 lines (155 loc) · 7.19 KB
/
ContentTypesServer.swift
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIRuntime
import OpenAPIVapor
import Vapor
struct Handler: APIProtocol {
func getExampleJSON(_ input: Operations.getExampleJSON.Input) async throws -> Operations.getExampleJSON.Output {
let name = input.query.name ?? "Stranger"
print("Greeting a person with the name: \(name)")
return .ok(.init(body: .json(.init(message: "Hello, \(name)!"))))
}
func postExampleJSON(_ input: Operations.postExampleJSON.Input) async throws -> Operations.postExampleJSON.Output {
let requestBody: Components.Schemas.Greeting
switch input.body {
case .json(let json): requestBody = json
}
print("Received a greeting with the message: '\(requestBody.message)'")
return .accepted(.init())
}
func getExamplePlainText(_ input: Operations.getExamplePlainText.Input) async throws
-> Operations.getExamplePlainText.Output
{
.ok(
.init(
body: .plainText(
"""
A snow log.
---
[2023-12-24] It snowed.
[2023-12-25] It snowed even more.
"""
)
)
)
}
func postExamplePlainText(_ input: Operations.postExamplePlainText.Input) async throws
-> Operations.postExamplePlainText.Output
{
let plainText: HTTPBody
switch input.body {
case .plainText(let body): plainText = body
}
let bufferedText = try await String(collecting: plainText, upTo: 1024)
print("Received text: \(bufferedText)")
return .accepted(.init())
}
func getExampleMultipleContentTypes(_ input: Operations.getExampleMultipleContentTypes.Input) async throws
-> Operations.getExampleMultipleContentTypes.Output
{
// The Accept header field lets the client communicate which response content type it prefers, by giving
// each content type a "quality" (in other words, a preference), from 0.0 to 1.0, from least to most preferred.
// However, the server is still in charge of choosing the response content type and uses the Accept header
// as a hint only.
//
// As a server, here we sort the received content types in the Accept header by quality, from most to least
// preferred. If none are provided, default to JSON.
let chosenContentType = input.headers.accept.sortedByQuality().first?.contentType ?? .json
let responseBody: Operations.getExampleMultipleContentTypes.Output.Ok.Body
switch chosenContentType {
case .json, .other: responseBody = .json(.init(message: "Hello, Stranger!"))
case .plainText: responseBody = .plainText("Hello, Stranger!")
}
return .ok(.init(body: responseBody))
}
func postExampleMultipleContentTypes(_ input: Operations.postExampleMultipleContentTypes.Input) async throws
-> Operations.postExampleMultipleContentTypes.Output
{
switch input.body {
case .json(let json): print("Received a JSON greeting with the message: \(json.message)")
case .plainText(let body):
let text = try await String(collecting: body, upTo: 1024)
print("Received a text greeting with the message: \(text)")
}
return .accepted(.init())
}
func postExampleURLEncoded(_ input: Operations.postExampleURLEncoded.Input) async throws
-> Operations.postExampleURLEncoded.Output
{
let requestBody: Operations.postExampleURLEncoded.Input.Body.urlEncodedFormPayload
switch input.body {
case .urlEncodedForm(let form): requestBody = form
}
print("Received a greeting with the message: '\(requestBody.message)'")
return .accepted(.init())
}
func getExampleRawBytes(_ input: Operations.getExampleRawBytes.Input) async throws
-> Operations.getExampleRawBytes.Output
{ .ok(.init(body: .binary([0x73, 0x6e, 0x6f, 0x77, 0x0a]))) }
func postExampleRawBytes(_ input: Operations.postExampleRawBytes.Input) async throws
-> Operations.postExampleRawBytes.Output
{
let binary: HTTPBody
switch input.body {
case .binary(let body): binary = body
}
// Processes each chunk as it comes in, avoids buffering the whole body into memory.
for try await chunk in binary { print("Received chunk: \(chunk)") }
return .accepted(.init())
}
func getExampleMultipart(_ input: Operations.getExampleMultipart.Input) async throws
-> Operations.getExampleMultipart.Output
{
let multipartBody: MultipartBody<Operations.getExampleMultipart.Output.Ok.Body.multipartFormPayload> = [
.greetingTemplate(.init(payload: .init(body: .init(message: "Hello, {name}!")))),
.names(.init(payload: .init(headers: .init(x_hyphen_name_hyphen_locale: "en_US"), body: "Frank"))),
.names(.init(payload: .init(body: "Not Frank"))),
]
return .ok(.init(body: .multipartForm(multipartBody)))
}
func postExampleMultipart(_ input: Operations.postExampleMultipart.Input) async throws
-> Operations.postExampleMultipart.Output
{
let multipartBody: MultipartBody<Operations.postExampleMultipart.Input.Body.multipartFormPayload>
switch input.body {
case .multipartForm(let form): multipartBody = form
}
for try await part in multipartBody {
switch part {
case .greetingTemplate(let template):
let message = template.payload.body.message
print("Received a template message: \(message)")
case .names(let name):
let stringName = try await String(collecting: name.payload.body, upTo: 1024)
// Multipart parts can have headers.
let locale = name.payload.headers.x_hyphen_name_hyphen_locale ?? "<nil>"
print("Received a name: '\(stringName)', header value: '\(locale)'")
case .undocumented(let part):
// Any part with a raw HTTPBody body must have its body consumed before moving on to the next part.
let bytes = try await [UInt8](collecting: part.body, upTo: 1024 * 1024)
print("Received an undocumented part with \(part.headerFields.count) headers and \(bytes.count) bytes.")
}
}
return .accepted(.init())
}
}
@main struct ContentTypesServer {
static func main() async throws {
let app = Vapor.Application()
let transport = VaporTransport(routesBuilder: app)
let handler = Handler()
try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!)
try await app.execute()
}
}