-
Notifications
You must be signed in to change notification settings - Fork 3
[Swift] Fix the Example app #137
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e063789
Remove Namespace types and use Rust types directly in Swift
crazytonyli 368bc47
Address SwiftLint issues
crazytonyli ccc7b24
[Swift] Stub HTTP requests in unit tests
crazytonyli 23e8f63
Add `SafeRequestExecutor` to avoid passing random error to Rust
crazytonyli abe6084
[Swift] Fix the Example app
crazytonyli 146eb03
[Swift] Use Swift 5.10 docker image
crazytonyli d80360a
[Swift] Silent SwiftLint warnings on 'TODO's
crazytonyli 5fc35fe
Fix linter issues
jkmassel 40abde3
Merge branch 'trunk' into request-executor-swift-package
crazytonyli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
native/swift/Sources/wordpress-api/SafeRequestExecutor.swift
This file contains hidden or 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,45 @@ | ||
import Foundation | ||
#if canImport(WordPressAPIInternal) | ||
import WordPressAPIInternal | ||
#endif | ||
|
||
#if os(Linux) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public protocol SafeRequestExecutor: RequestExecutor { | ||
|
||
func execute(_ request: WpNetworkRequest) async -> Result<WpNetworkResponse, RequestExecutionError> | ||
|
||
} | ||
|
||
extension SafeRequestExecutor { | ||
|
||
public func execute(request: WpNetworkRequest) async throws -> WpNetworkResponse { | ||
let result = await execute(request) | ||
return try result.get() | ||
} | ||
|
||
} | ||
|
||
extension URLSession: SafeRequestExecutor { | ||
|
||
// swiftlint:disable force_cast | ||
public func execute(_ request: WpNetworkRequest) async -> Result<WpNetworkResponse, RequestExecutionError> { | ||
do { | ||
let (data, response) = try await self.data(for: request.asURLRequest()) | ||
let urlResponse = response as! HTTPURLResponse | ||
return .success( | ||
WpNetworkResponse( | ||
body: data, | ||
statusCode: UInt16(urlResponse.statusCode), | ||
headerMap: urlResponse.httpHeaders | ||
) | ||
) | ||
} catch { | ||
// TODO: Translate error into the Rust type | ||
return .failure(.RequestExecutionFailed(statusCode: nil, reason: "")) | ||
} | ||
} | ||
// swiftlint:enable force_cast | ||
} |
This file contains hidden or 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,28 @@ | ||
#if os(Linux) | ||
|
||
import Foundation | ||
import FoundationNetworking | ||
|
||
// `URLSession.data(for:) async throws` is not available on Linux's Foundation framework. | ||
extension URLSession { | ||
func data(for request: URLRequest) async throws -> (Data, URLResponse) { | ||
try await withCheckedThrowingContinuation { continuation in | ||
let task = self.dataTask(with: request) { data, response, error in | ||
if let error { | ||
continuation.resume(throwing: error) | ||
return | ||
} | ||
|
||
guard let data = data, let response = response else { | ||
continuation.resume(throwing: WordPressAPI.Errors.unableToParseResponse) | ||
return | ||
} | ||
|
||
continuation.resume(returning: (data, response)) | ||
} | ||
task.resume() | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily for this PR, but are we able to make it so we can just call this like
try await api.users.listWithViewContext()
and provide params if needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. That's implemented in the old API design where we had generic types to check if the
params
argument isOptional
. I'll look into it later, potentially together with providing a better API regarding handling the newRequestExecutionError
type.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, we can just implement a few
listWith[View|Edit|Embed]Context()
functions to Swift, if we can't have a general solution in short term.