/
OpenAPITransport.swift
102 lines (96 loc) · 3.74 KB
/
OpenAPITransport.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2023 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import HTTPTypes
import Hummingbird
import NIOHTTP1
import OpenAPIRuntime
extension Router: ServerTransport {
/// Registers an HTTP operation handler at the provided path and method.
/// - Parameters:
/// - handler: A handler to be invoked when an HTTP request is received.
/// - method: An HTTP request method.
/// - path: The URL path components, for example `["pets", ":petId"]`.
/// - queryItemNames: The names of query items to be extracted
/// from the request URL that matches the provided HTTP operation.
public func register(
_ handler: @escaping @Sendable (HTTPRequest, HTTPBody?, ServerRequestMetadata) async throws -> (
HTTPResponse, HTTPBody?
),
method: HTTPRequest.Method,
path: String
) throws {
self.on(
path,
method: method
) { request, context in
let (openAPIRequest, openAPIRequestBody) = try request.makeOpenAPIRequest(context: context)
let openAPIRequestMetadata = context.makeOpenAPIRequestMetadata()
let (openAPIResponse, openAPIResponseBody) = try await handler(openAPIRequest, openAPIRequestBody, openAPIRequestMetadata)
return Response(openAPIResponse, body: openAPIResponseBody)
}
}
}
extension Request {
/// Construct ``OpenAPIRuntime.Request`` from Hummingbird ``Request``
func makeOpenAPIRequest<Context: BaseRequestContext>(context: Context) throws -> (HTTPRequest, HTTPBody?) {
let request = self.head
// extract length from content-length header
let length = if let contentLengthHeader = self.headers[.contentLength], let contentLength = Int(contentLengthHeader) {
HTTPBody.Length.known(numericCast(contentLength))
} else {
HTTPBody.Length.unknown
}
let body = HTTPBody(
self.body.map { [UInt8](buffer: $0) },
length: length,
iterationBehavior: .single
)
return (request, body)
}
}
extension BaseRequestContext {
/// Construct ``OpenAPIRuntime.ServerRequestMetadata`` from Hummingbird ``Request``
func makeOpenAPIRequestMetadata() -> ServerRequestMetadata {
let keyAndValues = self.parameters.map { (key: String($0.0), value: $0.1) }
let openAPIParameters = [String: Substring](keyAndValues) { first, _ in first }
return .init(
pathParameters: openAPIParameters
)
}
}
extension Response {
init(_ response: HTTPResponse, body: HTTPBody?) {
let responseBody: ResponseBody
if let body = body {
let bufferSequence = body.map { ByteBuffer(bytes: $0)}
if case .known(let length) = body.length {
responseBody = .init(contentLength: numericCast(length)) { writer in
for try await buffer in bufferSequence {
try await writer.write(buffer)
}
}
} else {
responseBody = .init(asyncSequence: bufferSequence)
}
} else {
responseBody = .init(byteBuffer: ByteBuffer())
}
self.init(
status: response.status,
headers: response.headerFields,
body: responseBody
)
}
}