This is a wrapper for the networking library HttpRequest. It aims to make it easier to use by wrapping the API in a more familiar interface.
The alamofire wrapper swift 5 is a HTTP request wrapper written in Swift. It allows developers to create HTTP requests with ease and without the need for any external libraries.
Net is a Swift wrapper for HttpRequest.
Features
- Methods GET, POST, PUT, and DELETE
- Nested params, integer, text, dic, array, picture, data are all useful request parameters.
- Responses in Json, Image, and XML
- Files to download: continue, suspend, and cancel
- Upload the file, the data, and the parameters (multi-part)
- Completion of the project
- Download the background, then upload it.
- Authentication
- Operations in a batch
- BaseURL
- Header that may be customized
Demo app
Usage
To build a Net instance, use one of the techniques listed below.
/ let net = Net() without baseURL / let net = Net(baseUrlString: “http://www.puqiz.com/”) with baseURL
HttpRequest
Request for a GET
let url = “get path” let url = “get path” let url = “get_ [“integerNumber”: 1, “doubleNumber”: 2.0, “string”: “hello”] let params = net.GET(url, params: params, success); net.GET(url, params: params, success); net.GET(url let result = responseData.json(error: nil) NSLog(“result (result)”) NSLog(“result (result)”) NSLog(“result (result)”) NSLog(“result (result)”) NSLog(“result (result Error in NSLog(“Error”)) Handler: Let url = “http://www.puqiz.com/get path” / You may also make a request using an absolute url. net.GET(absoluteUrl: url, params: params, success); net.GET(absoluteUrl: url, params: params, success); net.GET(absolu let result = responseData.json(error: nil) NSLog(“result (result)”) NSLog(“result (result)”) NSLog(“result (result)”) NSLog(“result (result)”) NSLog(“result (result Error in NSLog(“Error”)) Handler:
You may also make advantage of nested parameters.
“string”: “test”, “integerNumber”: 1, “floatNumber”: 1.5, “array”: [10, 20, 30], “dictionary”: [“x”: 100.0, “y”: 200.0], “image”: NetData(pngImage: img, filename: “myIcon”)]
You may easily use responseData in the sucessHandler closure.
- get the json dictionary
- get an image
- xml parse
Requests such as GET, POST, PUT, and DELETE are supported.
let jsonDic = responseData.json / obtain json dictionary from response data (error: error) let image = responseData.image = retrieve image from response data () let result = responseData.parseXml / parse xml using delegate (delegate: self)
Request for a POST
Net will automatically examine your parameters to determine whether to submit the request as a URL-Encoded or Multi-Part request. As a result, you may quickly submit data in the form of a number, text, picture, or binary data.
let post path = url [“string”: “test”, “integerNumber”: 1, “floatNumber”: 1] let params = .5] net.POST(url, params: params, success); net.POST(url, params: params, success); net.POST( let result = responseData.json(error: nil) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (re Error in NSLog(“Error”)) Handler:
let post path = url img = UIImage(“puqiz icon”); [“string”: “test”, “integerNumber”: 1, “icon”: NetData(pngImage: img, filename: “myIcon”)] let params = [“string”: “test”, “integerNumber”: 1, “icon”: NetData(pngImage: img, filename: “myIcon”)] net. GET(url, params: params, success) GET(url, params: params, success) GET(url, let result = responseData.json(error: nil) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (re Error in NSLog(“Error”)) Handler:
Request for a PUT
let put path = “url” [“string”: “test”, “integerNumber”: 1, “floatNumber”: 1] let params = .5] net.PUT(url, params: params, success); net.PUT(url, params: params, success); net.PUT( let result = responseData.json(error: nil) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (result)”) NSLog(“result: (re Error in NSLog(“Error”)) Handler:
REQUEST FOR DELETION
let delete path = url [“id”: 10] let params = net. DELETE(url, params: params, success) DELETE(url, params: params, success) DELETE(url failure Handler: responseData in NSLog(“result: (result)”) Error in NSLog(“Error”)) Handler:
Task
You must first use the setupSession method to establish up the session before utilizing the download/upload functions.
net.setupSession::setupSession::setupSession::setupSession::setupS ()
You must use the setupSession function with a background identification string to conduct background downloads or uploads. Your download/upload tasks will then be able to continue even if the program is stopped, exited, or crashes.
/ establish session with backgroundIdentifier net.setupSession(backgroundIdentifier: “com.nghialv.download”) (backgroundIdentifier: “com.nghialv.download”) You may use the eventsForBackgroundHandler closure to set events for the background handler. When a job in the background net is finished, this closure will be called. urlSession in urlSession.getDownloadingTasksCount = eventsForBackgroundHandler if downloadingTaskCount == 0; if downloadingTaskCount == 0; if downloadingTaskCount == 0; if downloadingTaskCount == NSLog(“All files have been downloaded!”) NSLog(“All files have been downloaded!”) NSLog(“All files have been downloaded!”) NSLog(“All files have been downloaded!”
Download
allow for download net is the task. download(absoluteUrl: url, progress: progress in NSLog(“progress (progress)”), completionHandler: completionHandler: completionHandler: completionHandler: completionHandler: completionHandler: completionHandler: completionHandler: completionHandler: completionHandler: completionHandler fileUrl, error in if error!= nil NSLog(“Download failed”) NSLog(“Downloaded to: (fileUrl)”) NSLog(“Downloaded to: (fileUrl)”) NSLog(“Downloaded to: (fileUrl)”) NSLog(“Downloaded to: (fileUrl)”) NSLog(“Downloaded to: (fileUrl)”) NSLog( download Download using Task.resume() Task.suspend() is a method for suspending a task. Task.cancel()
Upload
net.upload(absoluteUrl: url, from) let task = net.upload(absoluteUrl: url, from) progress, file: file Handler: completion in NSLog(“progress (progress)”), progress in NSLog(“progress (progress)”) NSLog(“Upload failed : (error)”) if error!= nil, then NSLog(“Upload finished”) if error!= nil.
Allow yourData to be equal to NSData(…) net. (absoluteUrl: url, data: yourData, progress) upload(absoluteUrl: url, data: yourData, progress) Handler: completion in NSLog(“progress: (progress)”), progress in NSLog(“progress: (progress)”) Handler: NSLog(“Upload finished”) error)
image = UIImage(“image file”) imageData = UIImagePNGRepresentation; (image) [“number”: 1, “string”: “net”, “data”: imageData] let params = net.upload(absoluteUrl: imgUrl, params: params, progress) net.upload(absoluteUrl: imgUrl, params: params, progress) net.upload( Handler: completion in NSLog(“progress: (progress)”), progress in NSLog(“progress: (progress)”) Handler: NSLog(“Upload finished”) error)
The upload job will be done via the POST method by default.
- Application/octet-stream is the content type (upload with file or data)
- multipart/form-data is the content type (upload with params)
However, before restarting, you may set the upload job.
setHttpMethod yourUploadTask.setHttpMethod yourUploadTask.setHttpMethod yourUploadT (.PUT) Set the header field using yourUploadTask.setValue(value: “your value,” forHttpHeaderField: “header field”)
Integration
Simply drop the Net folder into the project tree.
GitHub
https://github.com/nghialv/Net
The alamofire request url is a Swift wrapper for the HttpRequest class. It allows users to make http requests without having to use NSURLConnection.
Related Tags
- swift api call library
- alamofire put request swift 5
- urlsession with parameters swift
- alamofire 5 session
- how to make http get request with json body in swift
More Stories
Why has the Russian Federation been excluded from the Council of Europe?
EUAA Report on Russian Military Service
Prescription Drug Shortages in the U.S.