Now you can make simple web APIs the fast, Swifty way
IBM has released a major update to its Kitura server project, delivering new features that take advantage of Swift 4’s Codable
protocol, new command-line tools, and new commercial support for Swift on Linux.
Easily the most fascinating feature of Kitura 2.0 is its use of codable routing, which makes it trivial to add JSON-based web APIs to get and post data. In fact, as long as your custom data types support Codable
, you’re only a few lines of code away from adding support for common operations such as listing all items, finding a specific item by its identifier, and creating a new item from submitted values.
Of course, it’s easy to write that, but what you really want is a code example so you can see codable routing for yourself. Well, it’s lucky for you that I just updated my Server-Side Swift book for Kitura 2, so here’s a small slice from the chapter on codable routing:
// create a simple data type that conforms to Codable
struct Singer: Codable {
var name: String
var age: Int
}
// create an array of values so we have something to work with
var singers = [
Singer(name: "Taylor Swift", age: 27),
Singer(name: "Justin Bieber", age: 23),
Singer(name: "Ed Sheeran", age: 26)
]
// create a route that returns all singers
router.get("/singers/all") {
(completion: ([Singer]?, RequestError?) -> Void) in
completion(singers, nil)
}
All the magic is done at the end: by adding a route with a specific completion handler, the Kitura router is able to understand that we’ll return an array of a specific codable type, and handle the JSON sending automatically.
Codable routing makes it a cinch to implement create, read, update, and delete (CRUD) in your web API, which is a huge step forward. However, even better is the addition of the all-new KituraKit framework, which is a client-side framework to perform the flip side of the equation: it converts your models to JSON so they can be sent to your server. The combination of these two new features means you can finally share your model code between client and server, and never need to worry about encoding and decoding again.
As you might imagine, codable routing is aimed squarely at making it substantially easier to build web APIs. At the same time, though, it makes it substantially Swiftier – if you’re lucky enough that codable routing does everything you need, you can build web APIs in Swift without ever worrying about the underlying data that’s being transferred, which will definitely lower the learning curve for many.
You can find out more about Kitura 2.0, codable routing, and KituraKit at the official announcement. You might also be interested in my Server-Side Swift book, which was just updated for Kitura 2.0.
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.