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

SPM Prep – Move some async perform types at the WordPressComRESTAPIInterface level #788

Open
wants to merge 17 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
16 changes: 16 additions & 0 deletions Sources/APIInterface/include/WordPressComRESTAPIInterfacing.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@

@property (strong, nonatomic, readonly) NSURL * _Nonnull baseURL;

/// Whether the user's preferred language locale should be appended to the request.
/// Should default to `true`.
///
/// - SeeAlso: `localeKey` and `localeValue` to configure the locale appendend to the request.
@property (nonatomic, readonly) BOOL appendsPreferredLanguageLocale;

/// The key with which to specify locale in the parameters of a request.
@property (strong, nonatomic, readonly) NSString * _Nonnull localeKey;

/// The value with which to specify locale in the parameters of a request.
@property (strong, nonatomic, readonly) NSString * _Nonnull localeValue;

@property (strong, nonatomic, readonly) NSURLSession * _Nonnull urlSession;

@property (strong, nonatomic, readonly) void (^ _Nullable invalidTokenHandler)(void);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All these properties went in the protocol to make the async perform methods work.

The async perform methods, which like in WordPressComRestApi are Swift-only, of course.

I considered using an extension or a different protocol, but it seemed neat to have it all in WordPressComRESTAPIInterfacing, so that Swift code can used an upgraded version of the same interface that the Objective-C code uses. This will actually be useful in the cases where we have an ObjC superclass with the API client conforming to WordPressComRESTAPIInterfacing and a subclass using the API client, too.


/// - Note: `parameters` has `id` instead of the more common `NSObject *` as its value type so it will convert to `AnyObject` in Swift.
/// In Swift, it's simpler to work with `AnyObject` than with `NSObject`. For example `"abc" as AnyObject` over `"abc" as NSObject`.
- (NSProgress * _Nullable)get:(NSString * _Nonnull)URLString
Expand Down
32 changes: 24 additions & 8 deletions Sources/WordPressKit/Models/RemotePostParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,14 @@ private enum RemotePostWordPressComCodingKeys: String, CodingKey {
static let postTags = "post_tag"
}

struct RemotePostCreateParametersWordPressComEncoder: Encodable {
public struct RemotePostCreateParametersWordPressComEncoder: Encodable {
let parameters: RemotePostCreateParameters

func encode(to encoder: Encoder) throws {
public init(parameters: RemotePostCreateParameters) {
self.parameters = parameters
}

public func encode(to encoder: Encoder) throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When the type became public, a public init became necessary because the compiler does not synthesize one outside of the package.

As for encode(to:), it needs to be public because Encodable is declared as part of the public type interface.

🤔 In hindsight, I suppose the Encodable part could be moved into an internal extension, but I think it's okay to leave it as is. Let me know if you can think of a strong reason to hide it.

var container = encoder.container(keyedBy: RemotePostWordPressComCodingKeys.self)
try container.encodeIfPresent(parameters.type, forKey: .type)
try container.encodeIfPresent(parameters.status, forKey: .status)
Expand Down Expand Up @@ -281,10 +285,14 @@ struct RemotePostUpdateParametersWordPressComMetadata: Encodable {
}
}

struct RemotePostUpdateParametersWordPressComEncoder: Encodable {
public struct RemotePostUpdateParametersWordPressComEncoder: Encodable {
let parameters: RemotePostUpdateParameters

func encode(to encoder: Encoder) throws {
public init(parameters: RemotePostUpdateParameters) {
self.parameters = parameters
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: RemotePostWordPressComCodingKeys.self)
try container.encodeIfPresent(parameters.ifNotModifiedSince, forKey: .ifNotModifiedSince)

Expand Down Expand Up @@ -348,10 +356,14 @@ private enum RemotePostXMLRPCCodingKeys: String, CodingKey {
static let postTags = "post_tag"
}

struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
public struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
let parameters: RemotePostCreateParameters

func encode(to encoder: Encoder) throws {
public init(parameters: RemotePostCreateParameters) {
self.parameters = parameters
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: RemotePostXMLRPCCodingKeys.self)
try container.encode(parameters.type, forKey: .type)
try container.encodeIfPresent(parameters.status, forKey: .postStatus)
Expand Down Expand Up @@ -387,10 +399,14 @@ struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
}
}

struct RemotePostUpdateParametersXMLRPCEncoder: Encodable {
public struct RemotePostUpdateParametersXMLRPCEncoder: Encodable {
let parameters: RemotePostUpdateParameters

func encode(to encoder: Encoder) throws {
public init(parameters: RemotePostUpdateParameters) {
self.parameters = parameters
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: RemotePostXMLRPCCodingKeys.self)
try container.encodeIfPresent(parameters.ifNotModifiedSince, forKey: .ifNotModifiedSince)
try container.encodeIfPresent(parameters.status, forKey: .postStatus)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extension DateFormatter {
/// A `DateFormatter` configured to manage dates compatible with the WordPress.com API.
///
/// - SeeAlso: [https://developer.wordpress.com/docs/api/](https://developer.wordpress.com/docs/api/)
static let wordPressCom: DateFormatter = {
public static let wordPressCom: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) as TimeZone
Expand Down
4 changes: 2 additions & 2 deletions Sources/WordPressKit/WordPressAPI/HTTPRequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import wpxmlrpc
///
/// Calling this class's url related functions (the ones that changes path, query, etc) does not modify the
/// original URL string. The URL will be perserved in the final result that's returned by the `build` function.
final class HTTPRequestBuilder {
enum Method: String, CaseIterable {
public final class HTTPRequestBuilder {
public enum Method: String, CaseIterable {
case get = "GET"
case post = "POST"
case put = "PUT"
Expand Down
6 changes: 1 addition & 5 deletions Sources/WordPressKit/WordPressAPI/WordPressAPIError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ public enum WordPressAPIError<EndpointError>: Error where EndpointError: Localiz
/// The API call returned an status code that's unacceptable to the endpoint.
case unacceptableStatusCode(response: HTTPURLResponse, body: Data)
/// The API call returned an HTTP response that WordPressKit can't parse. Receiving this error could be an indicator that there is an error response that's not handled properly by WordPressKit.
case unparsableResponse(response: HTTPURLResponse?, body: Data?, underlyingError: Error)
case unparsableResponse(response: HTTPURLResponse?, body: Data?, underlyingError: Error = URLError(.cannotParseResponse))
/// Other error occured.
case unknown(underlyingError: Error)

static func unparsableResponse(response: HTTPURLResponse?, body: Data?) -> Self {
return WordPressAPIError<EndpointError>.unparsableResponse(response: response, body: body, underlyingError: URLError(.cannotParseResponse))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't look up when, but relatively recently, I think, Swift acquired default values for associated parameters. So we can remove this builder method in favor of defining the underlying error as a default for the case.


var response: HTTPURLResponse? {
switch self {
case .requestEncodingFailure, .connection, .unknown:
Expand Down
Loading