Last active
October 31, 2017 00:20
-
-
Save JacopoMangiavacchi/f15891a8916e3fdb88df1bb88f05f3c9 to your computer and use it in GitHub Desktop.
Swift - Kitura Rest Test : <- Post <- Request <- Get
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
import Foundation | |
import Kitura | |
import KituraContracts | |
import KituraKit | |
struct Language : Codable { | |
var language: String | |
} | |
struct Request : Codable { | |
var url: String | |
} | |
func languageHandler(completion: ([Language]?, RequestError?) -> Void ) -> Void { | |
let languages: [Language] = [Language(language: "Swift")] | |
completion(languages, nil) | |
} | |
func requestHandler(request: Request, completion: @escaping (Language?, RequestError?) -> Void ) -> Void { | |
guard let client = KituraKit(baseURL: "http://localhost:8080") else { return } | |
client.get("/language") { (languages: [Language]?, error: Error?) in | |
guard error == nil else { return } | |
guard let languages = languages, languages.count > 0 else { return } | |
completion(languages[0], nil) | |
} | |
} | |
let router = Router() | |
router.get("/language", handler: languageHandler) | |
router.post("/request", handler: requestHandler) | |
Kitura.addHTTPServer(onPort: 8080, with: router) | |
Kitura.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment