A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.
? TermiNetwork has been tested in a production environment with a heavy load of asynchronous requests and tens of thousands of unique clients per day.
This is a high level diagram of TermiNetwork showing how its componets are connected to each other.
Who's using TermiNetwork
Features
- [x] Multi-environment setup
- [x] Model deserialization with Codables
- [x] Decode response in the appropriate response type: Codable, UIImage, Data or String
- [x] UIKit/SwiftUI helpers for downloading remote images
- [x] Group your Requests in Routers
- [x] Reachability support
- [x] Transformers: convert models from one type to another easily
- [x] Error Handling
- [x] Interceptors
- [x] Mock Responses
- [x] Certificate Pinning
- [x] Flexible Configuration
- [x] Middleware
- [x] File/Data Upload/Download
- [x] Pretty printed debug information
Table of contents
- Installation
- Demo Application
- Usage
- Queue Hooks
- Error Handling
- Reachability
- Transformers
- Mock responses
- Interceptors
- Image Helpers
- Middleware
- Debug Logging
Installation
You can install TermiNetwork with one of the following ways...
CocoaPods
Add the following line to your Podfile and run pod install in your terminal:
Carthage
Add the following line to your Carthage and run carthage update in your terminal:
Swift Package Manager
Go to File > Swift Packages > Add Package Dependency and add the following URL :
https://github.com/billp/TermiNetwork
Demo Application
To see all the features of TermiNetwork in action, download the source code and run the TermiNetworkExamples scheme.
Usage
Simple usage (Request)
Let's say you have the following Codable model:
The following example creates a request that adds a new Todo:
Parameters Explanation
method
One of the following supported HTTP methods:
.get, .head, .post, .put, .delete, .connect, .options, .trace or .patch
responseType
One of the following supported response types
Codable.self (implementations), UIImage.self, Data.self or String.self
onSuccess
A callback that returns an object of the given type. (specified in responseType parameter)
onFailure
A callback that returns a Error and the response Data (if any).
Advanced usage of Request with Configuration and custom Queue
The following example uses a custom Queue with maxConcurrentOperationCount and a configuration object. To see the full list of available configuration properties, take a look at Configuration properties in documentation.
The request above uses a custom queue myQueue with a failure mode of .continue (default), which means that the queue continues its execution if a request fails.
Complete setup with Environments and Routers
The complete and recommended setup of TermiNetwork consists of defining Environments and Routers.
Environment setup
Create a swift enum that implements the EnvironmentProtocol and define your environments.
Example
Optionally you can pass a configuration object to make all Routers and Requests to inherit the given configuration settings.
To set your global environment use Environment.set method
Router setup
Create a swift enum that implements the RouteProtocol and define your environments.
The following example creates a TodosRoute with all the required API routes (cases).
Example
You can optionally pass a configuration object to each case if you want provide different configuration for each route.
Make a request
To create the request you have to initialize a Router instance and specialize it with your defined Router, in our case TodosRoute:
Queue Hooks
Hooks are closures that run before and/or after a request execution in a queue. The following hooks are available:
For more information take a look at Queue in documentation.
Error Handling
TermiNetwork provides its own error types (TNError) for all the possible error cases. These errors are typically returned in onFailure callbacks of start methods.
To see all the available errors, please visit the TNError in documentation.
Example
Reachability
With Reachability you can monitor the network state of the device, like whether it is connected through wifi or cellular network.
Example
Transformers
Transformers enables you to convert your Rest models to Domain models by defining your custom transform functions. To do so, you have to create a class that inherits the Transformer class and specializing it by providing the FromType and ToType generics.
The following example transforms an array of RSCity (rest) to an array of City (domain) by overriding the transform function.
Example
Finally, pass the CitiesTransformer in the Request's start method:
Example
Mock responses
Mock responses is a powerful feature of TermiNetwork that enables you to provide a local resource file as Request's response. This is useful, for example, when the API service is not yet available and you need to implement the app's functionality without losing any time. (Prerequisite for this is to have an API contract)
Steps to enable mock responses
- Create a Bundle resource and put your files there. (File > New -> File... > Settings Bundle)
- Specify the Bundle path in Configuration
Example
- Enable Mock responses in Configuration
Example
- Define the mockFilePath path in your Routes
Example
The example above loads the Cities/cities.json from MockData.bundle and returns its data as Request's response.
For a complete example, open the demo application and take a look at City Explorer - Offline Mode.
Interceptors
Interceptors offers you a way to change or augment the usual processing cycle of a Request. For instance, you can refresh an expired access token (unauthorized status code 401) and then retry the original request. To do so, you just have to implement the InterceptorProtocol.
The following Interceptor implementation tries to refresh the access token with a retry limit (5).
Example
Finally, you have to pass the UnauthorizedInterceptor to the interceptors property in Configuration:
Example
SwiftUI/UIKit Image Helpers
TermiNetwork provides two different helpers for setting remote images.
SwiftUI Image Helper
Examples
-
Example with URL
-
Example with Request
UIImageView, NSImageView, WKInterfaceImage Extensions
-
Example with URL
-
Example with Request
Middleware
Middleware enables you to modify headers, params and response before they reach the success/failure callbacks. You can create your own middleware by implementing the RequestMiddlewareProtocol and passing it to a Configuration object.
Take a look at ./Examples/Communication/Middleware/CryptoMiddleware.swift for an example that adds an additional encryption layer to the application.
Debug Logging
You can enable the debug logging by setting the verbose property to true in your Configuration.
... and you will see a beautiful pretty-printed debug output in debug window

Tests
To run the tests open the Xcode Project > TermiNetwork scheme, select Product -> Test or simply press ⌘U on keyboard.
Contributors
Alex Athanasiadis, alexanderathan@gmail.com
License
TermiNetwork is available under the MIT license. See the LICENSE file for more info.