Create a Router Network from URLSession

Kittisak Phetrungnapha
Published in
3 min readJan 14, 2019

--

Most of the iOS apps in the world could have an interaction with data from network e.g. Restful, XML-RPC, GraphQL etc. We mostly use 3rd party to get the data from many sources. We mostly use Alamofire or Moya for network library. It’s ok and should be used in case of your application in long term project and keep growing complex because contributors have already implemented many nice features and method signatures. You just take some coffee then anything works properly.

However, we have a question in our head why do we need 3rd party in case of our application is just a small, simple, solo project, especially those libraries are also implemented from the native fundamental iOS class. Would not be implemented them by ourselves? The benefit that we get from trying to get rid of 3rd party are

  1. Flexible because we can change anything if it’s need for fitting our requirements.
  2. Size of the application is very small because we do not plug many code from 3rd party library in our project (unless you add many big images :D).
  3. Build time is very fast because we do not need to compile dependencies like CocoaPods, Carthage, or anything else.
  4. We more understand concept, fundamental, function, class in iOS SDK. We are getting better.

So, today We will show you how to implement a simple network class from URLSession that is based class from any 3rd party network library. We are trying to be a zero dependency developer!

Our network class have four functionalities including GET, POST, PUT, DELETE(Common network operation we are using everyday in our job.)

Create a generic Enum Result

Firstly, we create a generic enumeration names Result for holding the response from network ether success or error case. We do not need to do this when Swift 5 release because Result will be a native class.

Create a custom Error Type

Secondly, we create another enumeration names ErrorType that conforms LocalizedError for representing error message in case of we get failure response from network data sources.

You can add more error cases as much as you can if it fits with your requirements.

Create a Router

We get this idea from Routing Requests section in Alamofire. It very great because of single source of truth. If we want to add more endpoints, we will just look at this class. So, we adopt some idea and do not use protocol for simplicity.

You see that we use the powerful of Enum with Switch Case for separating method, path, request() depends on case in Router enumeration. If we add more case (that meant we have a new endpoint), we will not forget to add method, path, and request() due to the compiler will tell you. We do not suggest you to add default to switch-case otherwise the compiler will not complain you in case of you are missing something. Thanks Enum and Switch Case!

Create a Network class

Because of we want our code is single source of truth so we create a new class names Network and adding request data function. We use URLSession in here and adopt with the powerful of Generic and Decodable . This function will be called from caller side e.g. from ViewModel if we use MVVM or from Interactor if we use Clean Swift or VIPER . It only require one parameter Router and return Result<T> where T is your Struct type.

You see that you can also handle HTTP status code as much as you want.

Last but not least, Testing

At this point we are all covered about performing network operation including GET, POST, PUT, DELETE. So, let test it to see how it works as we are expecting.

Where Contact is a Struct that conforms to Codable. All tests should be passed.

Congratulation! we just finished building network class using only native iOS classes. Hope you will enjoy this article and if you have any question, feel free to leave comments below. Happy Coding! :)

--

--

Kittisak Phetrungnapha
iTopStory

I am a software engineer who fall in love to code, read, and write. :) itopstory.com