Last week, we took a custom look at the keyboard library that displays pre-Vietnam currency suggestions. Today I will show you how to create a library to call the network as a restful API. The library is called Qnetwork. You can download it on github at the link:


What is this library for?

First before proceeding with construction, download my demo section at the link:


First, you need to know that I built this library best for MVVM architecture. However, you can still use it for other architectures you want. If you understand in a simple way, you use this library, you create services to call apIs. Then you use it to call wherever you want.

So how to use it? Take a look at my demo code. In this example I will proceed to call the GITHUB API:

  -H "Accept: application/vnd.github.v3+json" 

In a previous article , Iintroduced quite carefully how to write an API for it. The steps are as follows:

  • Create a Github module. Each module will serve a separate screen. In MVVM design I often do so, modules include View, Model, servive call API and view model. You can open the above souce code.
  • Create a GithubAPI file. This file contains the API cluster to call. Usually in my application there will be many files in *API format. Each file contains 1 cluster, for example, AuthenAPI will contain a logon and logout cluster. FeedAPI contains feed-related API clusters. The naming will kill what it does. The description of this file is as follows:
  • Then you create the GithubSearchService file again. Here you need to transfer 1 Codable struct so that when the API has a response, you need to insert it and transfer json to this struct. Specifically, struct GithubSearchResponse:

Many of you ask me how I can go from json to struct as above. I'm a lazy person, so I used
to go on this page to https://app.quicktype.io

Then you just need to edit the name to make sense. You do a lot of things that will get used to.

  • After you've finished the service cluster above, you need to create GithubViewModel:

Many of you will be curious why I wrote the above. Similarly, I often download other people's sources. Then I clone exactly the same, what they write, I write. Gradually accustomed to hands, with reflexes. Finally, understand what people do. Practice is still the best way to learn programming anyway.

How to build QNetwork?

This is quite obscure without videos. To be honest, I watched another developer's video tutorial on this link:

You watch the video carefully, follow him, you will create a swift package and can give it to others to use through a link. With Qnetwork, simply add this path to your swift package(Xcode ➞ File ➞ New ➞ Swift Package):


From now on, I will only need a link, then add, and can easily deploy my network service class.

If you have any questions, go to the page's fan page at:


Share it with someone if you find the post helpful.

User Avatar

Code Toàn Bug

Code nhiều bug nhiều!

Leave a Reply

Your email address will not be published. Required fields are marked *