Skip to content

Commit

Permalink
Define CoreAPI package (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio authored Apr 16, 2024
2 parents e3ab875 + 6fba3a1 commit 7b624cd
Show file tree
Hide file tree
Showing 80 changed files with 712 additions and 367 deletions.
3 changes: 3 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ swiftlint_version: 0.54.0
parent_config: https://raw.githubusercontent.com/Automattic/swiftlint-config/0f8ab6388bd8d15a04391825ab125f80cfb90704/.swiftlint.yml
remote_timeout: 10.0

excluded:
.build # `swift build` etc. output folder

opt_in_rules:
- overridden_super_call
- discarded_notification_center_observer
Expand Down
122 changes: 122 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"pins" : [
{
"identity" : "alamofire",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Alamofire/Alamofire",
"state" : {
"revision" : "f455c2975872ccd2d9c81594c658af65716e9b9a",
"version" : "5.9.1"
}
},
{
"identity" : "collectionconcurrencykit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/CollectionConcurrencyKit.git",
"state" : {
"revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95",
"version" : "0.2.0"
}
},
{
"identity" : "cryptoswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
"state" : {
"revision" : "7892a123f7e8d0fe62f9f03728b17bbd4f94df5c",
"version" : "1.8.1"
}
},
{
"identity" : "ohhttpstubs",
"kind" : "remoteSourceControl",
"location" : "https://github.com/AliSoftware/OHHTTPStubs",
"state" : {
"revision" : "12f19662426d0434d6c330c6974d53e2eb10ecd9",
"version" : "9.1.0"
}
},
{
"identity" : "sourcekitten",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/SourceKitten.git",
"state" : {
"revision" : "b6dc09ee51dfb0c66e042d2328c017483a1a5d56",
"version" : "0.34.1"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
"version" : "1.2.3"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036",
"version" : "509.0.2"
}
},
{
"identity" : "swiftlint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/SwiftLint",
"state" : {
"revision" : "f17a4f9dfb6a6afb0408426354e4180daaf49cee",
"version" : "0.54.0"
}
},
{
"identity" : "swiftytexttable",
"kind" : "remoteSourceControl",
"location" : "https://github.com/scottrhoyt/SwiftyTextTable.git",
"state" : {
"revision" : "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3",
"version" : "0.9.0"
}
},
{
"identity" : "swxmlhash",
"kind" : "remoteSourceControl",
"location" : "https://github.com/drmohundro/SWXMLHash.git",
"state" : {
"revision" : "a853604c9e9a83ad9954c7e3d2a565273982471f",
"version" : "7.0.2"
}
},
{
"identity" : "wordpress-ios-shared",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wordpress-mobile/WordPress-iOS-Shared.git",
"state" : {
"branch" : "mokagio/swiftlint-read-as-dependency",
"revision" : "422950b28f01d7cc11218e7d70a6cd65004d23ae"
}
},
{
"identity" : "wpxmlrpc",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wordpress-mobile/wpxmlrpc",
"state" : {
"revision" : "bfc413d336bdeaab89e62dc483380baa99b2257e",
"version" : "0.10.0"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "8a835d918245ca22f36663dd3862138805d7f707",
"version" : "5.1.0"
}
}
],
"version" : 2
}
40 changes: 37 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,46 @@ import PackageDescription

