Struggling in the network layer when sending requests and parsing responses.
Applying generic, modular network layer. The solution includes Generic Type, Completion, Singleton, Codable, URLSession and OOP(Object Oriented Programming) terms.
According to most of the mobile developers, the number one subject or layer or structure in mobile development is Network Layer. A lot of mobile applications work online then the Network Layer is the hearth of them. The point is, how can we build a strong network layer against even for complex applications? Let’s find the answer!
What are we going to do?
We are going to build a service manager, request and response classes, a middle layer to communicate between controller and service calls and data models. All are very similar for every developer but the difference is our response classes are generic. Also, the structure is modular and we can extend it easily.
Controller: The view that needs data and where the service call begins.
Middle Layer: The layer communicates between the Controller and the Service Manager. It defines request and response class.
Service Manager: The base class sends a request and gets a raw response. It parses the response and sends it to the Middle Layer.
Request Model: The model that has information about the endpoint requirements such as endpoint path, request headers, URL parameters, request body.
Response Model: Contains base structure of JSON such as isSuccess, message and data(generic).
I finished a working example of the network layer. Let’s talk over the project! You can download the project here → https://github.com/demirciy/GenericNetworkLayeriOS
I have created custom response JSON which is called Response.json in the Sources folder. Also, the project has codes for real REST service. The chosen REST service is Current Average Price provides by Binance public API. The service returns average price by minute. The Response.json has the same response.
Let’s start where we call the service.
When the app launches, the HomeController runs first. There is no code related to UI. We are just working on service calls.
The service wants us currency pairs in the parameters. We send ‘BTCUSDT’ as the value of the symbol key. By the way, the Services class is the Middle Layer on the Network Layer.
Why we need Middle Layer?
The Middle Layer is a bridge between the Controller and the Service Manager. We can integrate offline mod into the app. By then, we check online/offline status on the Middle Layer and get data by the network status.
The Services class includes class functions which represent an endpoint with its request and response model. Before seeing ServiceManager, we must see what is going on inside RequestModel and ResponseModel.
The RequestModel has variables what a request needs.
On the other hand, every endpoint has own request and response classes. The request class inherits from RequestModel which is the base request class. We have AveragePriceRequestModel inherits from the RequestModel.
It overrides path and parameters variables that are unique for the Average Price service.
The ResponseModel has a few variables to read returning JSON. The data variable is a generic type so that we can use a different kind of response models via generic type. In software development, most of the services have a base response structure which is very similar to that,
Basically, isSuccess represents the status of the request, message represents a text the user might see or an error message, data represents a JSON object the client uses.
We use Codable in the response model. Also, we can see what the response is on the 7th line.
It is the heart of the Network Layer. I have preferred to use URLSession which is the class provides by Apple in the Foundation framework for service communication. By the way, when decoding the Response.json, we do not need URLSession.
The Swift class has an enum for us which is Result. It has 2 cases, success and failure. We return generic response model(AveragePriceResponseModel) on the success. On the other hand, we return ErrorModel on the failure.