API Layer in Swift With Async:Await - Better Programming
API Layer in Swift With Async:Await - Better Programming
152 1
I want to demonstrate a quick and easy network layer you can use to ace live
coding interviews, it conforms to SOLID principles, uses generics,
implements Async/Await, has appropriate error handling, works in UIKit and
SwiftUI apps and if you practice writing it from scratch, can be up and
running within 10 minutes which leaves you more time in a technical
interview to focus on the rest of the functionality.
First of all, we’ll need an API to pull data from, preferably something with a
couple of different endpoints that return different types of data. The UK
government’s food hygiene rating API is a good starting point.
Take some time to familiarise yourself with it, but it’s not crucial to know it
as we’ll be looking at a couple of the endpoints in particular as part of this
guide.
On to some coding:
We’ll start coding the APIRouter with an enum to act as our router object
which will house the components needed to construct each of our requests.
For this guide, we’ll be creating 3 different requests:
4. method — The API method that you’re using to make the request
(POST/PUT/PATCH/GET/DELETE)
You might notice I’ve not covered body objects, I’ve never been asked
personally to implement POST requests during interviews, this will be due to
the additional time to set up and configure compared to more simple GET
requests. If we did want to add body data, it would be added as part of the
URLRequestDispatcher as URLComponents in iOS only deal with the construction
of URLs (which seems obvious) — It would be the URLRequest objects to
which we need to add data to.
You’ll see the complete router above, which matches what was discussed
previously. You might wonder why we decided to use an enum , and it’s for
quite a simple reason. If we decide to add a 4th request, the compiler will
throw errors within each computed variable to indicate we are missing a
handled case, which prevents you from forgetting to implement its
scheme/path/method etc.
Generics: https://docs.swift.org/swift-
book/LanguageGuide/Generics.html
Async/Await: https://docs.swift.org/swift-
book/LanguageGuide/Concurrency.html
There will be an element of custom error handling as well but that’s pretty
easy to see what’s going on.
The class itself will be straightforward and only contain a single class
function request<T: Codable>(router: APIRouter) async throws -> T . For most
cases, it will be more than sufficient to handle simple API requests. Breaking
it down, you can see it expects and will return objects that conform to the
codable protocol, you need to provide an APIRouter as a parameter, the
function runs asynchronously, and can also throw errors.
5. If our dataTask fails to get any data back, we can throw our other
custom error here to indicate a noData response, handling the error
appropriately.
That’s pretty much it for the request dispatcher, our generics are decoded
and returned, and any errors are handled and thrown accordingly.
The API Clients that we’ll make are how the users/apps interactions will
communicate with the API. We will create 1 client per request using a POP
(Protocol Oriented Programming) approach. Each request will have its own
protocol and a class that conforms to it, we do it this way for ease of mocking
the API Client and better separation of concerns as some screens may only
want to access a list of authorities, whereas others may only want ratings.
You can see the implementations above, you might notice that with the
RatingClient we pass a parameter but not to the router, this is because
there’s a limitation in the API, the rating endpoint doesn’t accept and query
parameters so filtering has to be done on the device.
152 1
iOS app developer, team lead, passionate about building and fostering a team culture that
keeps people engaged
5.5K 48 23
See all from Jonathon Albert See all from Better Programming
436
Lists
85 1 10
16 173 3
Help Status Writers Blog Careers Privacy Terms About Text to speech Teams