let package = Package(
name: "WordPressKit",
platforms: [.iOS(.v13)],
platforms: [
.iOS(.v13),
// 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.
.macOS(.v12),
],
products: [
.library(name: "APIInterface", targets: ["APIInterface"]),
.library(name: "CoreAPI", targets: ["CoreAPI"]),
],
dependencies: [
// .package(url: "https://github.com/wordpress-mobile/WordPress-iOS-Shared.git", from: "2.3.1"),
// See https://github.com/wordpress-mobile/WordPress-iOS-Shared/pull/354
.package(url: "https://github.com/wordpress-mobile/WordPress-iOS-Shared.git", branch: "mokagio/swiftlint-read-as-dependency"),
.package(url: "https://github.com/wordpress-mobile/wpxmlrpc", from: "0.10.0"),
// Test dependencies
.package(url: "https://github.com/AliSoftware/OHHTTPStubs", from: "9.1.0"),
.package(url: "https://github.com/Alamofire/Alamofire", from: "5.8.1"),
],
dependencies: [],
targets: [
.target(name: "APIInterface")
.target(name: "APIInterface"),
.target(
name: "CoreAPI",
dependencies: [
.target(name: "APIInterface"),
.product(name: "WordPressShared", package: "WordPress-iOS-Shared"),
"wpxmlrpc"
]
),
.testTarget(
name: "CoreAPITests",
dependencies: [
.target(name: "CoreAPI"),
.product(name: "OHHTTPStubs", package: "OHHTTPStubs"),
.product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs"),
"Alamofire",
],
path: "Tests/CoreAPITests",
resources: [
.process("Stubs") // Relative to path
]
),
]
)
60 changes: 60 additions & 0 deletions Sources/APIInterface/WordPressComRESTAPIVersionedPathBuilder.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#import <Foundation/Foundation.h>
#if SWIFT_PACKAGE
#import "WordPressComRESTAPIVersionedPathBuilder.h"
#import "WordPressComRESTAPIVersion.h"
#else
#import "WordPressKit/WordPressComRESTAPIVersionedPathBuilder.h"
#endif

static NSString* const WordPressComRESTApiVersionStringInvalid = @"invalid_api_version";
static NSString* const WordPressComRESTApiVersionString_1_0 = @"rest/v1";
static NSString* const WordPressComRESTApiVersionString_1_1 = @"rest/v1.1";
static NSString* const WordPressComRESTApiVersionString_1_2 = @"rest/v1.2";
static NSString* const WordPressComRESTApiVersionString_1_3 = @"rest/v1.3";
static NSString* const WordPressComRESTApiVersionString_2_0 = @"wpcom/v2";

@implementation WordPressComRESTAPIVersionedPathBuilder

+ (NSString *)pathForEndpoint:(NSString *)endpoint
withVersion:(WordPressComRESTAPIVersion)apiVersion
{
NSString *apiVersionString = [self apiVersionStringWithEnumValue:apiVersion];

return [NSString stringWithFormat:@"%@/%@", apiVersionString, endpoint];
}

