Skip to content

Resolve TODOs by backporting async/await code #610

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

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ _None._

### Internal Changes

_None._
- Enable async/await backporting to iOS 13 for `WordPressComRestApi` (resolves a TODO) [#610]

### Breaking Changes

Expand Down
31 changes: 23 additions & 8 deletions WordPressKit/WordPressComRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public enum ResponseType {

open class WordPressComRestApi: NSObject {

enum Errors: Error {
case invalidResponse
}

// MARK: Properties

@objc public static let ErrorKeyErrorCode = "WordPressComRestApiErrorCodeKey"
Expand Down Expand Up @@ -442,15 +446,14 @@ open class WordPressComRestApi: NSObject {
return URLSession(configuration: configuration)
}()

// TODO: when migrating to Xcode 13.2 make this available to iOS 13
/**
Executes a GET request to the specified endpoint

- parameter url: the url `String` to be requested

- returns: a tuple containing the `Data` of the response and the `URLResponse` object
*/
@available(iOS 15.0.0, *)
@available(iOS 13.0, *)
public func get(_ urlString: String) async throws -> (Data, URLResponse) {
guard let url = URL(string: urlString) else {
throw WordPressComRestApiError.malformedURL
Expand All @@ -468,14 +471,13 @@ open class WordPressComRestApi: NSObject {

- returns: a `Decodable` object of the `Type` given
*/
@available(iOS 15.0.0, *)
@available(iOS 13.0, *)
public func get<T: Decodable>(_ urlString: String) async throws -> T {
let (data, _) = try await get(urlString)
let object = try JSONDecoder().decode(T.self, from: data)
return object
}

// TODO: when migrating to Xcode 13.2 make this available to iOS 13
/**
Executes a POST request to the specified endpoint

Expand All @@ -484,7 +486,7 @@ open class WordPressComRestApi: NSObject {

- returns: a tuple containing the `Data` of the response and the `URLResponse` object
*/
@available(iOS 15.0.0, *)
@available(iOS 13.0, *)
public func post(_ urlString: String, parameters: [String: Any]? = nil) async throws -> (Data, URLResponse) {
guard let url = URL(string: urlString) else {
throw WordPressComRestApiError.malformedURL
Expand All @@ -498,10 +500,23 @@ open class WordPressComRestApi: NSObject {
request.httpBody = parameters.percentEncoded()
}

return try await urlSession.data(for: request, delegate: nil)
return try await withCheckedThrowingContinuation { continuation in
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urlSession.data(for:) is only available in iOS15, so I had to wrap the method in async/await myself

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jetpack/WordPress will soon support iOS 15 and above only and WooCommerce already does.

Your diff doesn't add much complexity, but I wonder if it would be cleaner to bump the library to iOS 15 instead.

urlSession.dataTask(with: request) { data, urlResponse, error in
if let error {
continuation.resume(throwing: error)
return
}

if let data, let urlResponse {
continuation.resume(with: .success((data, urlResponse)))
return
}

continuation.resume(throwing: Errors.invalidResponse)
}
}
}

// TODO: when migrating to Xcode 13.2 make this available to iOS 13
/**
Executes a POST request to the specified endpoint

Expand All @@ -511,7 +526,7 @@ open class WordPressComRestApi: NSObject {

- returns: a `Decodable` object of the `Type` given
*/
@available(iOS 15.0.0, *)
@available(iOS 13.0, *)
public func post<T: Decodable>(_ urlString: String, parameters: [String: Any]? = nil) async throws -> T {
let (data, _) = try await post(urlString, parameters: parameters)
let object = try JSONDecoder().decode(T.self, from: data)
Expand Down