Latest 1.1.1
Homepage https://github.com/nawar/HTTPLite
License MIT
Platforms ios 9.0
Authors

HTTPLite

A simple Swift 3.x wrapper for URLSession

MIT licensed ios/osx


Installation

CocoaPods

To install, check out Get Started tab on cocoapods.org.
To use HTTPLite in your project add the pod below to your project’s Podfile

pod 'HTTPLite'

Then, from the command line, run:

$ pod install # or pod update

Then you are good to start using it :+1:

Usage

To use the library, import it in your file.

import HTTPLite

Follow that with a simple declaration of a Request object with the recipient URL.

guard let request = Request(Url: "https://www.google.com") else {
    print("Can't intialize the request")
    return
}

After that, we can perform any HTTP verb.

GET

let params: [String: String] = ["Artist" : "Michael Jackson", "Album" : "Thriller"]

request.get(parameters: params, success: { response in

    if let data = response.data {
    }

    if let url = response.url {

    }

}, failure: { error in

}) { progress in

}

JSON handling

let params: [String: String] = ["album" : "Michael Jackson - Thriller"]

request.get(parameters: params, success: { response in

    if let data = response.data {

    do {

            let JSON = try JSONSerialization.jsonObject(with: data,
            options: .mutableContainers)

    } catch let error { 

        }

        if let url = response.url { }

}, failure: { error in

}, progress: { progress in

})

POST

let params: [String: String] = ["healer":"Grigori Yefimovich Rasputin", "powers": "healer and adviser"]

request.post(parameters: params, success: { response in

    print("response data:(response)")

}, failure: { error in

}, progress: { progress in

})

Download

We can download files using either download() or get().

Download with get()

let params: [String: String] = ["image":"to be downloaded as data"]

request.get(parameters: params, success: { response in

    if let data = response.data { 
        let image = UIImage(data: data)
    }

    if let url = response.url { }

}, failure: { error in

}, progress: { progress in

})

Download with download()

let params: [String: String] = ["image":"to be downloaded as one file"]

request.download(parameters: params, success: { response in

    if let data = response.data { }

    if let url = response.url { 
        let image = UIImage(contentsOfFile: url.path)
    }

}, failure: { error in

}, progress: { progress in

})
Download Progress

We can easily measure the progress of a file download using the progress closure.

let params: [String: String] = ["image":"to be downloaded as one file with progress"]

request.download(parameters: params, success: { response in

    if let data = response.data { }

    if let url = response.url { 
        let image = UIImage(contentsOfFile: url.path)
    }

}, failure: { error in

}, progress: { progress in

    print("Download progress (progress)")

})

Support

If you encounter a problem, please open a new issue. Also, pull requests are always welcome!

Latest podspec

{
    "name": "HTTPLite",
    "version": "1.1.1",
    "summary": "A simple Swift 3.x wrapper for URLSession",
    "description": "This library provides an easy entry to using HTTP with Swift 3. It is based on the philosophy of providing a simple API that avoids the overhead of using fully-fledged libraries.",
    "homepage": "https://github.com/nawar/HTTPLite",
    "license": "MIT",
    "authors": {
        "Nawar Nory": "[email protected]"
    },
    "social_media_url": "http://linkedin.com/profile/in/nnory",
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/nawar/HTTPLite.git",
        "tag": "v1.1.1"
    },
    "source_files": "Classes",
    "exclude_files": "Classes/*.h",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This