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

Experiment - Move WordPressKit code used only by this app into this app #22690

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ end

def wordpress_kit
# pod 'WordPressKit', '~> 13.1'
pod 'WordPressKit', git: 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', commit: 'f335202c98b972dd2a426dfe0e06780a38579ee7'
pod 'WordPressKit', git: 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', commit: 'd6dce6de4277a7e2046fef76c71de570d2a8acd0'
# pod 'WordPressKit', git: 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', branch: ''
# pod 'WordPressKit', git: 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', tag: ''
# pod 'WordPressKit', path: '../WordPressKit-iOS'
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ DEPENDENCIES:
- SwiftLint (~> 0.50)
- WordPress-Editor-iOS (~> 1.19.9)
- WordPressAuthenticator (>= 9.0.1, ~> 9.0)
- WordPressKit (from `https://github.com/wordpress-mobile/WordPressKit-iOS.git`, commit `f335202c98b972dd2a426dfe0e06780a38579ee7`)
- WordPressKit (from `https://github.com/wordpress-mobile/WordPressKit-iOS.git`, commit `d6dce6de4277a7e2046fef76c71de570d2a8acd0`)
- WordPressShared (~> 2.3)
- WordPressUI (~> 1.15)
- ZendeskSupportSDK (= 5.3.0)
Expand Down Expand Up @@ -176,15 +176,15 @@ EXTERNAL SOURCES:
Gutenberg:
:podspec: https://cdn.a8c-ci.services/gutenberg-mobile/Gutenberg-v1.112.0.podspec
WordPressKit:
:commit: f335202c98b972dd2a426dfe0e06780a38579ee7
:commit: d6dce6de4277a7e2046fef76c71de570d2a8acd0
:git: https://github.com/wordpress-mobile/WordPressKit-iOS.git

CHECKOUT OPTIONS:
FSInteractiveMap:
:git: https://github.com/wordpress-mobile/FSInteractiveMap.git
:tag: 0.2.0
WordPressKit:
:commit: f335202c98b972dd2a426dfe0e06780a38579ee7
:commit: d6dce6de4277a7e2046fef76c71de570d2a8acd0
:git: https://github.com/wordpress-mobile/WordPressKit-iOS.git

SPEC CHECKSUMS:
Expand Down Expand Up @@ -231,6 +231,6 @@ SPEC CHECKSUMS:
ZendeskSupportSDK: 3a8e508ab1d9dd22dc038df6c694466414e037ba
ZIPFoundation: d170fa8e270b2a32bef9dcdcabff5b8f1a5deced

PODFILE CHECKSUM: c7bf9c542367e6ea6b245cc378ee3dd43a7bffef
PODFILE CHECKSUM: 19df34fbf5bebd8ac33562114ad3a647e716be60

