Skip to content

belalsamyyy/SimpleAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SimpleAPI

Simple HTTP Networking in Swift based on UrlSession ⚑

Installation

SimpleAPI is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SimpleAPI'

Usage

Step 1 - create Constants.swift file for your API Urls

import Foundation

let BASE_URL = "https://jsonplaceholder.typicode.com"

struct Endpoints {
    static let posts = "\(BASE_URL)/posts"
}

step 2 - make your model conform Model protocol

Model protocol will add 3 static properties to your struct

  • endpoint
  • params
  • headers

πŸ’‘ Tip: add setParams() function so you don't have to specify params keys inside your viewController

import Foundation
import SimpleAPI

struct Post: Model {
    //API
    static var endpoint: String! = Endpoints.posts
    static var params: Params?
    static var headers: Headers? = ["Content-type": "application/json"]

    static func setParams(title: String, body: String) {
        self.params = ["title": title, "body": body]
    }
    
    //Properties
    var title: String
    var body: String
    
    private enum CodingKeys : String, CodingKey {
        case title = "title"
        case body = "body"
    }
    
}

step 3 - create your API function in ViewController

  • just type API and specify the type of your <object> that you want to return
  • you have 2 functions : object to return one object, list to return list of objects
  • object function takes 2 parameters the first one is http method enum like .get(id), .post
  • the second parameter is decode boolean its true by default
  • but if you didn't need to decode the response to consider it as a success make it false

πŸ’‘ Tip: .get(id) it will add /id at the end of your end point automatically

GET - list of objects

    //MARK: - get list of posts
    func getPosts() {
        API<Post>.list { result in
            switch result {
            case .success(let posts):
                posts.forEach { post in
                    print(post!.title)
                }
            case .failure(let error):
                print(error)
            }
        }
    }

GET - object without id

    //MARK: - get post with id
    func getPost() {
        API<Post>.object(.getWithoutID) { result in
            switch result {
            case .success(let post):
                print(post!.title)
            case .failure(let error):
                print(error)
            }
        }
    }

GET - object with id

    //MARK: - get post with id
    func getPost(id: String) {
        API<Post>.object(.get(id)) { result in
            switch result {
            case .success(let post):
                print(post!.title)
            case .failure(let error):
                print(error)
            }
        }
    }

POST - object with id

    //MARK: - set post
    func setPost(title: String, body: String) {
        Post.setParams(title: title, body: body)

        API<Post>.object(.post) { result in
            switch result {
            case .success(let post):
                print(post!.title)
            case .failure(let error):
                print(error)
            }
        }
    }

PUT - object with id

    //MARK: - update post with id
    func updatePost(id: String, title: String, body: String) {
        Post.setParams(title: title, body: body)

        API<Post>.object(.put(id)) { result in
            switch result {
            case .success(let post):
                print(post!.body)
            case .failure(let error):
                print(error)
            }
        }
    }

DELETE - object with id

    //MARK: - delete post with id
    func deletePost(id: String) {        
        API<Post>.object(.delete(id), decode: false) { result in
            switch result {
            case .success(_):
                print("Deleted Successfully !")
            case .failure(let error):
                print(error)
            }
        }
    }

step 4 - call your API function in viewDidLoad()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        getPosts()
    }

Extra Tips 😎

πŸ’‘ tip #1: you can customize your endpoint from your function as you want, like this

   func getVideos(page: Int, genreID: String) {
        Video.endpoint = "\(BASE_URL)\(CategoryName.movies)/genre/\(genreID)/\(page)" // << endpoint
        API<Video>.list { [weak self] result in
          // .
          // . 
          // .

πŸ’‘ tip #2: there're quicker versions of our 2 main funtions => quickObject & quickList

  • object & list comes with success & failure callbacks
  • but quickObject & quickList just return the value directly
  • if quick functions fails it'll only print the error without customizations
API<Post>.quickObject(.get("5")) { [weak self] post in 
    self?.label.text = post?.title
}
API<Post>.quickList() { posts in
    posts.forEach { post in
        print(post!.title)
    }
}

Author

BelalSamy, belalsamy10@gmail.com

License

SimpleAPI is available under the MIT license. See the LICENSE file for more info.