Skip to content
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

[Runtime] Integrate the new URI and String coders #45

Merged
merged 37 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
956d966
[Prototype] URIEncoder
czechboy0 Aug 21, 2023
bb9ba79
Merge branch 'main' into hd-uri-encoder
czechboy0 Aug 21, 2023
dbbe261
Refactored the URIEncoder a bunch, only support top level values for …
czechboy0 Aug 22, 2023
2c99128
Encoder, serializer, parser work, what remains is the decoder
czechboy0 Aug 23, 2023
2f710ab
Renaming, prepared the ground for implementing the decoder
czechboy0 Aug 23, 2023
569c576
WIP on the Decoder
czechboy0 Aug 23, 2023
e6d4b2f
Decoder working as well now
czechboy0 Aug 23, 2023
1befb30
Allow caching the parsed result
czechboy0 Aug 24, 2023
fcc413b
Update tests
czechboy0 Aug 24, 2023
ed6e073
Added rountrip tests
czechboy0 Aug 24, 2023
ae171d5
Improve tests, fix a few bugs around empty containers
czechboy0 Aug 24, 2023
e36c893
Formatting
czechboy0 Aug 24, 2023
e6526b0
WIP on integrating the experimental URI coder
czechboy0 Aug 24, 2023
3b36c16
Merge branch 'main' into hd-uri-encoder
czechboy0 Aug 24, 2023
c54983a
Merge branch 'hd-uri-encoder' into hd-uri-encoder-integration
czechboy0 Aug 24, 2023
a24c805
PR feedback
czechboy0 Aug 24, 2023
a5f7c83
Merge branch 'hd-uri-encoder' into hd-uri-encoder-integration
czechboy0 Aug 24, 2023
697a62b
Add a StringEncoder and StringDecoder, to handle the remaining respon…
czechboy0 Aug 24, 2023
1d84e81
Prepare new URI-based converter variants for headers
czechboy0 Aug 24, 2023
a153358
WIP
czechboy0 Aug 25, 2023
d821442
Add Date support
czechboy0 Aug 25, 2023
e73ef79
Merge branch 'hd-uri-encoder' into hd-uri-encoder-integration
czechboy0 Aug 25, 2023
9315d9b
Add Date support to String coder, fix up query items
czechboy0 Aug 25, 2023
e24cb86
Path param support
czechboy0 Aug 25, 2023
fec080e
Query params
czechboy0 Aug 25, 2023
5fc468c
Request bodies working
czechboy0 Aug 25, 2023
bbeb04e
More cleanup
czechboy0 Aug 25, 2023
efb7fc0
WIP on documenting the URI coder
czechboy0 Aug 25, 2023
1eec75f
Finished documenting the URI coder
czechboy0 Aug 25, 2023
e3d25c2
Merge branch 'hd-uri-encoder' into hd-uri-encoder-integration
czechboy0 Aug 25, 2023
96fa202
Introduce StringEncoder and StringDecoder types
czechboy0 Aug 25, 2023
db84422
Fixed missing escaping of formatted dates
czechboy0 Aug 25, 2023
9fafceb
Brought in changes from the standalone PR
czechboy0 Aug 26, 2023
3f0be17
Merge branch 'hd-string-coder' into hd-uri-and-string-coders
czechboy0 Aug 26, 2023
5c6b385
Merge branch 'hd-uri-and-string-coders' into hd-uri-encoder-integration
czechboy0 Aug 26, 2023
e94d59c
Updated unit tests
czechboy0 Aug 28, 2023
983cb71
Merge branch 'main' into hd-uri-encoder-integration
czechboy0 Aug 29, 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
161 changes: 41 additions & 120 deletions Sources/OpenAPIRuntime/Conversion/Converter+Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,98 +31,61 @@ extension Converter {
)
}

