Skip to content

Commit 7b624cd

Browse files
authored
Define CoreAPI package (#782)
2 parents e3ab875 + 6fba3a1 commit 7b624cd

File tree

80 files changed

+712
-367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+712
-367
lines changed

.swiftlint.yml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ swiftlint_version: 0.54.0
33
parent_config: https://raw.githubusercontent.com/Automattic/swiftlint-config/0f8ab6388bd8d15a04391825ab125f80cfb90704/.swiftlint.yml
44
remote_timeout: 10.0
55

6+
excluded:
7+
.build # `swift build` etc. output folder
8+
69
opt_in_rules:
710
- overridden_super_call
811
- discarded_notification_center_observer

Package.resolved

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"pins" : [
3+
{
4+
"identity" : "alamofire",
5+
"kind" : "remoteSourceControl",
6+
"location" : "https://github.com/Alamofire/Alamofire",
7+
"state" : {
8+
"revision" : "f455c2975872ccd2d9c81594c658af65716e9b9a",
9+
"version" : "5.9.1"
10+
}
11+
},
12+
{
13+
"identity" : "collectionconcurrencykit",
14+
"kind" : "remoteSourceControl",
15+
"location" : "https://github.com/JohnSundell/CollectionConcurrencyKit.git",
16+
"state" : {
17+
"revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95",
18+
"version" : "0.2.0"
19+
}
20+
},
21+
{
22+
"identity" : "cryptoswift",
23+
"kind" : "remoteSourceControl",
24+
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
25+
"state" : {
26+
"revision" : "7892a123f7e8d0fe62f9f03728b17bbd4f94df5c",
27+
"version" : "1.8.1"
28+
}
29+
},
30+
{
31+
"identity" : "ohhttpstubs",
32+
"kind" : "remoteSourceControl",
33+
"location" : "https://github.com/AliSoftware/OHHTTPStubs",
34+
"state" : {
35+
"revision" : "12f19662426d0434d6c330c6974d53e2eb10ecd9",
36+
"version" : "9.1.0"
37+
}
38+
},
39+
{
40+
"identity" : "sourcekitten",
41+
"kind" : "remoteSourceControl",
42+
"location" : "https://github.com/jpsim/SourceKitten.git",
43+
"state" : {
44+
"revision" : "b6dc09ee51dfb0c66e042d2328c017483a1a5d56",
45+
"version" : "0.34.1"
46+
}
47+
},
48+
{
49+
"identity" : "swift-argument-parser",
50+
"kind" : "remoteSourceControl",
51+
"location" : "https://github.com/apple/swift-argument-parser.git",
52+
"state" : {
53+
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
54+
"version" : "1.2.3"
55+
}
56+
},
57+
{
58+
"identity" : "swift-syntax",
59+
"kind" : "remoteSourceControl",
60+
"location" : "https://github.com/apple/swift-syntax.git",
61+
"state" : {
62+
"revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036",
63+
"version" : "509.0.2"
64+
}
65+
},
66+
{
67+
"identity" : "swiftlint",
68+
"kind" : "remoteSourceControl",
69+
"location" : "https://github.com/realm/SwiftLint",
70+
"state" : {
71+
"revision" : "f17a4f9dfb6a6afb0408426354e4180daaf49cee",
72+
"version" : "0.54.0"
73+
}
74+
},
75+
{
76+
"identity" : "swiftytexttable",
77+
"kind" : "remoteSourceControl",
78+
"location" : "https://github.com/scottrhoyt/SwiftyTextTable.git",
79+
"state" : {
80+
"revision" : "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3",
81+
"version" : "0.9.0"
82+
}
83+
},
84+
{
85+
"identity" : "swxmlhash",
86+
"kind" : "remoteSourceControl",
87+
"location" : "https://github.com/drmohundro/SWXMLHash.git",
88+
"state" : {
89+
"revision" : "a853604c9e9a83ad9954c7e3d2a565273982471f",
90+
"version" : "7.0.2"
91+
}
92+
},
93+
{
94+
"identity" : "wordpress-ios-shared",
95+
"kind" : "remoteSourceControl",
96+
"location" : "https://github.com/wordpress-mobile/WordPress-iOS-Shared.git",
97+
"state" : {
98+
"branch" : "mokagio/swiftlint-read-as-dependency",
99+
"revision" : "422950b28f01d7cc11218e7d70a6cd65004d23ae"
100+
}
101+
},
102+
{
103+
"identity" : "wpxmlrpc",
104+
"kind" : "remoteSourceControl",
105+
"location" : "https://github.com/wordpress-mobile/wpxmlrpc",
106+
"state" : {
107+
"revision" : "bfc413d336bdeaab89e62dc483380baa99b2257e",
108+
"version" : "0.10.0"
109+
}
110+
},
111+
{
112+
"identity" : "yams",
113+
"kind" : "remoteSourceControl",
114+
"location" : "https://github.com/jpsim/Yams.git",
115+
"state" : {
116+
"revision" : "8a835d918245ca22f36663dd3862138805d7f707",
117+
"version" : "5.1.0"
118+
}
119+
}
120+
],
121+
"version" : 2
122+
}