COCOAPODS: 1.14.2
406 changes: 406 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@objc public class ActivityServiceRemote_ApiVersion1_0: ServiceRemoteWordPressComREST {

public enum ResponseError: Error {
case decodingFailure
}

/// Makes a request to Restore a site to a previous state.
///
/// - Parameters:
/// - siteID: The target site's ID.
/// - rewindID: The rewindID to restore to.
/// - types: The types of items to restore.
/// - success: Closure to be executed on success
/// - failure: Closure to be executed on error.
///
/// - Returns: A restoreID and jobID to check the status of the rewind request.
///
public func restoreSite(_ siteID: Int,
rewindID: String,
types: JetpackRestoreTypes? = nil,
success: @escaping (_ restoreID: String, _ jobID: Int) -> Void,
failure: @escaping (Error) -> Void) {
let endpoint = "activity-log/\(siteID)/rewind/to/\(rewindID)"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_0)
var parameters: [String: AnyObject] = [:]

if let types = types {
parameters["types"] = types.toDictionary() as AnyObject
}

wordPressComRestApi.POST(path,
parameters: parameters,
success: { response, _ in
guard let restoreID = response["restore_id"] as? Int,
let jobID = response["job_id"] as? Int else {
failure(ResponseError.decodingFailure)
return
}
success(String(restoreID), jobID)
},
failure: { error, _ in
failure(error)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
public struct ValidateDomainContactInformationResponse: Codable {
public struct Messages: Codable {
public var phone: [String]?
public var email: [String]?
public var postalCode: [String]?
public var countryCode: [String]?
public var city: [String]?
public var address1: [String]?
public var address2: [String]?
public var firstName: [String]?
public var lastName: [String]?
public var state: [String]?
public var organization: [String]?
}

public var success: Bool = false
public var messages: Messages?

/// Returns true if any of the properties within `messages` has a value.
///
public var hasMessages: Bool {
if let messages = messages {
let mirror = Mirror(reflecting: messages)

for child in mirror.children {
let childMirror = Mirror(reflecting: child.value)

if childMirror.displayStyle == .optional,
let _ = childMirror.children.first {
return true
}
}
}

return false
}

public init() {
}
}

public struct DomainContactInformation: Codable {
public var phone: String?
public var email: String?
public var postalCode: String?
public var countryCode: String?
public var city: String?
public var address1: String?
public var firstName: String?
public var lastName: String?
public var fax: String?
public var state: String?
public var organization: String?

public init() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import Foundation

extension DomainsServiceRemote {

// MARK: - API

/// Makes a call request to `GET /v1.1/all-domains` and returns a list of domain objects.
///
/// The endpoint accepts 3 **optionals** query params:
/// - `resolve_status` of type `boolean`. If `true`, the response will include a `status` attribute for each `domain` object.
/// - `no_wpcom`of type `boolean`. If `true`, the respnse won't include `wpcom` domains.
/// - `locale` of type `string`. Used for string localization.
public func fetchAllDomains(params: AllDomainsEndpointParams? = nil, completion: @escaping (AllDomainsEndpointResult) -> Void) {
let path = self.path(forEndpoint: "all-domains", withVersion: ._1_1)
let parameters: [String: AnyObject]?

do {
parameters = try queryParameters(from: params)
} catch let error {
completion(.failure(error))
return
}

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
Task { @MainActor in
await self.wordPressComRestApi
.perform(
.get,
URLString: path,
parameters: parameters,
jsonDecoder: decoder,
type: AllDomainsEndpointResponse.self
)
.map { $0.body.domains }
.mapError { error -> Error in error.asNSError() }
.execute(completion)
}
}

private func queryParameters(from params: AllDomainsEndpointParams?) throws -> [String: AnyObject]? {
guard let params else {
return nil
}
let encoder = JSONEncoder()
let data = try encoder.encode(params)
let dict = try JSONSerialization.jsonObject(with: data) as? [String: AnyObject]
return dict
}

// MARK: - Public Types

public typealias AllDomainsEndpointResult = Result<[AllDomainsListItem], Error>

public struct AllDomainsEndpointParams {

public var resolveStatus: Bool = false
public var noWPCOM: Bool = false
public var locale: String?

public init() {}
}

public struct AllDomainsListItem {

public enum StatusType: String {
case success
case premium
case neutral
case warning
case alert
case error
}

public struct Status {

public let value: String
public let type: StatusType

public init(value: String, type: StatusType) {
self.value = value
self.type = type
}
}

public let domain: String
public let blogId: Int
public let blogName: String
public let type: DomainType
public let isDomainOnlySite: Bool
public let isWpcomStagingDomain: Bool
public let hasRegistration: Bool
public let registrationDate: Date?
public let expiryDate: Date?
public let wpcomDomain: Bool
public let currentUserIsOwner: Bool?
public let siteSlug: String
public let status: Status?
}

// MARK: - Private Types

private struct AllDomainsEndpointResponse: Decodable {
let domains: [AllDomainsListItem]
}
}

// MARK: - Encoding / Decoding

extension DomainsServiceRemote.AllDomainsEndpointParams: Encodable {

enum CodingKeys: String, CodingKey {
case resolveStatus = "resolve_status"
case locale
case noWPCOM = "no_wpcom"
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode("\(resolveStatus)", forKey: .resolveStatus)
try container.encode("\(noWPCOM)", forKey: .noWPCOM)
try container.encodeIfPresent(locale, forKey: .locale)
}
}

extension DomainsServiceRemote.AllDomainsListItem.StatusType: Decodable {
}

extension DomainsServiceRemote.AllDomainsListItem.Status: Decodable {
enum CodingKeys: String, CodingKey {
case value = "status"
case type = "status_type"
}
}

extension DomainsServiceRemote.AllDomainsListItem: Decodable {

enum CodingKeys: String, CodingKey {
case domain
case blogId = "blog_id"
case blogName = "blog_name"
case type
case isDomainOnlySite = "is_domain_only_site"
case isWpcomStagingDomain = "is_wpcom_staging_domain"
case hasRegistration = "has_registration"
case registrationDate = "registration_date"
case expiryDate = "expiry"
case wpcomDomain = "wpcom_domain"
case currentUserIsOwner = "current_user_is_owner"
case siteSlug = "site_slug"
case status = "domain_status"
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.domain = try container.decode(String.self, forKey: .domain)
self.blogId = try container.decode(Int.self, forKey: .blogId)
self.blogName = try container.decode(String.self, forKey: .blogName)
self.isDomainOnlySite = try container.decode(Bool.self, forKey: .isDomainOnlySite)
self.isWpcomStagingDomain = try container.decode(Bool.self, forKey: .isWpcomStagingDomain)
self.hasRegistration = try container.decode(Bool.self, forKey: .hasRegistration)
self.wpcomDomain = try container.decode(Bool.self, forKey: .wpcomDomain)
self.currentUserIsOwner = try container.decode(Bool?.self, forKey: .currentUserIsOwner)
self.siteSlug = try container.decode(String.self, forKey: .siteSlug)
self.registrationDate = try {
if let timestamp = try? container.decodeIfPresent(String.self, forKey: .registrationDate), !timestamp.isEmpty {
return try container.decode(Date.self, forKey: .registrationDate)
}
return nil
}()
self.expiryDate = try {
if let timestamp = try? container.decodeIfPresent(String.self, forKey: .expiryDate), !timestamp.isEmpty {
return try container.decode(Date.self, forKey: .expiryDate)
}
return nil
}()
let type: String = try container.decode(String.self, forKey: .type)
self.type = .init(type: type, wpComDomain: wpcomDomain, hasRegistration: hasRegistration)
self.status = try container.decodeIfPresent(Status.self, forKey: .status)
}
}
Loading