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 pathWordPressComServiceRemote+SiteVerticalsPrompt.swift
More file actions
86 lines (67 loc) · 2.72 KB
/
Copy pathWordPressComServiceRemote+SiteVerticalsPrompt.swift
File metadata and controls
86 lines (67 loc) · 2.72 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
import Foundation
// MARK: - Site Verticals Prompt : Request
public typealias SiteVerticalsPromptRequest = Int64
// MARK: - Site Verticals Prompt : Response
public struct SiteVerticalsPrompt: Decodable {
public let title: String
public let subtitle: String
public let hint: String
public init(title: String, subtitle: String, hint: String) {
self.title = title
self.subtitle = subtitle
self.hint = hint
}
private enum CodingKeys: String, CodingKey {
case title = "site_topic_header"
case subtitle = "site_topic_subheader"
case hint = "site_topic_placeholder"
}
}
public typealias SiteVerticalsPromptServiceCompletion = ((SiteVerticalsPrompt?) -> Void)
/// Site verticals services, exclusive to WordPress.com.
///
public extension WordPressComServiceRemote {
/// Retrieves the prompt information presented to users when searching Verticals.
///
/// - Parameters:
/// - request: the value object with which to compose the request.
/// - completion: a closure including the result of the request for site verticals.
///
func retrieveVerticalsPrompt(request: SiteVerticalsPromptRequest, completion: @escaping SiteVerticalsPromptServiceCompletion) {
let endpoint = "verticals/prompt"
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)
let requestParameters: [String: AnyObject] = [
"segment_id": request as AnyObject
]
wordPressComRESTAPI.get(
path,
parameters: requestParameters,
success: { [weak self] responseObject, httpResponse in
WPKitLogInfo("\(responseObject) | \(String(describing: httpResponse))")
guard let self = self else {
return
}
do {
let response = try self.decodeResponse(responseObject: responseObject)
completion(response)
} catch {
WPKitLogError("Failed to decode SiteVerticalsPrompt : \(error.localizedDescription)")
completion(nil)
}
},
failure: { error, httpResponse in
WPKitLogError("\(error) | \(String(describing: httpResponse))")
completion(nil)
})
}
}
// MARK: - Serialization support
//
private extension WordPressComServiceRemote {
func decodeResponse(responseObject: Any) throws -> SiteVerticalsPrompt {
let decoder = JSONDecoder()
let data = try JSONSerialization.data(withJSONObject: responseObject, options: [])
let response = try decoder.decode(SiteVerticalsPrompt.self, from: data)
return response
}
}