-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseEndpoint.swift
More file actions
38 lines (33 loc) · 1.02 KB
/
BaseEndpoint.swift
File metadata and controls
38 lines (33 loc) · 1.02 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
import ExFigCore
import Foundation
#if os(Linux)
import FoundationNetworking
#endif
/// Base Endpoint for application remote resource.
///
/// Contains shared logic for all endpoints in app.
protocol BaseEndpoint: Endpoint where Content: Decodable {
/// Content wrapper.
associatedtype Root: Decodable = Content
/// Extract content from root.
func content(from root: Root) -> Content
}
extension BaseEndpoint where Root == Content {
func content(from root: Root) -> Content {
root
}
}
extension BaseEndpoint {
public func content(from response: URLResponse?, with body: Data) throws -> Content {
do {
// Models use explicit CodingKeys for snake_case mapping
let resource = try JSONCodec.decode(Root.self, from: body)
return content(from: resource)
} catch let mainError {
if let error = try? JSONCodec.decode(FigmaClientError.self, from: body) {
throw error
}
throw mainError
}
}
}