Creating an API Helper Library for SwiftNIO

In this SwiftNIO tutorial you’ll learn how to utilize the helper types from SwiftNIO to create an API library that accesses the Star Wars API. By Jari Koopman.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Using the New Methods

You’ve survived the copy and paste storm! All that’s left now is to use the awesome new methods you added! Open main.swift and replace everything below let client = SwapiClient(worker: eventLoopGroup) with the following:

let person = try client.getPerson(withId: 1).wait()
let details = try person.personalDetails.wait()
print(details)
print("===\n")

let film = try client.getFilm(withId: 1).wait()
print(film.info)
print("===\n")

let starship = try client.getStarship(withId: 9).wait()
print(starship.info)
print("===\n")

let vehicle = try client.getVehicle(withId: 4).wait()
print(vehicle.info)
print("===\n")

let species = try client.getSpecies(withId: 3).wait()
let speciesInfo = try species.info.wait()
print(speciesInfo)
print("===\n")

let planet = try client.getPlanet(withId: 1).wait()
print(planet.info)

Build and Run the application and there you have it. You created your very own API wrapper using SwiftNIO!

Where to Go From Here

You can download the final project using the Download Materials button at the top or bottom of this page.

To learn more about SwiftNIO and how to use it, take a look at these tutorials for a simple guide to async on the server or creating a TCP server with SwiftNIO. If you want to explore even further, the official NIO Docs are a great place to start.

Need a challenge? Try extending your API wrapper by adding even more functionality. For example, you could add SWAPI’s Wookiee mode.

If you have any comments or questions, please join the forum below!