-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathRESTRequest.swift
108 lines (96 loc) · 3.48 KB
/
RESTRequest.swift
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
import Foundation
import Alamofire
/// Represents a WordPress.org REST API request
///
struct RESTRequest: Request {
/// URL of the site to make the request with
///
let siteURL: String
/// WooCommerce / WordPress API Version Path
///
let apiVersionPath: String?
/// HTTP Request Method
///
let method: HTTPMethod
/// RPC
///
let path: String
/// Parameters
///
let parameters: [String: Any]?
private init(siteURL: String,
apiVersionPath: String?,
method: HTTPMethod,
path: String,
parameters: [String: Any]? = nil) {
self.siteURL = siteURL
self.apiVersionPath = apiVersionPath
self.method = method
self.path = path
self.parameters = parameters
}
/// - Parameters:
/// - siteURL: URL of the site to send the REST request to.
/// - method: HTTP Method we should use.
/// - path: path to the target endpoint.
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
///
init(siteURL: String,
method: HTTPMethod,
path: String,
parameters: [String: Any]? = nil) {
self.init(siteURL: siteURL, apiVersionPath: nil, method: method, path: path, parameters: parameters)
}
/// - Parameters:
/// - siteURL: URL of the site to send the REST request to.
/// - wooApiVersion: WooCommerce API version.
/// - method: HTTP Method we should use.
/// - path: path to the target endpoint.
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
///
init(siteURL: String,
wooApiVersion: WooAPIVersion,
method: HTTPMethod,
path: String,
parameters: [String: Any]? = nil) {
self.init(siteURL: siteURL, apiVersionPath: wooApiVersion.path, method: method, path: path, parameters: parameters)
}
/// - Parameters:
/// - siteURL: URL of the site to send the REST request to.
/// - wordpressApiVersion: WordPress API version.
/// - method: HTTP Method we should use.
/// - path: path to the target endpoint.
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
///
init(siteURL: String,
wordpressApiVersion: WordPressAPIVersion,
method: HTTPMethod,
path: String,
parameters: [String: Any]? = nil) {
self.init(siteURL: siteURL, apiVersionPath: wordpressApiVersion.path, method: method, path: path, parameters: parameters)
}
/// Returns a URLRequest instance representing the current REST API Request.
///
func asURLRequest() throws -> URLRequest {
let components = [siteURL, Settings.basePath, apiVersionPath, path]
.compactMap { $0 }
.map { $0.trimSlashes() }
.filter { $0.isEmpty == false }
let url = try components.joined(separator: "/").asURL()
let request = try URLRequest(url: url, method: method)
switch method {
case .post, .put:
return try JSONEncoding.default.encode(request, with: parameters)
default:
return try URLEncoding.default.encode(request, with: parameters)
}
}
func responseDataValidator() -> ResponseDataValidator {
PlaceholderDataValidator()
}
}
extension RESTRequest {
enum Settings {
static let basePath = "?rest_route="
}
}