Package.swift

+37-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,46 @@ import PackageDescription
44

55
let package = Package(
66
name: "WordPressKit",
7-
platforms: [.iOS(.v13)],
7+
platforms: [
8+
.iOS(.v13),
9+
// The package(s) are meant for iOS only, but the use of the SwiftLint plugin down the dependency chain requires specifying a compatible macOS version.
10+
.macOS(.v12),
11+
],
812
products: [
913
.library(name: "APIInterface", targets: ["APIInterface"]),
14+
.library(name: "CoreAPI", targets: ["CoreAPI"]),
15+
],
16+
dependencies: [
17+
// .package(url: "https://github.com/wordpress-mobile/WordPress-iOS-Shared.git", from: "2.3.1"),
18+
// See https://github.com/wordpress-mobile/WordPress-iOS-Shared/pull/354
19+
.package(url: "https://github.com/wordpress-mobile/WordPress-iOS-Shared.git", branch: "mokagio/swiftlint-read-as-dependency"),
20+
.package(url: "https://github.com/wordpress-mobile/wpxmlrpc", from: "0.10.0"),
21+
// Test dependencies
22+
.package(url: "https://github.com/AliSoftware/OHHTTPStubs", from: "9.1.0"),
23+
.package(url: "https://github.com/Alamofire/Alamofire", from: "5.8.1"),
1024
],
11-
dependencies: [],
1225
targets: [
13-
.target(name: "APIInterface")
26+
.target(name: "APIInterface"),
27+
.target(
28+
name: "CoreAPI",
29+
dependencies: [
30+
.target(name: "APIInterface"),
31+
.product(name: "WordPressShared", package: "WordPress-iOS-Shared"),
32+
"wpxmlrpc"
33+
]
34+
),
35+
.testTarget(
36+
name: "CoreAPITests",
37+
dependencies: [
38+
.target(name: "CoreAPI"),
39+
.product(name: "OHHTTPStubs", package: "OHHTTPStubs"),
40+
.product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs"),
41+
"Alamofire",
42+
],
43+
path: "Tests/CoreAPITests",
44+
resources: [
45+
.process("Stubs") // Relative to path
46+
]
47+
),
1448
]
1549
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#import <Foundation/Foundation.h>
2+
#if SWIFT_PACKAGE
3+
#import "WordPressComRESTAPIVersionedPathBuilder.h"
4+
#import "WordPressComRESTAPIVersion.h"
5+
#else
6+
#import "WordPressKit/WordPressComRESTAPIVersionedPathBuilder.h"
7+
#endif
8+
9+
static NSString* const WordPressComRESTApiVersionStringInvalid = @"invalid_api_version";
10+
static NSString* const WordPressComRESTApiVersionString_1_0 = @"rest/v1";
11+
static NSString* const WordPressComRESTApiVersionString_1_1 = @"rest/v1.1";
12+
static NSString* const WordPressComRESTApiVersionString_1_2 = @"rest/v1.2";
13+
static NSString* const WordPressComRESTApiVersionString_1_3 = @"rest/v1.3";
14+
static NSString* const WordPressComRESTApiVersionString_2_0 = @"wpcom/v2";
15+
16+
@implementation WordPressComRESTAPIVersionedPathBuilder
17+
18+
+ (NSString *)pathForEndpoint:(NSString *)endpoint
19+
withVersion:(WordPressComRESTAPIVersion)apiVersion
20+
{
21+
NSString *apiVersionString = [self apiVersionStringWithEnumValue:apiVersion];
22+
23+
return [NSString stringWithFormat:@"%@/%@", apiVersionString, endpoint];
24+
}
25+
26+
+ (NSString *)apiVersionStringWithEnumValue:(WordPressComRESTAPIVersion)apiVersion
27+
{
28+
NSString *result = nil;
29+
30+
switch (apiVersion) {
31+
case WordPressComRESTAPIVersion_1_0:
32+
result = WordPressComRESTApiVersionString_1_0;
33+
break;
34+
35+
case WordPressComRESTAPIVersion_1_1:
36+
result = WordPressComRESTApiVersionString_1_1;
37+
break;
38+
39+
case WordPressComRESTAPIVersion_1_2:
40+
result = WordPressComRESTApiVersionString_1_2;
41+
break;
42+
43+
case WordPressComRESTAPIVersion_1_3:
44+
result = WordPressComRESTApiVersionString_1_3;
45+
break;
46+
47+
case WordPressComRESTAPIVersion_2_0:
48+
result = WordPressComRESTApiVersionString_2_0;
49+
break;
50+
51+
default:
52+
NSAssert(NO, @"This should never by executed");
53+
result = WordPressComRESTApiVersionStringInvalid;
54+
break;
55+
}
56+
57+
return result;
58+
}
59+
60+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#import <Foundation/Foundation.h>
2+
#if SWIFT_PACKAGE
3+
#import "WordPressComRESTAPIVersion.h"
4+
#else
5+
#import <WordPressKit/WordPressComRESTAPIVersion.h>
6+
#endif
7+
8+
@interface WordPressComRESTAPIVersionedPathBuilder: NSObject
9+
10+
+ (NSString *)pathForEndpoint:(NSString *)endpoint
11+
withVersion:(WordPressComRESTAPIVersion)apiVersion
12+
NS_SWIFT_NAME(path(forEndpoint:withVersion:));
13+
14+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#import <Foundation/Foundation.h>
2+
3+
/// Error domain of `NSError` instances that are converted from `WordPressComRestApiEndpointError`
4+
/// and `WordPressAPIError<WordPressComRestApiEndpointError>` instances.
5+
///
6+
/// This matches the compiler generated value and is used to ensure consistent error domain across error types and SPM or Framework build modes.
7+
///
8+
/// See `extension WordPressComRestApiEndpointError: CustomNSError` in CoreAPI package for context.
9+
static NSString *const _Nonnull WordPressComRestApiErrorDomain = @"WordPressKit.WordPressComRestApiError";

Sources/WordPressKit/Services/ServiceRemoteWordPressComREST.h renamed to Sources/BasicBlogAPIObjc/ServiceRemoteWordPressComREST.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
1515
/**
1616
* @brief The API object to use for communications.
1717
*/
18+
// TODO: This needs to go before being able to put this ObjC in a package.
1819
@property (nonatomic, strong, readonly) WordPressComRestApi *wordPressComRestApi;
1920

2021
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#import "ServiceRemoteWordPressComREST.h"
2+
#import "WPKit-Swift.h"
3+
4+
@implementation ServiceRemoteWordPressComREST
5+
6+
- (instancetype)initWithWordPressComRestApi:(WordPressComRestApi *)wordPressComRestApi {
7+
8+
NSParameterAssert([wordPressComRestApi isKindOfClass:[WordPressComRestApi class]]);
9+
10+
self = [super init];
11+
if (self) {
12+
_wordPressComRestApi = wordPressComRestApi;
13+
_wordPressComRESTAPI = wordPressComRestApi;
14+
}
15+
return self;
16+
}
17+
18+
#pragma mark - Request URL construction
19+
20+
- (NSString *)pathForEndpoint:(NSString *)resourceUrl
21+
withVersion:(WordPressComRESTAPIVersion)apiVersion
22+
{
23+
NSParameterAssert([resourceUrl isKindOfClass:[NSString class]]);
24+
25+
return [WordPressComRESTAPIVersionedPathBuilder pathForEndpoint:resourceUrl
26+
withVersion:apiVersion];
27+
}
28+
29+
@end

Sources/WordPressKit/WordPressAPI/Date+WordPressCom.swift renamed to Sources/CoreAPI/Date+WordPressCom.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
extension Date {
24

35
/// Parses a date string

Sources/WordPressKit/WordPressAPI/DateFormatter+WordPressCom.swift renamed to Sources/CoreAPI/DateFormatter+WordPressCom.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
extension DateFormatter {
24

35
/// A `DateFormatter` configured to manage dates compatible with the WordPress.com API.

Sources/WordPressKit/WordPressAPI/HTTPAuthenticationAlertController.swift renamed to Sources/CoreAPI/HTTPAuthenticationAlertController.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import UIKit
23

34
/// URLAuthenticationChallenge Handler: It's up to the Host App to actually use this, whenever `WordPressOrgXMLRPCApi.onChallenge` is hit!
45
///

Sources/WordPressKit/Utility/Result+Callback.swift renamed to Sources/CoreAPI/Result+Callback.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extension Swift.Result {
1+
public extension Swift.Result {
22

33
// Notice there are no explicit unit tests for this utility because it is implicitly tested via the consuming code's tests.
44
func execute(onSuccess: (Success) -> Void, onFailure: (Failure) -> Void) {
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Foundation
2+
3+
extension String.Encoding {
4+
/// See: https://www.iana.org/assignments/character-sets/character-sets.xhtml
5+
init?(ianaCharsetName: String) {
6+
let encoding: CFStringEncoding = CFStringConvertIANACharSetNameToEncoding(ianaCharsetName as CFString)
7+
guard encoding != kCFStringEncodingInvalidId,
8+
let builtInEncoding = CFStringBuiltInEncodings(rawValue: encoding)
9+
else {
10+
return nil
11+
}
12+
13+
switch builtInEncoding {
14+
case .macRoman:
15+
self = .macOSRoman
16+
case .windowsLatin1:
17+
self = .windowsCP1252
18+
case .isoLatin1:
19+
self = .isoLatin1
20+
case .nextStepLatin:
21+
self = .nextstep
22+
case .ASCII:
23+
self = .ascii
24+
case .unicode:
25+
self = .unicode
26+
case .UTF8:
27+
self = .utf8
28+
case .nonLossyASCII:
29+
self = .nonLossyASCII
30+
case .UTF16BE:
31+
self = .utf16BigEndian
32+
case .UTF16LE:
33+
self = .utf16LittleEndian
34+
case .UTF32:
35+
self = .utf32
36+
case .UTF32BE:
37+
self = .utf32BigEndian
38+
case .UTF32LE:
39+
self = .utf32LittleEndian
40+
@unknown default:
41+
return nil
42+
}
43+
}
44+
}

Sources/WordPressKit/WordPressAPI/WordPressAPIError+NSErrorBrdige.swift renamed to Sources/CoreAPI/WordPressAPIError+NSErrorBridge.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import Foundation
2+
#if SWIFT_PACKAGE
3+
import APIInterface
4+
#endif
25

36
/// Custom `NSError` bridge implementation.
47
///
@@ -86,8 +89,7 @@ extension WordPressAPIError: CustomNSError {
8689
/// And in cases where additional user info was provided, they need to be carried over to the `NSError` instances.
8790
extension WordPressComRestApiEndpointError: CustomNSError {
8891

89-
// This value is the same as the `WordPressComRestApiErrorDomain` constant generated by Swift compiler.
90-
public static let errorDomain = "WordPressKit.WordPressComRestApiError"
92+
public static let errorDomain = WordPressComRestApiErrorDomain
9193

9294
public var errorCode: Int {
9395
code.rawValue

0 commit comments

Comments
 (0)