Today I want to tell you about writing Network Layer with Moya:

Moya (https://github.com/Moya/Moya) is Network Abstration layer between your code and Alamofire for Swift. It has many functions like custom Providers, Plugins and other. You can easily subclass it’s classes to add features you need. Awesome work Moya team!

We will write Network Layer with next capabilities:

Request information from server

Validate response status code in general way

Parse server data to Objects

Let’s begin!

Request information from Server

To achieve this you must describe your server API by writting API Target. Let’s do this:

Step by step:

1 — Firstly you create enumeration which will describe your service. In our example we have several API calls like .cameras (which will be GET cameras from server), .settings (which will be GET settings for camera ID) and finaly .createUser (which will be POST create user with parameters).

2 — The second step is to extend your enum and conform TargetType protocol. To conform TargetType protocol we must implement several methods which will be in steps 3–9.

3 — Here you write your server base URL. Nothing special.

4 — At this point you add extra path to make full route for our Server call. For example route for .camera will be base URL + extra path added in line 28 (https://testing.myserver.com/api/v1/cameras)

5 — At this point you set method type. You can set GET, PUT, POST, DELETE etc.

6 — You add parameters for your request.

7 — Encoding. You can choose between JSON Encoding (parameters will be set as object in request body) and URL encoding (parameters will be in URL seperated by & symbol).

8 — Sample data is data for testing purposes. You can for example set mock data and test your requests even if your server is not avaialable right now.

9 — Set task type, you can set request, download, upload etc.

And now you can make your request:

Easy right? You just described your server API and then you can make requests and get responses. Cool!

Validate response status code in general way

By default .failure(let error): will be called if your App can’t reach your server (for example timed out error). But how to validate for example 500 or 404 errors? You can do this by wrapping your request in adapter. Please look at this example:

We are wrapping our request call and then validate it. Step by step:

1 — At this point we are validating our response status code.

2 — We send error in errorCallback when we get error from Server. At this point you can event parse your own errors (if Server sends error from example in JSON format).

3 — This is callback for Moya errors like timed out.

Let’s look how changed our method calling:

Parse server data to Objects

We created our Network Layer, separate valid responses and errors. And now I want to tell you how to quickly parse objects from JSON to your own classes. We will use Moya-ModelMapper (https://github.com/sunshinejr/Moya-ModelMapper). This library allows you to describe your object and then parse it with one line of code. Let’s do this!

Firstly let’s see how to describe Class to support Moya-ModelMapper:

1 — You must conform Mappable protocol.

2 — To conform Mappable protocol you must write required init(map: Mapper) throws method. In this method you parse your JSON and assign values from server to your properties.

And now you can parse your data:

Conclusion

I described how to make your own Network Layer with Moya library. You can do this by describing Server API and conforming Moya’s TargetType protocol. I showed you how to make you Network Adapter for your API and validate responses. Also I showed you how to easily parse data from SErver with Moya-ModelMapper.

P.S. As usual song which I found several days ago: Scorpion s— Maybe I Maybe You https://itun.es/ru/Rj6vr?i=290955890