Skip to content

Commit bdfcd5e

Browse files
Reuse CIOBrowseResponse and BrowseResponseParser from outside of the SDK (#256)
1 parent 859f71b commit bdfcd5e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

AutocompleteClient/FW/API/Parser/Browse/BrowseResponseParser.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ import Foundation
1010

1111
class BrowseResponseParser: AbstractBrowseResponseParser {
1212
func parse(browseResponseData: Data) throws -> CIOBrowseResponse {
13-
1413
do {
1514
let json = try JSONSerialization.jsonObject(with: browseResponseData) as? JSONObject
16-
17-
guard let response = json?["response"] as? JSONObject else {
15+
16+
return try jsonObjectToCIOBrowseResponse(dictionary: json)
17+
} catch {
18+
throw CIOError(errorType: .invalidResponse)
19+
}
20+
}
21+
22+
func jsonObjectToCIOBrowseResponse(dictionary: JSONObject?) throws -> CIOBrowseResponse {
23+
do {
24+
guard let response = dictionary?["response"] as? JSONObject else {
1825
throw CIOError(errorType: .invalidResponse)
1926
}
2027

@@ -30,11 +37,11 @@ class BrowseResponseParser: AbstractBrowseResponseParser {
3037
let groups: [CIOFilterGroup] = groupsObj?.compactMap({ obj in return CIOFilterGroup(json: obj) }) ?? []
3138
let totalNumResults = response["total_num_results"] as? Int ?? 0
3239
let collection: CIOCollectionData? = CIOCollectionData(json: response["collection"] as? JSONObject)
33-
let resultID = json?["result_id"] as? String ?? ""
40+
let resultID = dictionary?["result_id"] as? String ?? ""
3441
let resultSources: CIOResultSources? = CIOResultSources(json: response["result_sources"] as? JSONObject)
3542
let refinedContent: [CIORefinedContent] = refinedContentObj?.compactMap({ obj in return CIORefinedContent(json: obj) }) ?? []
3643

37-
guard let request: JSONObject = json?["request"] as? JSONObject else {
44+
guard let request: JSONObject = dictionary?["request"] as? JSONObject else {
3845
throw CIOError(errorType: .invalidResponse)
3946
}
4047

@@ -53,6 +60,5 @@ class BrowseResponseParser: AbstractBrowseResponseParser {
5360
} catch {
5461
throw CIOError(errorType: .invalidResponse)
5562
}
56-
5763
}
5864
}

AutocompleteClient/FW/Logic/Result/Responses/CIOBrowseResponse.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ public struct CIOBrowseResponse {
6262
*/
6363
public var request: JSONObject
6464
}
65+
66+
extension CIOBrowseResponse {
67+
public static func response(from JSON: JSONObject) throws -> Self {
68+
try BrowseResponseParser().jsonObjectToCIOBrowseResponse(dictionary: JSON)
69+
}
70+
}

0 commit comments

Comments
 (0)