// | client | set | request path | text | string-convertible | required | renderedRequestPath |
public func renderedRequestPath(
// | client | set | request path | URI | required | renderedPath |
czechboy0 marked this conversation as resolved.
Show resolved Hide resolved
public func renderedPath(
template: String,
parameters: [any _StringConvertible]
parameters: [any Encodable]
) throws -> String {
var renderedString = template
let encoder = URIEncoder(
configuration: .init(
style: .simple,
explode: false,
spaceEscapingCharacter: .percentEncoded,
dateTranscoder: configuration.dateTranscoder
)
)
for parameter in parameters {
let value = try encoder.encode(parameter, forKey: "")
if let range = renderedString.range(of: "{}") {
renderedString = renderedString.replacingOccurrences(
of: "{}",
with: parameter.description,
with: value,
range: range
)
}
}
return renderedString
}

// | client | set | request query | text | string-convertible | both | setQueryItemAsText |
public func setQueryItemAsText<T: _StringConvertible>(
// | client | set | request query | URI | both | setQueryItemAsURI |
public func setQueryItemAsURI<T: Encodable>(
in request: inout Request,
style: ParameterStyle?,
explode: Bool?,
name: String,
value: T?
) throws {
try setQueryItem(
try setEscapedQueryItem(
in: &request,
style: style,
explode: explode,
name: name,
value: value,
convert: convertStringConvertibleToText
)
}

// | client | set | request query | text | array of string-convertibles | both | setQueryItemAsText |
public func setQueryItemAsText<T: _StringConvertible>(
in request: inout Request,
style: ParameterStyle?,
explode: Bool?,
name: String,
value: [T]?
) throws {
try setQueryItems(
in: &request,
style: style,
explode: explode,
name: name,
values: value,
convert: convertStringConvertibleToText
)
}

// | client | set | request query | text | date | both | setQueryItemAsText |
public func setQueryItemAsText(
in request: inout Request,
style: ParameterStyle?,
explode: Bool?,
name: String,
value: Date?
) throws {
try setQueryItem(
in: &request,
style: style,
explode: explode,
name: name,
value: value,
convert: convertDateToText
)
}

// | client | set | request query | text | array of dates | both | setQueryItemAsText |
public func setQueryItemAsText(
in request: inout Request,
style: ParameterStyle?,
explode: Bool?,
name: String,
value: [Date]?
) throws {
try setQueryItems(
in: &request,
style: style,
explode: explode,
name: name,
values: value,
convert: convertDateToText
convert: { value, style, explode in
try convertToURI(
style: style,
explode: explode,
inBody: false,
key: name,
value: value
)
}
)
}

// | client | set | request body | text | string-convertible | optional | setOptionalRequestBodyAsText |
public func setOptionalRequestBodyAsText<T: _StringConvertible>(
// | client | set | request body | string | optional | setOptionalRequestBodyAsString |
public func setOptionalRequestBodyAsString<T: Encodable>(
_ value: T?,
headerFields: inout [HeaderField],
contentType: String
Expand All @@ -131,12 +94,12 @@ extension Converter {
value,
headerFields: &headerFields,
contentType: contentType,
convert: convertStringConvertibleToTextData
convert: convertToStringData
)
}

// | client | set | request body | text | string-convertible | required | setRequiredRequestBodyAsText |
public func setRequiredRequestBodyAsText<T: _StringConvertible>(
// | client | set | request body | string | required | setRequiredRequestBodyAsString |
public func setRequiredRequestBodyAsString<T: Encodable>(
_ value: T,
headerFields: inout [HeaderField],
contentType: String
Expand All @@ -145,39 +108,11 @@ extension Converter {
value,
headerFields: &headerFields,
contentType: contentType,
convert: convertStringConvertibleToTextData
)
}

// | client | set | request body | text | date | optional | setOptionalRequestBodyAsText |
public func setOptionalRequestBodyAsText(
_ value: Date?,
headerFields: inout [HeaderField],
contentType: String
) throws -> Data? {
try setOptionalRequestBody(
value,
headerFields: &headerFields,
contentType: contentType,
convert: convertDateToTextData
convert: convertToStringData
)
}

// | client | set | request body | text | date | required | setRequiredRequestBodyAsText |
public func setRequiredRequestBodyAsText(
_ value: Date,
headerFields: inout [HeaderField],
contentType: String
) throws -> Data {
try setRequiredRequestBody(
value,
headerFields: &headerFields,
contentType: contentType,
convert: convertDateToTextData
)
}

// | client | set | request body | JSON | codable | optional | setOptionalRequestBodyAsJSON |
// | client | set | request body | JSON | optional | setOptionalRequestBodyAsJSON |
public func setOptionalRequestBodyAsJSON<T: Encodable>(
_ value: T?,
headerFields: inout [HeaderField],
Expand All @@ -191,7 +126,7 @@ extension Converter {
)
}

// | client | set | request body | JSON | codable | required | setRequiredRequestBodyAsJSON |
// | client | set | request body | JSON | required | setRequiredRequestBodyAsJSON |
public func setRequiredRequestBodyAsJSON<T: Encodable>(
_ value: T,
headerFields: inout [HeaderField],
Expand All @@ -205,7 +140,7 @@ extension Converter {
)
}

// | client | set | request body | binary | data | optional | setOptionalRequestBodyAsBinary |
// | client | set | request body | binary | optional | setOptionalRequestBodyAsBinary |
public func setOptionalRequestBodyAsBinary(
_ value: Data?,
headerFields: inout [HeaderField],
Expand All @@ -219,7 +154,7 @@ extension Converter {
)
}

// | client | set | request body | binary | data | required | setRequiredRequestBodyAsBinary |
// | client | set | request body | binary | required | setRequiredRequestBodyAsBinary |
public func setRequiredRequestBodyAsBinary(
_ value: Data,
headerFields: inout [HeaderField],
Expand All @@ -233,8 +168,8 @@ extension Converter {
)
}

// | client | get | response body | text | string-convertible | required | getResponseBodyAsText |
public func getResponseBodyAsText<T: _StringConvertible, C>(
// | client | get | response body | string | required | getResponseBodyAsString |
public func getResponseBodyAsString<T: Decodable, C>(
_ type: T.Type,
from data: Data,
transforming transform: (T) -> C
Expand All @@ -243,25 +178,11 @@ extension Converter {
type,
from: data,
transforming: transform,
convert: convertTextDataToStringConvertible
)
}

// | client | get | response body | text | date | required | getResponseBodyAsText |
public func getResponseBodyAsText<C>(
_ type: Date.Type,
from data: Data,
transforming transform: (Date) -> C
) throws -> C {
try getResponseBody(
type,
from: data,
transforming: transform,
convert: convertTextDataToDate
convert: convertFromStringData
)
}

// | client | get | response body | JSON | codable | required | getResponseBodyAsJSON |
// | client | get | response body | JSON | required | getResponseBodyAsJSON |
public func getResponseBodyAsJSON<T: Decodable, C>(
_ type: T.Type,
from data: Data,
Expand All @@ -271,11 +192,11 @@ extension Converter {
type,
from: data,
transforming: transform,
convert: convertJSONToCodable
convert: convertJSONToBodyCodable
)
}

// | client | get | response body | binary | data | required | getResponseBodyAsBinary |
// | client | get | response body | binary | required | getResponseBodyAsBinary |
public func getResponseBodyAsBinary<C>(
_ type: Data.Type,
from data: Data,
Expand Down
Loading