+ (NSString *)apiVersionStringWithEnumValue:(WordPressComRESTAPIVersion)apiVersion
{
NSString *result = nil;

switch (apiVersion) {
case WordPressComRESTAPIVersion_1_0:
result = WordPressComRESTApiVersionString_1_0;
break;

case WordPressComRESTAPIVersion_1_1:
result = WordPressComRESTApiVersionString_1_1;
break;

case WordPressComRESTAPIVersion_1_2:
result = WordPressComRESTApiVersionString_1_2;
break;

case WordPressComRESTAPIVersion_1_3:
result = WordPressComRESTApiVersionString_1_3;
break;

case WordPressComRESTAPIVersion_2_0:
result = WordPressComRESTApiVersionString_2_0;
break;

default:
NSAssert(NO, @"This should never by executed");
result = WordPressComRESTApiVersionStringInvalid;
break;
}

return result;
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <Foundation/Foundation.h>
#if SWIFT_PACKAGE
#import "WordPressComRESTAPIVersion.h"
#else
#import <WordPressKit/WordPressComRESTAPIVersion.h>
#endif

@interface WordPressComRESTAPIVersionedPathBuilder: NSObject

+ (NSString *)pathForEndpoint:(NSString *)endpoint
withVersion:(WordPressComRESTAPIVersion)apiVersion
NS_SWIFT_NAME(path(forEndpoint:withVersion:));

@end
9 changes: 9 additions & 0 deletions Sources/APIInterface/include/WordPressComRestApiErrorDomain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <Foundation/Foundation.h>

/// Error domain of `NSError` instances that are converted from `WordPressComRestApiEndpointError`
/// and `WordPressAPIError<WordPressComRestApiEndpointError>` instances.
///
/// This matches the compiler generated value and is used to ensure consistent error domain across error types and SPM or Framework build modes.
///
/// See `extension WordPressComRestApiEndpointError: CustomNSError` in CoreAPI package for context.
static NSString *const _Nonnull WordPressComRestApiErrorDomain = @"WordPressKit.WordPressComRestApiError";
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
* @brief The API object to use for communications.
*/
// TODO: This needs to go before being able to put this ObjC in a package.
@property (nonatomic, strong, readonly) WordPressComRestApi *wordPressComRestApi;

/**
Expand Down
29 changes: 29 additions & 0 deletions Sources/BasicBlogAPIObjc/ServiceRemoteWordPressComREST.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#import "ServiceRemoteWordPressComREST.h"
#import "WPKit-Swift.h"

@implementation ServiceRemoteWordPressComREST

- (instancetype)initWithWordPressComRestApi:(WordPressComRestApi *)wordPressComRestApi {

NSParameterAssert([wordPressComRestApi isKindOfClass:[WordPressComRestApi class]]);

self = [super init];
if (self) {
_wordPressComRestApi = wordPressComRestApi;
_wordPressComRESTAPI = wordPressComRestApi;
}
return self;
}

#pragma mark - Request URL construction

- (NSString *)pathForEndpoint:(NSString *)resourceUrl
withVersion:(WordPressComRESTAPIVersion)apiVersion
{
NSParameterAssert([resourceUrl isKindOfClass:[NSString class]]);

return [WordPressComRESTAPIVersionedPathBuilder pathForEndpoint:resourceUrl
withVersion:apiVersion];
}

@end
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Foundation

extension Date {

/// Parses a date string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Foundation

extension DateFormatter {

/// A `DateFormatter` configured to manage dates compatible with the WordPress.com API.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import UIKit

/// URLAuthenticationChallenge Handler: It's up to the Host App to actually use this, whenever `WordPressOrgXMLRPCApi.onChallenge` is hit!
///
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extension Swift.Result {
public extension Swift.Result {

// Notice there are no explicit unit tests for this utility because it is implicitly tested via the consuming code's tests.
func execute(onSuccess: (Success) -> Void, onFailure: (Failure) -> Void) {
Expand Down
44 changes: 44 additions & 0 deletions Sources/CoreAPI/StringEncoding+IANA.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Foundation

extension String.Encoding {
/// See: https://www.iana.org/assignments/character-sets/character-sets.xhtml
init?(ianaCharsetName: String) {
let encoding: CFStringEncoding = CFStringConvertIANACharSetNameToEncoding(ianaCharsetName as CFString)
guard encoding != kCFStringEncodingInvalidId,
let builtInEncoding = CFStringBuiltInEncodings(rawValue: encoding)
else {
return nil
}

switch builtInEncoding {
case .macRoman:
self = .macOSRoman
case .windowsLatin1:
self = .windowsCP1252
case .isoLatin1:
self = .isoLatin1
case .nextStepLatin:
self = .nextstep
case .ASCII:
self = .ascii
case .unicode:
self = .unicode
case .UTF8:
self = .utf8
case .nonLossyASCII:
self = .nonLossyASCII
case .UTF16BE:
self = .utf16BigEndian
case .UTF16LE:
self = .utf16LittleEndian
case .UTF32:
self = .utf32
case .UTF32BE:
self = .utf32BigEndian
case .UTF32LE:
self = .utf32LittleEndian
@unknown default:
return nil
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Foundation
#if SWIFT_PACKAGE
import APIInterface
#endif

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

// This value is the same as the `WordPressComRestApiErrorDomain` constant generated by Swift compiler.
public static let errorDomain = "WordPressKit.WordPressComRestApiError"
public static let errorDomain = WordPressComRestApiErrorDomain

public var errorCode: Int {
code.rawValue
Expand Down
File renamed without changes.
Loading

0 comments on commit 7b624cd

Please sign in to comment.