Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 37 additions & 3 deletions
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
)
Lines changed: 60 additions & 0 deletions
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
Lines changed: 14 additions & 0 deletions
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
Lines changed: 9 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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
/**
Lines changed: 29 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

0 commit comments

Comments
 (0)