Skip to content

nawar/HTTPLite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# 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 👍

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!