SHARE
TWEET

Untitled

a guest Oct 18th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. final class NetworkUsers: URLRepresentable {
  2.     let apiVersion: APIVersion
  3.     let network: String
  4.    
  5.     init(apiVersion: APIVersion, network: String) {
  6.         self.apiVersion = apiVersion
  7.         self.network = network
  8.     }
  9.    
  10.     func makeURL() throws -> URL {
  11.         return try apiVersion.makeURL(forPath: "/networks/\(network)/users")
  12.     }
  13. }
  14.  
  15. enum APIVersion {
  16.     case v1, v2, v3
  17.    
  18.     func makeURL(forPath path: String) throws -> URL {
  19.         let endpoint: APIEndpoint
  20.        
  21.         switch self {
  22.         case .v1:
  23.             endpoint = .v1(path)
  24.         case .v2:
  25.             endpoint = .v2(path)
  26.         case .v3:
  27.             endpoint = .v3(path)
  28.         }
  29.        
  30.         return try endpoint.makeURL()
  31.     }
  32. }
  33.  
  34. @dynamicMemberLookup
  35. struct NetworkRoutes {
  36.     let apiVersion: APIVersion
  37.     let network: String
  38.    
  39.     var users: NetworkUsers {
  40.         return NetworkUsers(apiVersion: apiVersion, network: network)
  41.     }
  42.    
  43.     init(apiVersion: APIVersion, network: String) {
  44.         self.apiVersion = apiVersion
  45.         self.network = network
  46.     }
  47.    
  48.     subscript<Route>(dynamicMember keyPath: KeyPath<Self, Route>) -> Route {
  49.         return self[keyPath: keyPath]
  50.     }
  51. }
  52.  
  53. struct AllNetworkRoutes {
  54.     let apiVersion: APIVersion
  55.    
  56.     subscript(id: String) -> NetworkRoutes {
  57.         NetworkRoutes(apiVersion: apiVersion, network: id)
  58.     }
  59.    
  60.     init(apiVersion: APIVersion) {
  61.         self.apiVersion = apiVersion
  62.     }
  63. }
  64.  
  65. struct V2 {
  66.     let networks = AllNetworkRoutes(apiVersion: .v2)
  67.    
  68.     init() {}
  69. }
  70.  
  71. @dynamicMemberLookup
  72. struct APIRoutes {
  73.     subscript<Route>(dynamicMember keyPath: KeyPath<Self, Route>) -> Route {
  74.         return self[keyPath: keyPath]
  75.     }
  76.    
  77.     let v2 = V2()
  78.    
  79.     init() {}
  80. }
  81.  
  82. let routes = APIRoutes()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top