Created
September 30, 2019 09:53
-
-
Save Joannis/38a9c41a60a8a7bbb1006426bc119e80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class NetworkUsers: URLRepresentable { | |
let apiVersion: APIVersion | |
let network: String | |
init(apiVersion: APIVersion, network: String) { | |
self.apiVersion = apiVersion | |
self.network = network | |
} | |
func makeURL() throws -> URL { | |
return try apiVersion.makeURL(forPath: "/networks/\(network)/users") | |
} | |
} | |
enum APIVersion { | |
case v1, v2, v3 | |
func makeURL(forPath path: String) throws -> URL { | |
let endpoint: APIEndpoint | |
switch self { | |
case .v1: | |
endpoint = .v1(path) | |
case .v2: | |
endpoint = .v2(path) | |
case .v3: | |
endpoint = .v3(path) | |
} | |
return try endpoint.makeURL() | |
} | |
} | |
@dynamicMemberLookup | |
struct NetworkRoutes { | |
let apiVersion: APIVersion | |
let network: String | |
var users: NetworkUsers { | |
return NetworkUsers(apiVersion: apiVersion, network: network) | |
} | |
init(apiVersion: APIVersion, network: String) { | |
self.apiVersion = apiVersion | |
self.network = network | |
} | |
subscript<Route>(dynamicMember keyPath: KeyPath<Self, Route>) -> Route { | |
return self[keyPath: keyPath] | |
} | |
} | |
struct AllNetworkRoutes { | |
let apiVersion: APIVersion | |
subscript(id: String) -> NetworkRoutes { | |
NetworkRoutes(apiVersion: apiVersion, network: id) | |
} | |
init(apiVersion: APIVersion) { | |
self.apiVersion = apiVersion | |
} | |
} | |
struct V2 { | |
let networks = AllNetworkRoutes(apiVersion: .v2) | |
init() {} | |
} | |
@dynamicMemberLookup | |
struct APIRoutes { | |
subscript<Route>(dynamicMember keyPath: KeyPath<Self, Route>) -> Route { | |
return self[keyPath: keyPath] | |
} | |
let v2 = V2() | |
init() {} | |
} | |
let routes = APIRoutes() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment