-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndpoint.swift
80 lines (67 loc) · 2.77 KB
/
Endpoint.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
import Foundation
/// A type to represent SWAPI endpoints.
public enum Endpoint: String, CaseIterable {
case root, films, people, planets, species, starships, vehicles
/// The `URL` including the path of the endpoint.
public var baseURL: URL {
switch self {
case .root: return URL(string: "https://swapi.dev/api/")!
case .films: return URL(string: "https://swapi.dev/api/films/")!
case .people: return URL(string: "https://swapi.dev/api/people/")!
case .planets: return URL(string: "https://swapi.dev/api/planets/")!
case .species: return URL(string: "https://swapi.dev/api/species/")!
case .starships: return URL(string: "https://swapi.dev/api/starships/")!
case .vehicles: return URL(string: "https://swapi.dev/api/vehicles/")!
}
}
/// Constructs a `URL` from this endpoint and a given id.
///
/// - Parameter id: The id of the resource.
/// - Returns: The constructed `URL` for the resources.
public func itemURL(id: Int) -> URL {
return baseURL.appendingPathComponent("\(id)/")
}
/// Constructs a `URL` from this endpoint and a given page.
///
/// - Parameter id: The page for the resources.
/// - Returns: The constructed `URL` for the resources.
public func pageURL(page: Int) -> URL {
assert(self != .root)
return baseURL.appendingPathComponent("?page=\(page)")
}
/// Constructs a `URL` from this endpoint and a given search term.
///
/// - Parameter search: The search term to find resources.
/// - Returns: The constructed `URL` for the resources.
public func searchURL(search: String) -> URL {
assert(self != .root)
return baseURL.appendingPathComponent("?search=\(search)")
}
}
extension URL {
enum EndpointStyle {
case none, root, resource, item, page, search
}
var endpoint: Endpoint? {
guard Endpoint.root.baseURL != self else { return .root }
return Endpoint.allCases.filter { $0 != .root }.first { self.absoluteString.contains($0.baseURL.absoluteString) }
}
var endpointStyle: EndpointStyle {
guard let endpoint = endpoint else { return .none }
switch endpoint {
case .root: return .root
default:
if absoluteString.contains("?page=") { return .page }
if absoluteString.contains("?search=") { return .search }
if absoluteString == endpoint.baseURL.absoluteString { return .resource }
if Int(lastPathComponent) != nil { return .item }
return .none
}
}
var isPagedResults: Bool {
switch endpointStyle {
case .resource, .page, .search: return true
default: return false
}
}
}