What is TermiNetwork?
TermiNetwork is a zero-dependency networking solution for building modern and secure applications for Apple platforms.
It is designed to provide automatic deserialization with Apple’s Codables and flexible configuration, as well as some advanced features like Certificate Pinning, Interceptors and Mock responses. To see all the features the library provides, visit: https://github.com/billp/TermiNetwork.
In this tutorial we’ll start by defining our Environments, Routers and creating Requests for a simple Todos iOS application using TermiNetwork.
TermiNetwork supports all the commonly used installation methods: CocoaPods, Carthage and Swift Packages. We are going to use the Swift Packages because it’s the quickest method, but you can use any method you want based on your needs.
Create a new project and go to File > Swift Packages > Add Package Dependency > Enter: https://github.com/billp/TermiNetwork
When you start developing your application, one important thing to consider is your API environments. Some commonly used environments are: development, qa, uat and production but for simplicity we are going to define only two: development and qa.
Let’s define the Environments based in the above table. Your environments are defined as an enum, which implements the EnvironmentProtocol:
One last thing before moving to the Routers is to set the global environment.
Put the .set method call to an entry-point of your application launch, e.g. in AppDelegate’s application(_:didFinishLaunchingWithOptions:)
Now that we have configured our environments it’s time to define our Todos Router(CRUD).
Let’s say that we have the following API description for todos:
To create these routes we have to create an enum that implements the RouteProtocol:
Create and execute a Request
At this point we are ready to create and start requests based on the Routes we have defined previously.
To let TermiNetwork deserialize the response for us, we need to define our Codable model as well.
Finally, we create a Router instance by specializing it with the TodoRoute, and calling the start method by passing Todo.self as responseType.
By default, debug logging in console is disabled. To enable it, just use a Configuration object and pass it to your current environment.
For a full list of configuration parameters take a look at official documentation for Configuration
Bellow you will see some beautiful, pretty-printed, pieces of debug information in the console when verbose is true:
And that’s it, now you know enough to start using the TermNetwork library in your own projects. To check all the available features the library provides, visit the project’s GitHub page: https://github.com/billp/TermiNetwork and the documentation at: https://billp.github.io/TermiNetwork.
If you’ve found this library useful, it would be very helpful if you press the Star button in the lib’s GitHub page, so that that it will be presented to other iOS developers as well.