Introducing Nikka, a lightweight networking library for Swift
A networking library for Swift? What’s wrong with Alamofire, you might think. Well, Alamofire is really great but how about something that you could actually understand and adapt to your needs?
Check out this article if you want to know what problems you might face with Alamofire:
When building this library, we really wanted two things: first a core module that everybody could understand and that will give you a nice architecture to start with; and second an easy way to adapt the library if you need something quite specific.
A better structure
Alamofire lets you handle the API by yourself, meaning that you define your endpoints, HTTP headers and parameters however you want. There is no real structure for this. This can lead to a big mess if you have many developers on the same project, and that doesn’t feel right. That’s why other libraries like Moya have emerged. Moya gives you a structure and that’s really great. But dammit, again another library.
What we did is combine the best of both world, and keep it as simple as possible.
Providers
With Nikka the first thing you need is implementing a provider. HTTPProvider is a protocol that lets you define your API and how you want to talk to it. What’s really great about it, is that you can define as many providers as you want in your app. Usually you have your own API, but then you might also use Spotify, GoogleMaps or Giphy because apps today are all about interacting together.
Here’s the simplest way of creating a provider, the only thing it needs is a base URL:
Endpoints
After creating your provider, you can define all the endpoints that you will need in your app. We have created a structure for this, that has everything you need, and we called it Route. The way you define a Route is up to you. You can have them all in the same place, or you can split them amongst your business logic. At Her, we have separated them in each one of our services for a better scalability. Here’s what it could looks like:
Making a request
After you have defined your API and your endpoints, it becomes really easy to make a request. We have kept the basic Alamofire syntax that is really easy to use and can be adapted nicely:
Great, that’s it! With just a few lines of code you have made a simple and clean request.
Extensions, extensions, extensions
So, this is quite nice, it is the core of Nikka, it can be customized as you wish for different API. That wasn’t enough for us, though. In every app nowadays, networking means a lot of other things, so we made the core of Nikka really simple so that it’s easy to add extensions like JSON parsing ones.
JSON Management
Your app will likely communicate with your API with JSON. It made sense to us to include a module that will parse your data directly and return Swift objects. Here’s what a request looks like if your favorite JSON library is Mapper:
And since it is actually pretty simple to create independent extensions in the library, we also added support for all these JSON libraries:
The request syntax stays the same for all these libraries so that it makes you even less dependent on one of them. If tomorrow, a new JSON library pops up, it will be super easy for you to switch.
Everything that we saw is quite nice, we can write our requests like we used to, and with a better structure. We wanted to go even further though, and implement some nice features of modern programming.
Futures
Futures comes really handy in modern programming and can greatly improve the readability of your code. If you have no idea what a future is, i would highly recommend you to watch this talk by Javier Soto:
Let’s try it with an example: Imagine that you have an app that has a feed. In order to access the feed the user must log in. But it wouldn’t make a lot of sense for the user to log in and have no feed so you want to make sure that both requests succeed before letting the user into the app. Here’s how you would do it with Nikka:
RxSwift
Finally you might be also really into Reactive Programming, so we have added an extension for RxSwift that returns Observables:
Great, that’s it! We believe we have made library really simple to use and to understand, that is scalable and adaptable. We hope you’ll like it as much as we do, feel free to send us feedback at emilien@weareher.com.
You can check for more documentation and how to customize it on the project’s github below.