-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Implemented the `RequestDLClientTransport` to use the power of RequestDL with OpenAPI format. Fixes **None** ## Type of change <!--- Please delete options that are not relevant. --> - [x] New feature (non-breaking change which adds functionality) ## Checklist <!--- Please delete options that are not relevant. --> - [x] My code follows the code style of this project. - [x] I have updated the documentation accordingly. - [x] I have added tests to cover my changes.
- Loading branch information
Showing
6 changed files
with
259 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
See LICENSE for this package's licensing information. | ||
*/ | ||
|
||
import OpenAPIRuntime | ||
import RequestDL | ||
import Foundation | ||
|
||
struct OpenAPIRequest: Property { | ||
|
||
let request: OpenAPIRuntime.Request | ||
|
||
var body: some Property { | ||
if let data = request.body { | ||
Payload(data: data) | ||
} | ||
|
||
if let query = request.query { | ||
Path(request.path + (query.hasPrefix("?") ? query : "?\(query)")) | ||
} else { | ||
Path(request.path) | ||
} | ||
|
||
PropertyForEach(request.headerFields, id: \.self) { header in | ||
CustomHeader(name: header.name, value: header.value) | ||
} | ||
|
||
RequestMethod(.init(request.method.name)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
125 changes: 125 additions & 0 deletions
125
Sources/OpenAPIRequestDL/RequestDLClientTransport.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
See LICENSE for this package's licensing information. | ||
*/ | ||
|
||
import OpenAPIRuntime | ||
import RequestDL | ||
import Foundation | ||
|
||
/** | ||
A `ClientTransport` that utilizes `RequestDL` to execute HTTP requests. | ||
When creating the `Client` for `OpenAPIRuntime`, the instantiated transport should be provided as | ||
shown in the example: | ||
```swift | ||
Client( | ||
serverURL: try! Servers.server1(), | ||
transport: RequestDLClientTransport() | ||
) | ||
``` | ||
You can add extra configurations if necessary to maintain communication security with the server: | ||
```swift | ||
RequestDLClientTransport { | ||
SecureConnection { | ||
AdditionalTrusts("apple.com") | ||
// Add more properties as needed | ||
} | ||
} | ||
``` | ||
Furthermore, you can combine any other necessary `Property` objects to customize your request, just | ||
specify them as shown in the example above. | ||
*/ | ||
public struct RequestDLClientTransport: ClientTransport { | ||
|
||
// MARK: - Private properties | ||
|
||
private let content: AnyProperty | ||
private let task: @Sendable (AnyProperty) -> any RequestTask<TaskResult<Data>> | ||
|
||
// MARK: - Inits | ||
|
||
/// Instancia o `RequestDLClientTransport`. | ||
public init() { | ||
self.init(content: EmptyProperty.init) | ||
} | ||
|
||
/** | ||
Initializes the `RequestDLClientTransport` with a content closure that returns a `Property` | ||
object. | ||
- Parameter content: A closure that returns a `Property` object, which specifies the properties | ||
of the request. | ||
Example usage: | ||
```swift | ||
let transport = RequestDLClientTransport { | ||
BaseURL("https://api.example.com") | ||
RequestMethod(.post) | ||
// Add more properties as needed | ||
} | ||
let client = Client( | ||
serverURL: try! Servers.server1(), | ||
transport: transport | ||
) | ||
``` | ||
*/ | ||
public init<Content: Property>(@PropertyBuilder content: () -> Content) { | ||
self.init( | ||
content: content(), | ||
task: { request in DataTask { request }} | ||
) | ||
} | ||
|
||
init<Content: Property>( | ||
content: Content, | ||
task: @escaping @Sendable (AnyProperty) -> any RequestTask<TaskResult<Data>> | ||
) { | ||
self.content = .init(content) | ||
self.task = task | ||
} | ||
|
||
// MARK: - Public methods | ||
|
||
public func send( | ||
_ request: OpenAPIRuntime.Request, | ||
baseURL: URL, | ||
operationID: String | ||
) async throws -> OpenAPIRuntime.Response { | ||
|
||
let scheme = baseURL.scheme ?? "http" | ||
let url = baseURL.absoluteString.replacingOccurrences( | ||
of: scheme + "://", | ||
with: "" | ||
) | ||
var components = url.split(separator: "/") | ||
let baseURL = components.removeFirst() | ||
|
||
let response = try await task(AnyProperty( | ||
PropertyGroup { | ||
content | ||
|
||
BaseURL(.init(scheme), host: String(baseURL)) | ||
PropertyForEach(components, id: \.self) { | ||
Path($0) | ||
} | ||
|
||
OpenAPIRequest(request: request) | ||
} | ||
)) | ||
.result() | ||
|
||
return .init( | ||
statusCode: Int(response.head.status.code), | ||
headerFields: response.head.headers.map { | ||
.init(name: $0, value: $1) | ||
}, | ||
body: response.payload | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters