The framework is designed to reduce the amount of repeative boiler plate code which is required to make a URLSession request for a url string.
By using this framework you can really go from writing 20 lines of code to basically one if you are only interested in the Data which returns from the request.
-
- Create DataRequest instance using a String
- Create DataRequest instance using a URL
- Inferring URL is secure
- Using DataRequestResult
- Asynchronous Data Request with completion handler
- Asynchronous DataRequest using the value property of DataRequestResult
- Synchronous DataRequest
- Optional Data using Synchronous DataRequest
- String's Extensions
- URL's Extensions
- Xcode 9.2
- Minimum iOS Deploment Target 9.0
- Swift 4.0
Tested with carthage version: 0.26.2
Add this to Cartfile
git "https://github.com/davidthorn/DataRequest.git"
$ carthage updateTested with swift build --version: Swift 4.0.0-dev (swiftpm-13126)
Create a Package.swift file.
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "PACKAGE_NAME",
dependencies: [
.package(url: "https://github.com/davidthorn/DataRequest.git", from: "2.0.0")
],
targets: [
.target(name: "PACKAGE_NAME", dependencies: [
"DataRequest"
])
]
)$ swift build
/// Creates an optional DataRequest object
let dataRequest: DataRequest? = DataRequest(urlString: "https://github.com/davidthorn/DataRequest.git")let url = URL(string: "https://github.com/davidthorn/DataRequest.git")
let dataRequest: DataRequest = DataRequest(url: url)
..The https:// scheme can be left out of the url because it will be inferred that this is a secure request.
let url = URL(string: "github.com/davidthorn/DataRequest.git")
let dataRequest: DataRequest = DataRequest(url: url)
..All methods in the DataRequest framework will return a DataRequestResult.
A DataRequestResult is an enum containing 2 possible responses:
public enum DataRequestResult {
// Used only if the data request returns Data and a URLResponse
case success(Data, URLResponse)
/// Used for all other situations where success is not used
case fail(Error)
}In this example the DataRequestResult has been used with a switch case.
import Foundation
import DataRequest
let dataRequest = DataRequest(urlString: "github.com/davidthorn/DataRequest.git")
dataRequest.data { (result: DataRequestResult) in
switch result {
case .success(let data: Data , let response: URLResponse):
// use the data
case .fail(let error):
// an error has occurred
}
}If you are not interested in handling the error case, then you can access the value: Data? property of
the DataRequestResult directly
import Foundation
import DataRequest
let dataRequest = DataRequest(urlString: "github.com/davidthorn/DataRequest.git")
dataRequest.data { (result: DataRequestResult) in
// check if the result has data if so then the it will return here with a value
guard let data: Data = result.value else { return }
}If this request should be completed synchronously then you can use the sync property of the DataRequest to retrieve the DataRequestResult.
import Foundation
import DataRequest
let dataRequest = DataRequest(urlString: "github.com/davidthorn/DataRequest.git")
let dataResult: DataRequestResult = dataRequest.sync
Once again you can use the value: Data? property of the DataRequestResult whilst using the sync property of the DataRequest.
import Foundation
import DataRequest
let dataRequest: DataRequest = DataRequest(urlString: "github.com/davidthorn/DataRequest.git")
guard let data: Data = dataRequest.sync.value else {
// not data has been return
return
}To make this process even shorter you can use the String's syncURLRequest property to execute this DataTask inline and synchronously
import Foundation
import DataRequest
// Option 1
let dataResult: DataRequestResult = "github.com/davidthorn/DataRequest.git".syncURLRequest
let data: Data? = dataResult.value
// Option 2
let dataResult: Data? = "github.com/davidthorn/DataRequest.git".syncURLRequest.value
// Option 3 - Do the request synchronously with a completion handler
"github.com/davidthorn/DataRequest.git".urlSyncRequest { result in
guard let data: Data? = result.value else { return }
}
// Option 3 - Do the request Asynchronously with a completion handler
"github.com/davidthorn/DataRequest.git".urlAsyncRequest { result in
guard let data: Data? = result.value else { return }
}To make this process even shorter you can use the URL's syncURLRequest property to execute this DataTask inline and synchronously
import Foundation
import DataRequest
let secureString = "github.com/davidthorn/DataRequest.git".asSecureURLString
let url = URL(string: secureString)!
// Option 1
let dataResult: DataRequestResult = url.sync
let data: Data? = dataResult.value
// Option 2
let dataResult: Data? = url.sync.value
// Option 3 - Do the request synchronously with a completion handler
url.sync { result in
guard let data: Data? = result.value else { return }
}
// Option 3 - Do the request Asynchronously with a completion handler
url.async { result in
guard let data: Data? = result.value else { return }
}