Skip to content

[Runtime] Async bodies + swift-http-types adoption #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 60 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
fd47bd2
[WIP] Async body currency type
czechboy0 Sep 1, 2023
beca630
Rename Body to HTTPBody to avoid confusion with the generated Body enums
czechboy0 Sep 4, 2023
34f7c59
[WIP] Adopt swift-http-types
czechboy0 Sep 4, 2023
d046bf9
WIP
czechboy0 Sep 4, 2023
164f84e
wip
czechboy0 Sep 5, 2023
3db782e
Remove mapChunks, rename initializer parameters
czechboy0 Sep 5, 2023
6c97d48
wip
czechboy0 Sep 5, 2023
466b701
Remove String coder, fix up client unit tests
czechboy0 Sep 5, 2023
00ec189
Got the other tests working
czechboy0 Sep 5, 2023
fe08164
Just use Substring in more places
czechboy0 Sep 5, 2023
63f05d6
wip
czechboy0 Sep 6, 2023
982fe13
wip
czechboy0 Sep 6, 2023
1317631
Added more docs
czechboy0 Sep 6, 2023
faba5ce
Add back docs
czechboy0 Sep 6, 2023
6a71613
Improve loggability of the currency types
czechboy0 Sep 7, 2023
1c07795
Bring back comments on ServerError
czechboy0 Sep 7, 2023
2540b77
WIP
czechboy0 Sep 7, 2023
a165558
Add comments back
czechboy0 Sep 7, 2023
c4186f8
Prefix extensions on types we don't own
czechboy0 Sep 7, 2023
5e1d21e
WIP on documentation
czechboy0 Sep 7, 2023
0ee065f
Add docs for HTTPBody
czechboy0 Sep 7, 2023
99d8252
Add doc comments
czechboy0 Sep 7, 2023
d85eb55
More docs
czechboy0 Sep 7, 2023
844be3a
Merge branch 'main' into hd-adopt-http-types
czechboy0 Sep 7, 2023
1406537
Fixes
czechboy0 Sep 7, 2023
84361a8
Make iterator next() method mutating
czechboy0 Sep 8, 2023
9f10325
Represent no responses as a nil HTTPBody in server transport and midd…
czechboy0 Sep 8, 2023
dbda450
Feedback: make the AsyncSequences Sendable, which adds the requiremen…
czechboy0 Sep 12, 2023
fbe8eea
Feedback: further cleanup of the HTTPBody API
czechboy0 Sep 12, 2023
8b432b2
Remove unnecessary initializers
czechboy0 Sep 12, 2023
3f59f44
Review feedback:
czechboy0 Sep 13, 2023
16917ce
Review feedback: make response body optional
czechboy0 Sep 13, 2023
4830821
A few more changes
czechboy0 Sep 13, 2023
213addf
Wording and a refactor fix
czechboy0 Sep 14, 2023
a5e22b6
Merge branch 'main' into hd-adopt-http-types
czechboy0 Sep 18, 2023
c314914
Fix a few more missing sendable annotations
czechboy0 Sep 18, 2023
0cf76f2
Update docs
czechboy0 Sep 18, 2023
af10f7d
Update HTTPBody.swift
czechboy0 Sep 18, 2023
b2fb79e
Update HTTPBody.swift
czechboy0 Sep 18, 2023
1778209
Update HTTPBody.swift
czechboy0 Sep 18, 2023
b58a3b6
Update HTTPBody.swift
czechboy0 Sep 18, 2023
b2d4ec3
Update HTTPBody.swift
czechboy0 Sep 18, 2023
66b602f
Update HTTPBody.swift
czechboy0 Sep 18, 2023
b0ccaae
Update ServerTransport.swift
czechboy0 Sep 18, 2023
3f5b3d3
Merge remote-tracking branch 'apple/main' into hd-adopt-http-types
czechboy0 Sep 25, 2023
a129b6b
Fixing up the merge from main
czechboy0 Sep 25, 2023
a3516b7
Merge branch 'hd-adopt-http-types' of github.com:czechboy0/swift-open…
czechboy0 Sep 25, 2023
f5d8c1c
Merge branch 'main' into hd-adopt-http-types
czechboy0 Sep 25, 2023
a89bced
Use swift-http-types 1.0
czechboy0 Sep 25, 2023
b19a787
Provide a closure for accessing the locked value HTTPBody.iteratorCre…
czechboy0 Sep 25, 2023
481e6bb
Fix up encoding comment
czechboy0 Sep 25, 2023
dc209d3
Update Sources/OpenAPIRuntime/Interface/HTTPBody.swift
czechboy0 Sep 25, 2023
05082f9
Update Sources/OpenAPIRuntime/Conversion/CurrencyExtensions.swift
czechboy0 Sep 25, 2023
03a9f2d
Update Tests/OpenAPIRuntimeTests/Conversion/Test_Converter+Common.swift
czechboy0 Sep 26, 2023
ed46fa5
Explicitly test substring support
czechboy0 Sep 26, 2023
669a7f3
Add a test case with an unknown length
czechboy0 Sep 26, 2023
72d241d
PR feedback: improve the iteration behavior checking in HTTPBody, ren…
czechboy0 Sep 26, 2023
0704673
Added a test for the collect method based on a known length
czechboy0 Sep 26, 2023
492cf0c
Move extensions of http types into the generated SPI
czechboy0 Sep 26, 2023
24f8870
Update version in docs to 0.3.0
czechboy0 Sep 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"UseLetInEveryBoundCaseVariable" : false,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : false,
"UseSynthesizedInitializer" : true,
"UseSynthesizedInitializer" : false,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : false
Expand Down
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ let package = Package(
.library(
name: "OpenAPIRuntime",
targets: ["OpenAPIRuntime"]
),
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
.target(
name: "OpenAPIRuntime",
dependencies: [],
dependencies: [
.product(name: "HTTPTypes", package: "swift-http-types")
],
swiftSettings: swiftSettings
),
.testTarget(
Expand Down
108 changes: 34 additions & 74 deletions Sources/OpenAPIRuntime/Conversion/Converter+Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import HTTPTypes

extension Converter {

Expand All @@ -20,15 +21,10 @@ extension Converter {
/// - headerFields: The header fields where to add the "accept" header.
/// - contentTypes: The array of acceptable content types by the client.
public func setAcceptHeader<T: AcceptableProtocol>(
in headerFields: inout [HeaderField],
in headerFields: inout HTTPFields,
contentTypes: [AcceptHeaderContentType<T>]
) {
headerFields.append(
.init(
name: "accept",
value: contentTypes.map(\.rawValue).joined(separator: ", ")
)
)
headerFields[.accept] = contentTypes.map(\.rawValue).joined(separator: ", ")
}

// | client | set | request path | URI | required | renderedPath |
Expand Down Expand Up @@ -60,7 +56,7 @@ extension Converter {

// | client | set | request query | URI | both | setQueryItemAsURI |
public func setQueryItemAsURI<T: Encodable>(
in request: inout Request,
in request: inout HTTPRequest,
style: ParameterStyle?,
explode: Bool?,
name: String,
Expand All @@ -84,40 +80,12 @@ extension Converter {
)
}

// | client | set | request body | string | optional | setOptionalRequestBodyAsString |
public func setOptionalRequestBodyAsString<T: Encodable>(
_ value: T?,
headerFields: inout [HeaderField],
contentType: String
) throws -> Data? {
try setOptionalRequestBody(
value,
headerFields: &headerFields,
contentType: contentType,
convert: convertToStringData
)
}

// | client | set | request body | string | required | setRequiredRequestBodyAsString |
public func setRequiredRequestBodyAsString<T: Encodable>(
_ value: T,
headerFields: inout [HeaderField],
contentType: String
) throws -> Data {
try setRequiredRequestBody(
value,
headerFields: &headerFields,
contentType: contentType,
convert: convertToStringData
)
}

// | client | set | request body | JSON | optional | setOptionalRequestBodyAsJSON |
public func setOptionalRequestBodyAsJSON<T: Encodable>(
_ value: T?,
headerFields: inout [HeaderField],
headerFields: inout HTTPFields,
contentType: String
) throws -> Data? {
) throws -> HTTPBody? {
try setOptionalRequestBody(
value,
headerFields: &headerFields,
Expand All @@ -129,9 +97,9 @@ extension Converter {
// | client | set | request body | JSON | required | setRequiredRequestBodyAsJSON |
public func setRequiredRequestBodyAsJSON<T: Encodable>(
_ value: T,
headerFields: inout [HeaderField],
headerFields: inout HTTPFields,
contentType: String
) throws -> Data {
) throws -> HTTPBody {
try setRequiredRequestBody(
value,
headerFields: &headerFields,
Expand All @@ -142,38 +110,38 @@ extension Converter {

// | client | set | request body | binary | optional | setOptionalRequestBodyAsBinary |
public func setOptionalRequestBodyAsBinary(
_ value: Data?,
headerFields: inout [HeaderField],
_ value: HTTPBody?,
headerFields: inout HTTPFields,
contentType: String
) throws -> Data? {
) throws -> HTTPBody? {
try setOptionalRequestBody(
value,
headerFields: &headerFields,
contentType: contentType,
convert: convertDataToBinary
convert: { $0 }
)
}

// | client | set | request body | binary | required | setRequiredRequestBodyAsBinary |
public func setRequiredRequestBodyAsBinary(
_ value: Data,
headerFields: inout [HeaderField],
_ value: HTTPBody,
headerFields: inout HTTPFields,
contentType: String
) throws -> Data {
) throws -> HTTPBody {
try setRequiredRequestBody(
value,
headerFields: &headerFields,
contentType: contentType,
convert: convertDataToBinary
convert: { $0 }
)
}

// | client | set | request body | urlEncodedForm | codable | optional | setOptionalRequestBodyAsURLEncodedForm |
public func setOptionalRequestBodyAsURLEncodedForm<T: Encodable>(
_ value: T,
headerFields: inout [HeaderField],
headerFields: inout HTTPFields,
contentType: String
) throws -> Data? {
) throws -> HTTPBody? {
try setOptionalRequestBody(
value,
headerFields: &headerFields,
Expand All @@ -185,9 +153,9 @@ extension Converter {
// | client | set | request body | urlEncodedForm | codable | required | setRequiredRequestBodyAsURLEncodedForm |
public func setRequiredRequestBodyAsURLEncodedForm<T: Encodable>(
_ value: T,
headerFields: inout [HeaderField],
headerFields: inout HTTPFields,
contentType: String
) throws -> Data {
) throws -> HTTPBody {
try setRequiredRequestBody(
value,
headerFields: &headerFields,
Expand All @@ -196,27 +164,16 @@ extension Converter {
)
}

// | client | get | response body | string | required | getResponseBodyAsString |
public func getResponseBodyAsString<T: Decodable, C>(
_ type: T.Type,
from data: Data,
transforming transform: (T) -> C
) throws -> C {
try getResponseBody(
type,
from: data,
transforming: transform,
convert: convertFromStringData
)
}

// | client | get | response body | JSON | required | getResponseBodyAsJSON |
public func getResponseBodyAsJSON<T: Decodable, C>(
_ type: T.Type,
from data: Data,
from data: HTTPBody?,
transforming transform: (T) -> C
) throws -> C {
try getResponseBody(
) async throws -> C {
guard let data else {
throw RuntimeError.missingRequiredResponseBody
}
return try await getBufferingResponseBody(
type,
from: data,
transforming: transform,
Expand All @@ -226,15 +183,18 @@ extension Converter {

// | client | get | response body | binary | required | getResponseBodyAsBinary |
public func getResponseBodyAsBinary<C>(
_ type: Data.Type,
from data: Data,
transforming transform: (Data) -> C
_ type: HTTPBody.Type,
from data: HTTPBody?,
transforming transform: (HTTPBody) -> C
) throws -> C {
try getResponseBody(
guard let data else {
throw RuntimeError.missingRequiredResponseBody
}
return try getResponseBody(
type,
from: data,
transforming: transform,
convert: convertBinaryToData
convert: { $0 }
)
}
}
17 changes: 9 additions & 8 deletions Sources/OpenAPIRuntime/Conversion/Converter+Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import HTTPTypes

extension Converter {

Expand All @@ -21,8 +22,8 @@ extension Converter {
/// - Parameter headerFields: The header fields to inspect for the content
/// type header.
/// - Returns: The content type value, or nil if not found or invalid.
public func extractContentTypeIfPresent(in headerFields: [HeaderField]) -> OpenAPIMIMEType? {
guard let rawValue = headerFields.firstValue(name: "content-type") else {
public func extractContentTypeIfPresent(in headerFields: HTTPFields) -> OpenAPIMIMEType? {
guard let rawValue = headerFields[.contentType] else {
return nil
}
return OpenAPIMIMEType(rawValue)
Expand Down Expand Up @@ -72,7 +73,7 @@ extension Converter {

// | common | set | header field | URI | both | setHeaderFieldAsURI |
public func setHeaderFieldAsURI<T: Encodable>(
in headerFields: inout [HeaderField],
in headerFields: inout HTTPFields,
name: String,
value: T?
) throws {
Expand All @@ -97,7 +98,7 @@ extension Converter {

// | common | set | header field | JSON | both | setHeaderFieldAsJSON |
public func setHeaderFieldAsJSON<T: Encodable>(
in headerFields: inout [HeaderField],
in headerFields: inout HTTPFields,
name: String,
value: T?
) throws {
Expand All @@ -111,7 +112,7 @@ extension Converter {

// | common | get | header field | URI | optional | getOptionalHeaderFieldAsURI |
public func getOptionalHeaderFieldAsURI<T: Decodable>(
in headerFields: [HeaderField],
in headerFields: HTTPFields,
name: String,
as type: T.Type
) throws -> T? {
Expand All @@ -133,7 +134,7 @@ extension Converter {

// | common | get | header field | URI | required | getRequiredHeaderFieldAsURI |
public func getRequiredHeaderFieldAsURI<T: Decodable>(
in headerFields: [HeaderField],
in headerFields: HTTPFields,
name: String,
as type: T.Type
) throws -> T {
Expand All @@ -155,7 +156,7 @@ extension Converter {

// | common | get | header field | JSON | optional | getOptionalHeaderFieldAsJSON |
public func getOptionalHeaderFieldAsJSON<T: Decodable>(
in headerFields: [HeaderField],
in headerFields: HTTPFields,
name: String,
as type: T.Type
) throws -> T? {
Expand All @@ -169,7 +170,7 @@ extension Converter {

// | common | get | header field | JSON | required | getRequiredHeaderFieldAsJSON |
public func getRequiredHeaderFieldAsJSON<T: Decodable>(
in headerFields: [HeaderField],
in headerFields: HTTPFields,
name: String,
as type: T.Type
) throws -> T {
Expand Down
Loading