Skip to content

Simple lightweight HTTP Networking Library written in Swift based on UrlSession

License

Notifications You must be signed in to change notification settings

belalsamyyy/SimpleAPI

Repository files navigation

SimpleAPI

Simple lightweight HTTP Networking Library written in Swift based on UrlSession

Requirements

  • Swift 5.0+
  • iOS 13+

Installation

Cocoapods

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

pod 'SimpleAPI', '~> 2.0.7'

Usage

Quick Start

1. Create your model ( Must Conform to Model)

import Foundation
import SimpleAPI

struct Post: Model {
    //API => Model protocol will add 3 static properties to communicate with API
    static var endpoint: String! = "https://jsonplaceholder.typicode.com/posts"
    static var params: Params?
    static var headers: Headers? = ["Content-type": "application/json"]

    //Properties
    var title: String
    var body: String
}

2. Get your data immediately ⚡️⚡️⚡️

  • Quicker Version => return value directly

    API<Post>.object { post in
        // do whatever you want with "post" object ... 
    }
    API<Post>.list { posts in
        // do whatever you want with "posts" array ... 
    }
  • Longer Version => handle success & failure cases

    API<Post>.objectResult { result in
        switch result {
        case .success(let post):
        // do whatever you want with "post" object ... 
    
        case .failure(let error):
            print(error)
        }
    }
    API<Post>.listResult { result in
        switch result {
        case .success(let posts):
            // do whatever you want with "posts" array ... 
    
        case .failure(let error):
            print(error)
        }
    }

Get a descriptive success/error messages

Screen Shot 2022-04-02 at 11 44 47 AM

object() and objectResult()

Parameters Value Notes
HTTPMethod [enum] .get() [default], .put(), .delete(), .post() you could pass an endpoint extension for specfic request through http methods
encoding [enum] .json [default], .url to change body parameters encoding => json encoded or url encoded
decoding - [Bool] true [default], false if your API response is empty or not return object from the same type , you need to set decoding to false

Endpoint Extension

you could pass an endpoint extension ( custom paths/queries) for specfic request through http methods

// Post.endpoint = "https://jsonplaceholder.typicode.com/posts" [original]

// .get("1") => https://jsonplaceholder.typicode.com/posts/1 🆕
// .get("?page=1&search=movie") => https://jsonplaceholder.typicode.com/posts?page=1&search=movie 🆕

Examples

GET - object

API<Post>.object { post in
    self.label.text = post.title 
}

GET - object with id

API<Post>.object(.get("1")) { post in
    self.label.text = post.title 
}

PUT - object with id

API<Post>.object(.put("1")) { post in
    // ...
}

DELETE - object with id

API<Post>.object(.delete("1"), decoding: false) { post in
    // ...
}

POST

API<Token>.object(.post, encoding: .url) { token in
    // ...
}

Extra Features

  • SimpleAPI run on the main thread, so you could work with your UI directly
  • Endpoints support Arabic characters and spacing with percent-encoding by default

Author

BelalSamy, belalsamy10@gmail.com

License

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