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 pathJetpackBackupServiceRemote.swift
More file actions
130 lines (113 loc) · 4.68 KB
/
Copy pathJetpackBackupServiceRemote.swift
File metadata and controls
130 lines (113 loc) · 4.68 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
import Foundation
import WordPressShared
open class JetpackBackupServiceRemote: ServiceRemoteWordPressComREST {
/// Prepare a downloadable backup snapshot for a site.
///
/// - Parameters:
/// - siteID: The target site's ID.
/// - rewindID: The rewindID of the snapshot to download.
/// - types: The types of items to restore.
/// - success: Closure to be executed on success.
/// - failure: Closure to be executed on error.
///
/// - Returns: A backup snapshot object.
///
open func prepareBackup(_ siteID: Int,
rewindID: String? = nil,
types: JetpackRestoreTypes? = nil,
success: @escaping (_ backup: JetpackBackup) -> Void,
failure: @escaping (Error) -> Void) {
let path = backupPath(for: siteID)
var parameters: [String: AnyObject] = [:]
if let rewindID = rewindID {
parameters["rewindId"] = rewindID as AnyObject
}
if let types = types {
parameters["types"] = types.toDictionary() as AnyObject
}
wordPressComRestApi.POST(path, parameters: parameters) { result in
result
.flatMap { data, _ in
Result {
try JSONDecoder.apiDecoder.decode(JetpackBackup.self, from: data)
}
}
.invoke(success, or: failure)
}
}
/// Get the backup download status for a site and downloadID.
/// - Parameters:
/// - siteID: The target site's ID.
/// - downloadID: The download ID of the snapshot being downloaded.
/// - success: Closure to be executed on success.
/// - failure: Closure to be executed on error.
///
/// - Returns: A backup snapshot object.
///
open func getBackupStatus(_ siteID: Int,
downloadID: Int,
success: @escaping (_ backup: JetpackBackup) -> Void,
failure: @escaping (Error) -> Void) {
getDownloadStatus(siteID, downloadID: downloadID, success: success, failure: failure)
}
/// Get the backup status for all the backups in a site.
/// - Parameters:
/// - siteID: The target site's ID.
/// - success: Closure to be executed on success.
/// - failure: Closure to be executed on error.
///
/// - Returns: A backup snapshot object.
///
open func getAllBackupStatus(_ siteID: Int,
success: @escaping (_ backup: [JetpackBackup]) -> Void,
failure: @escaping (Error) -> Void) {
getDownloadStatus(siteID, success: success, failure: failure)
}
/// Mark a backup as dismissed
/// - Parameters:
/// - siteID: The target site's ID.
/// - downloadID: The download ID of the snapshot being downloaded.
/// - success: Closure to be executed on success.
/// - failure: Closure to be executed on error.
///
open func markAsDismissed(_ siteID: Int,
downloadID: Int,
success: @escaping () -> Void,
failure: @escaping (Error) -> Void) {
let path = backupPath(for: siteID, with: "\(downloadID)")
let parameters = ["dismissed": true] as [String: AnyObject]
wordPressComRestApi.POST(path, parameters: parameters, success: { _, _ in
success()
}, failure: { error, _ in
failure(error)
})
}
// MARK: - Private
private func getDownloadStatus<T: Decodable>(_ siteID: Int,
downloadID: Int? = nil,
success: @escaping (_ backup: T) -> Void,
failure: @escaping (Error) -> Void) {
let path: String
if let downloadID = downloadID {
path = backupPath(for: siteID, with: "\(downloadID)")
} else {
path = backupPath(for: siteID)
}
wordPressComRestApi.GETData(path, parameters: nil) { result in
result
.flatMap { data, _ in
Result {
try JSONDecoder.apiDecoder.decode(T.self, from: data)
}
}
.invoke(success, or: failure)
}
}
private func backupPath(for siteID: Int, with path: String? = nil) -> String {
var endpoint = "sites/\(siteID)/rewind/downloads/"
if let path = path {
endpoint = endpoint.appending(path)
}
return self.path(forEndpoint: endpoint, withVersion: ._2_0)
}
}