Install

ZDKit is a framework wich simplify the queries between your API and your Swift Application.
Sending & Fetching JSON has never been so easy.
ZDKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ZDKit'
ZDKit is powered by Swift 4.2 so ensure in your podfile that your deployment target is at least '12.0'.

Get Started

Step 1 : Create a Model

First create your model (struct or class) conforming to the Swift's Codable protocol.
For a clean code ZDKit will assume your JSON encoding from snake_case to camelCase.

In this code sample let assume our API endpoint is returning an array of users where each entities are formated like below :
						
							GET /users

							{
								   id : 69006,
								   name : 'Jhon Doe',
								   picture_url : 'https://yourPictureUrl.com', // snake_case
								   age : 25
							}
						
					
					
						import Foundation

						struct User : Codable {
							   let id : Int
							   let name : String
							   let pictureUrl : String // camelCase
							   let age : Int
						}
					
				

Step 2 : Create a singleton class

In this class you will implement your own methods using ZDKit ZDRequest.fetchData().
ZDRequest use <T> generic's power to be reusable anytime.
						
							func fetchData<T : Codable>(request: URLRequest, for type: T.type, completion: @escaping: (T?) -> Void) -> Void
						
					
  • request : URLRequest
  • type : <T>.Type
  • completion : <T>?
					
						import ZDKit

						final class APIService {

							   static let shared = APIService()  //singleton
							   private let request = ZDRequest()   // ZDKit public class
							   private let host = "https://www.yourApiUrl/"


							   func getUsers(route: String, completion: @escaping([User]?) -> Void) -> Void {

								      guard let url = URL(string: "\(host)\(route)") else { return }

								      request.fetchData(request: URLRequest(url: url), for: [User].self { data in
									         guard let user = data else {
										            completion(nil)
										            return
									         }
									         completion(user)
								      })

							   }
						}
					
				

Step 3 : The magic in your Controller

Then in your view controller you just have to call your previously implemented method getUsers() from the signleton APIService.shared.
Be safe and make sure to unwrap the data before updating your controller's view.
					
						import UIKit

						class MyViewController : UIViewController {

							   let users : [User]?
							   let API = APIService.shared  // Singleton reference

							   viewDidLoad() {
								      super.viewDidLoad()

								      API.getUsers(route: "/users", { data in
									         guard let fetch = data else { return }   // Handle your error here.
									         self.users = fetch
									         self.updateWhatYouNeedTo()
								      })
							   }
						}
					
				

Source

ZDKit is available on CocoaPods and github.
Feel free to send me pull request if you find an issue(s) or if you have any great idea(s).