Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.07 KB | None | 0 0
  1. func fetchLessons(groupName: String, in container: Container) throws -> Future<KPIResponseContainer<[KPILesson]>> {
  2.    
  3.     let logger = try container.make(Logger.self)
  4.     let client = HTTPClient.connect(scheme: .https, hostname: hostname, on: container)
  5.    
  6.     return client.flatMap(to: HTTPResponse.self) { client in
  7.         guard
  8.             let encodedGroupName = groupName.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
  9.             let url = URLComponents(string: "/v2/groups/\(encodedGroupName)/lessons")?.url
  10.         else {
  11.             throw Abort(.badRequest, reason: "Invalid group name")
  12.         }
  13.        
  14.         logger.debug("Sending request to http://\(self.hostname)\(url.path)")
  15.        
  16.         let request = HTTPRequest(method: .GET, url: url)
  17.         return client.send(request)
  18.        
  19.     }.flatMap(to: KPIResponseContainer<[KPILesson]>.self) { httpResponse in
  20.        
  21.         let response = Response(http: httpResponse, using: container)
  22.         return try response.content.decode(KPIResponseContainer<[KPILesson]>.self)
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement