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

Remove PostServiceRemoteExtended and implementations #784

Draft
wants to merge 18 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
96ae82f
Remove an `enum` builder in favor of default associated value
mokagio Apr 9, 2024
d17587a
Remove `PostServiceRemoteExtended` and implementations
mokagio Apr 9, 2024
58a6386
Update access control to components called by removed types
mokagio Apr 9, 2024
19bbd29
Move `upload(...` definition before `perform(...`
mokagio Apr 9, 2024
61dbb4e
Make a couple of properties used in `async` API `public`
mokagio Apr 9, 2024
5571b03
Remove a stray double new line
mokagio Apr 9, 2024
be9635f
Move `processError` to an extension on `WordPressComRESTAPIInterfacing`
mokagio Apr 9, 2024
61f6a44
Require `URLSession` in root `async` `perform` method
mokagio Apr 9, 2024
b03cc2d
Move `session` argument before `taskCreated`
mokagio Apr 9, 2024
a5edf0d
Require `invalidTokenHandler` in `perform`
mokagio Apr 9, 2024
68e32b8
Move root `perform` definition to `WordPressComRESTAPIInterfacing`
mokagio Apr 9, 2024
16f2af7
Extract logic to create builder with locale to extension
mokagio Apr 9, 2024
63ac625
Add new `localeValue` property to `WordPressComRESTAPIInterfacing`
mokagio Apr 9, 2024
b09ee31
Move request builder to `WordPressComRESTAPIInterfacing` extension
mokagio Apr 9, 2024
72c8f7b
Redefine `APIResult` typealias in the context of `WPComRESTAPIInterfa…
mokagio Apr 9, 2024
ed6a634
Move `urlSession` and `invalidTokenHandler` to API interface protocol
mokagio Apr 9, 2024
55d8e8f
Move a couple more `perform` types to API protocol
mokagio Apr 9, 2024
f9a15a5
Remove a couple of unused variable assignments
mokagio Apr 9, 2024
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);

/// - 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
36 changes: 26 additions & 10 deletions Sources/WordPressKit/Models/RemotePostParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,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 {
Comment on lines +183 to +187
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 @@ -212,10 +216,14 @@ struct RemotePostCreateParametersWordPressComEncoder: 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 All @@ -228,7 +236,7 @@ struct RemotePostUpdateParametersWordPressComEncoder: Encodable {
try container.encodeIfPresent(parameters.excerpt, forKey: .excerpt)
try container.encodeIfPresent(parameters.slug, forKey: .slug)
if let value = parameters.featuredImageID {
if let featuredImageID = value {
if value != nil {
try container.encode(parameters.featuredImageID, forKey: .featuredImageID)
} else {
// Passing `null` doesn't work.
Expand Down Expand Up @@ -274,10 +282,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 @@ -309,10 +321,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 All @@ -324,7 +340,7 @@ struct RemotePostUpdateParametersXMLRPCEncoder: Encodable {
try container.encodeIfPresent(parameters.excerpt, forKey: .excerpt)
try container.encodeIfPresent(parameters.slug, forKey: .slug)
if let value = parameters.featuredImageID {
if let featuredImageID = value {
if value != nil {
try container.encode(parameters.featuredImageID, forKey: .featuredImageID)
} else {
// Passing `null` doesn't work.
Expand Down
25 changes: 0 additions & 25 deletions Sources/WordPressKit/Services/PostServiceRemoteExtended.swift

This file was deleted.

67 changes: 0 additions & 67 deletions Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift

This file was deleted.

This file was deleted.

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))
}
Comment on lines -21 to -27
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