This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAutomatedTransferService.swift
More file actions
136 lines (114 loc) · 5.1 KB
/
Copy pathAutomatedTransferService.swift
File metadata and controls
136 lines (114 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import Foundation
/// Class encapsualting all requests related to performing Automated Transfer operations.
public class AutomatedTransferService: ServiceRemoteWordPressComREST {
@frozen public enum ResponseError: Error {
case decodingFailure
}
@frozen public enum AutomatedTransferEligibilityError: Error {
case unverifiedEmail
case excessiveDiskSpaceUsage
case noBusinessPlan
case VIPSite
case notAdmin
case notDomainOwner
case noCustomDomain
case greylistedSite
case privateSite
case unknown
}
public func checkTransferEligibility(siteID: Int,
success: @escaping () -> Void,
failure: @escaping (AutomatedTransferEligibilityError) -> Void) {
let endpoint = "sites/\(siteID)/automated-transfers/eligibility"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
wordPressComRESTAPI.get(path, parameters: nil, success: { (responseObject, _) in
guard let response = responseObject as? [String: AnyObject] else {
failure(.unknown)
return
}
guard let isEligible = response["is_eligible"] as? Bool, isEligible == true else {
failure(self.eligibilityError(from: response))
return
}
success()
}, failure: { _, _ in
failure(.unknown)
})
}
public typealias AutomatedTransferInitationResponse = (transferID: Int, status: AutomatedTransferStatus)
public func initiateAutomatedTransfer(siteID: Int,
pluginSlug: String,
success: @escaping (AutomatedTransferInitationResponse) -> Void,
failure: @escaping (Error) -> Void) {
let endpoint = "sites/\(siteID)/automated-transfers/initiate"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let payload = ["plugin": pluginSlug] as [String: AnyObject]
wordPressComRESTAPI.post(path, parameters: payload, success: { (responseObject, _) in
guard let response = responseObject as? [String: AnyObject] else {
failure(ResponseError.decodingFailure)
return
}
guard let transferID = response["transfer_id"] as? Int,
let status = response["status"] as? String,
let statusObject = AutomatedTransferStatus(status: status) else {
failure(ResponseError.decodingFailure)
return
}
success((transferID: transferID, status: statusObject))
}) { (error, _) in
failure(error)
}
}
public func fetchAutomatedTransferStatus(siteID: Int,
success: @escaping (AutomatedTransferStatus) -> Void,
failure: @escaping (Error) -> Void) {
let endpoint = "sites/\(siteID)/automated-transfers/status"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
wordPressComRESTAPI.get(path, parameters: nil, success: { (responseObject, _) in
guard let response = responseObject as? [String: AnyObject] else {
failure(ResponseError.decodingFailure)
return
}
guard let status = response["status"] as? String,
let currentStep = response["step"] as? Int,
let totalSteps = response["total"] as? Int,
let statusObject = AutomatedTransferStatus(status: status, step: currentStep, totalSteps: totalSteps) else {
failure(ResponseError.decodingFailure)
return
}
success(statusObject)
}) { (error, _) in
failure(error)
}
}
private func eligibilityError(from response: [String: AnyObject]) -> AutomatedTransferEligibilityError {
guard let errors = response["errors"] as? [[String: AnyObject]],
let errorType = errors.first?["code"] as? String else {
// The API can potentially return multiple errors here. Since there isn't really an actionable
// way for user to deal with multiple of them at once, we're just picking the first one.
return .unknown
}
switch errorType {
case "email_unverified":
return .unverifiedEmail
case "excessive_disk_space":
return .excessiveDiskSpaceUsage
case "no_business_plan":
return .noBusinessPlan
case "no_vip_sites":
return .VIPSite
case "non_admin_user":
return .notAdmin
case "not_domain_owner":
return .notDomainOwner
case "not_using_custom_domain":
return .noCustomDomain
case "site_graylisted":
return .greylistedSite
case "site_private":
return .privateSite
default:
return .unknown
}
}
}