The quest for the perfect networking library

An attempt to solve a very subjective problem

Raul Riera
2 min readJan 20, 2016
Source: Frozen, duh

If you are like me, you care about the “networking” part of your app, that piece of logic that communicates with your API and handles all the HTTP requests that make your app unique and most likely be reused for a Watch, TV, etc.

The tail of a thousand libraries

There is no shortage of networking libraries out there. Many great developers have put a lot of effort into creating some really good ones, my personally favorites are Moya and APIKit. After a little battling over the features and the limitations, I came to the conclusion that APIKit was the perfect fit for me.

Endpoints as value types

This right here is the core of the previously mentioned libraries, and it’s fantastic. Having each of the endpoints in your API represented as value types, instead of the typical method in some class in your code is a wonderful way to keep everything “single purpose” and neatly compressed into understandable bits of code.

Let’s look at a request that goes into the Dribbble API an returns the authenticated user:

Conforming to the “RequestType” protocol, the “UserRequest” becomes a powerful value type.

And how do we use this endpoint? APIKit includes a Session struct that is the responsable of executing all requests. So, calling the previous endpoint would look something like this:

let request = UserRequest()
Session.sendRequest(request) { result in
// do something with the result
}

That is pretty much it, just how we like things. Simple and easy to understand.

Wrapping it up with NSOperstions

A little while ago I wrote something about this subject, and the example code included an Operation that handled NSURLRequests, this was not a coincidence. Taking into consideration the post about the subject try to wrap this library around NSOperations, it would be fun to join the two approaches together.

--

--

Raul Riera
Raul Riera

Written by Raul Riera

Software Engineer, I make things so you don’t have to. More at https://raulriera.com

No responses yet