|
|
@@ -24,60 +24,43 @@
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
-/// Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to
|
|
|
-/// construct URL requests.
|
|
|
-public protocol URLStringConvertible {
|
|
|
- /// A URL string that conforms to RFC 2396.
|
|
|
+/// Types adopting the `URLConvertible` protocol can be used to construct URLs, which are then used to construct
|
|
|
+/// URL requests.
|
|
|
+public protocol URLConvertible {
|
|
|
+ /// Returns a URL that conforms to RFC 2396 or throws an `Error`.
|
|
|
///
|
|
|
- /// Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
|
|
|
+ /// - throws: An `Error` if the type cannot be converted to a `URL`.
|
|
|
///
|
|
|
- /// See https://tools.ietf.org/html/rfc2396
|
|
|
- /// See https://tools.ietf.org/html/rfc1738
|
|
|
- /// See https://tools.ietf.org/html/rfc1808
|
|
|
- var urlString: String? { get }
|
|
|
+ /// - returns: A URL or throws an `Error`.
|
|
|
+ func asURL() throws -> URL
|
|
|
+}
|
|
|
|
|
|
- /// Returns a URL string that conforms to RFC 2396 or throws if an `Error` was encoutered.
|
|
|
- ///
|
|
|
- /// Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
|
|
|
+extension String: URLConvertible {
|
|
|
+ /// Returns a URL if `self` represents a valid URL string that conforms to RFC 2396 or throws an `AFError`.
|
|
|
///
|
|
|
- /// - throws: An `Error` if underlying url string is `nil`.
|
|
|
+ /// - throws: An `AFError.invalidURL` if `self` is not a valid URL string.
|
|
|
///
|
|
|
- /// - returns: A URL string.
|
|
|
- func asURLString() throws -> String
|
|
|
-}
|
|
|
-
|
|
|
-extension URLStringConvertible {
|
|
|
- public var urlString: String? { return try? asURLString() }
|
|
|
+ /// - returns: A URL or throws an `AFError`.
|
|
|
+ public func asURL() throws -> URL {
|
|
|
+ guard let url = URL(string: self) else { throw AFError.invalidURL(url: self) }
|
|
|
+ return url
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-extension String: URLStringConvertible {
|
|
|
- /// Returns a URL string that conforms to RFC 2396.
|
|
|
- public func asURLString() throws -> String { return self }
|
|
|
+extension URL: URLConvertible {
|
|
|
+ /// Returns self.
|
|
|
+ public func asURL() throws -> URL { return self }
|
|
|
}
|
|
|
|
|
|
-extension URL: URLStringConvertible {
|
|
|
- /// Returns a URL string that conforms to RFC 2396.
|
|
|
- public func asURLString() throws -> String { return absoluteString }
|
|
|
-
|
|
|
- /// Creates a URL with the specified URL string if possible, otherwise throws an `Error`.
|
|
|
+extension URLComponents: URLConvertible {
|
|
|
+ /// Returns a URL if `url` is not nil, otherise throws an `Error`.
|
|
|
///
|
|
|
- /// - parameter urlString: The URL string convertible to create the URL with.
|
|
|
+ /// - throws: An `AFError.invalidURL` if `url` is `nil`.
|
|
|
///
|
|
|
- /// - throws: An `AFError.invalidURLString` if invalid.
|
|
|
- ///
|
|
|
- /// - returns: A URL if created successfully, otherwise throws an `AFError`.
|
|
|
- public init(urlString: URLStringConvertible) throws {
|
|
|
- let urlString = try urlString.asURLString()
|
|
|
- guard let url = URL(string: urlString) else { throw AFError.invalidURLString(urlString: urlString) }
|
|
|
- self = url
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-extension URLComponents: URLStringConvertible {
|
|
|
- /// Returns a URL string that conforms to RFC 2396.
|
|
|
- public func asURLString() throws -> String {
|
|
|
- guard let urlString = url?.urlString else { throw AFError.invalidURLString(urlString: self) }
|
|
|
- return urlString
|
|
|
+ /// - returns: A URL or throws an `AFError`.
|
|
|
+ public func asURL() throws -> URL {
|
|
|
+ guard let url = url else { throw AFError.invalidURL(url: self) }
|
|
|
+ return url
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -111,13 +94,13 @@ extension URLRequest: URLRequestConvertible {
|
|
|
extension URLRequest {
|
|
|
/// Creates an instance with the specified `method`, `urlString` and `headers`.
|
|
|
///
|
|
|
- /// - parameter urlString: The URL string.
|
|
|
- /// - parameter method: The HTTP method.
|
|
|
- /// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
+ /// - parameter url: The URL.
|
|
|
+ /// - parameter method: The HTTP method.
|
|
|
+ /// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
///
|
|
|
/// - returns: The new `URLRequest` instance.
|
|
|
- public init(urlString: URLStringConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) throws {
|
|
|
- let url = try URL(urlString: urlString)
|
|
|
+ public init(url: URLConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) throws {
|
|
|
+ let url = try url.asURL()
|
|
|
|
|
|
self.init(url: url)
|
|
|
|
|
|
@@ -138,10 +121,10 @@ extension URLRequest {
|
|
|
|
|
|
// MARK: - Data Request
|
|
|
|
|
|
-/// Creates a `DataRequest` using the default `SessionManager` to retrieve the contents of a URL based on the
|
|
|
-/// specified `urlString`, `method`, `parameters`, `encoding` and `headers`.
|
|
|
+/// Creates a `DataRequest` using the default `SessionManager` to retrieve the contents of the specified `url`,
|
|
|
+/// `method`, `parameters`, `encoding` and `headers`.
|
|
|
///
|
|
|
-/// - parameter urlString: The URL string.
|
|
|
+/// - parameter url: The URL.
|
|
|
/// - parameter method: The HTTP method. `.get` by default.
|
|
|
/// - parameter parameters: The parameters. `nil` by default.
|
|
|
/// - parameter encoding: The parameter encoding. `URLEncoding.default` by default.
|
|
|
@@ -150,7 +133,7 @@ extension URLRequest {
|
|
|
/// - returns: The created `DataRequest`.
|
|
|
@discardableResult
|
|
|
public func request(
|
|
|
- _ urlString: URLStringConvertible,
|
|
|
+ _ url: URLConvertible,
|
|
|
method: HTTPMethod = .get,
|
|
|
parameters: Parameters? = nil,
|
|
|
encoding: ParameterEncoding = URLEncoding.default,
|
|
|
@@ -158,7 +141,7 @@ public func request(
|
|
|
-> DataRequest
|
|
|
{
|
|
|
return SessionManager.default.request(
|
|
|
- urlString,
|
|
|
+ url,
|
|
|
method: method,
|
|
|
parameters: parameters,
|
|
|
encoding: encoding,
|
|
|
@@ -181,13 +164,13 @@ public func request(_ urlRequest: URLRequestConvertible) -> DataRequest {
|
|
|
|
|
|
// MARK: URL Request
|
|
|
|
|
|
-/// Creates a `DownloadRequest` using the default `SessionManager` to retrieve the contents of a URL based on the
|
|
|
-/// specified `urlString`, `method`, `parameters`, `encoding`, `headers` and save them to the `destination`.
|
|
|
+/// Creates a `DownloadRequest` using the default `SessionManager` to retrieve the contents of the specified `url`,
|
|
|
+/// `method`, `parameters`, `encoding`, `headers` and save them to the `destination`.
|
|
|
///
|
|
|
/// If `destination` is not specified, the contents will remain in the temporary location determined by the
|
|
|
/// underlying URL session.
|
|
|
///
|
|
|
-/// - parameter urlString: The URL string.
|
|
|
+/// - parameter url: The URL.
|
|
|
/// - parameter method: The HTTP method. `.get` by default.
|
|
|
/// - parameter parameters: The parameters. `nil` by default.
|
|
|
/// - parameter encoding: The parameter encoding. `URLEncoding.default` by default.
|
|
|
@@ -197,7 +180,7 @@ public func request(_ urlRequest: URLRequestConvertible) -> DataRequest {
|
|
|
/// - returns: The created `DownloadRequest`.
|
|
|
@discardableResult
|
|
|
public func download(
|
|
|
- _ urlString: URLStringConvertible,
|
|
|
+ _ url: URLConvertible,
|
|
|
method: HTTPMethod = .get,
|
|
|
parameters: Parameters? = nil,
|
|
|
encoding: ParameterEncoding = URLEncoding.default,
|
|
|
@@ -206,7 +189,7 @@ public func download(
|
|
|
-> DownloadRequest
|
|
|
{
|
|
|
return SessionManager.default.download(
|
|
|
- urlString,
|
|
|
+ url,
|
|
|
method: method,
|
|
|
parameters: parameters,
|
|
|
encoding: encoding,
|
|
|
@@ -261,24 +244,24 @@ public func download(
|
|
|
|
|
|
// MARK: File
|
|
|
|
|
|
-/// Creates an `UploadRequest` using the default `SessionManager` from the specified `method`, `urlString`
|
|
|
-/// and `headers` for uploading the `file`.
|
|
|
+/// Creates an `UploadRequest` using the default `SessionManager` from the specified `url`, `method` and `headers`
|
|
|
+/// for uploading the `file`.
|
|
|
///
|
|
|
-/// - parameter file: The file to upload.
|
|
|
-/// - parameter urlString: The URL string.
|
|
|
-/// - parameter method: The HTTP method. `.post` by default.
|
|
|
-/// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
+/// - parameter file: The file to upload.
|
|
|
+/// - parameter url: The URL.
|
|
|
+/// - parameter method: The HTTP method. `.post` by default.
|
|
|
+/// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
///
|
|
|
/// - returns: The created `UploadRequest`.
|
|
|
@discardableResult
|
|
|
public func upload(
|
|
|
_ fileURL: URL,
|
|
|
- to urlString: URLStringConvertible,
|
|
|
+ to url: URLConvertible,
|
|
|
method: HTTPMethod = .post,
|
|
|
headers: HTTPHeaders? = nil)
|
|
|
-> UploadRequest
|
|
|
{
|
|
|
- return SessionManager.default.upload(fileURL, to: urlString, method: method, headers: headers)
|
|
|
+ return SessionManager.default.upload(fileURL, to: url, method: method, headers: headers)
|
|
|
}
|
|
|
|
|
|
/// Creates a `UploadRequest` using the default `SessionManager` from the specified `urlRequest` for
|
|
|
@@ -295,24 +278,24 @@ public func upload(_ fileURL: URL, with urlRequest: URLRequestConvertible) -> Up
|
|
|
|
|
|
// MARK: Data
|
|
|
|
|
|
-/// Creates an `UploadRequest` using the default `SessionManager` from the specified `method`, `urlString`
|
|
|
-/// and `headers` for uploading the `data`.
|
|
|
+/// Creates an `UploadRequest` using the default `SessionManager` from the specified `url`, `method` and `headers`
|
|
|
+/// for uploading the `data`.
|
|
|
///
|
|
|
-/// - parameter data: The data to upload.
|
|
|
-/// - parameter urlString: The URL string.
|
|
|
-/// - parameter method: The HTTP method. `.post` by default.
|
|
|
-/// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
+/// - parameter data: The data to upload.
|
|
|
+/// - parameter url: The URL.
|
|
|
+/// - parameter method: The HTTP method. `.post` by default.
|
|
|
+/// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
///
|
|
|
/// - returns: The created `UploadRequest`.
|
|
|
@discardableResult
|
|
|
public func upload(
|
|
|
_ data: Data,
|
|
|
- to urlString: URLStringConvertible,
|
|
|
+ to url: URLConvertible,
|
|
|
method: HTTPMethod = .post,
|
|
|
headers: HTTPHeaders? = nil)
|
|
|
-> UploadRequest
|
|
|
{
|
|
|
- return SessionManager.default.upload(data, to: urlString, method: method, headers: headers)
|
|
|
+ return SessionManager.default.upload(data, to: url, method: method, headers: headers)
|
|
|
}
|
|
|
|
|
|
/// Creates an `UploadRequest` using the default `SessionManager` from the specified `urlRequest` for
|
|
|
@@ -329,24 +312,24 @@ public func upload(_ data: Data, with urlRequest: URLRequestConvertible) -> Uplo
|
|
|
|
|
|
// MARK: InputStream
|
|
|
|
|
|
-/// Creates an `UploadRequest` using the default `SessionManager` from the specified `method`, `urlString`
|
|
|
-/// and `headers` for uploading the `stream`.
|
|
|
+/// Creates an `UploadRequest` using the default `SessionManager` from the specified `url`, `method` and `headers`
|
|
|
+/// for uploading the `stream`.
|
|
|
///
|
|
|
-/// - parameter stream: The stream to upload.
|
|
|
-/// - parameter urlString: The URL string.
|
|
|
-/// - parameter method: The HTTP method. `.post` by default.
|
|
|
-/// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
+/// - parameter stream: The stream to upload.
|
|
|
+/// - parameter url: The URL.
|
|
|
+/// - parameter method: The HTTP method. `.post` by default.
|
|
|
+/// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
///
|
|
|
/// - returns: The created `UploadRequest`.
|
|
|
@discardableResult
|
|
|
public func upload(
|
|
|
_ stream: InputStream,
|
|
|
- to urlString: URLStringConvertible,
|
|
|
+ to url: URLConvertible,
|
|
|
method: HTTPMethod = .post,
|
|
|
headers: HTTPHeaders? = nil)
|
|
|
-> UploadRequest
|
|
|
{
|
|
|
- return SessionManager.default.upload(stream, to: urlString, method: method, headers: headers)
|
|
|
+ return SessionManager.default.upload(stream, to: url, method: method, headers: headers)
|
|
|
}
|
|
|
|
|
|
/// Creates an `UploadRequest` using the default `SessionManager` from the specified `urlRequest` for
|
|
|
@@ -364,7 +347,7 @@ public func upload(_ stream: InputStream, with urlRequest: URLRequestConvertible
|
|
|
// MARK: MultipartFormData
|
|
|
|
|
|
/// Encodes `multipartFormData` using `encodingMemoryThreshold` with the default `SessionManager` and calls
|
|
|
-/// `encodingCompletion` with new `UploadRequest` using the `method`, `urlString` and `headers`.
|
|
|
+/// `encodingCompletion` with new `UploadRequest` using the `url`, `method` and `headers`.
|
|
|
///
|
|
|
/// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
|
|
|
/// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
|
|
|
@@ -382,14 +365,14 @@ public func upload(_ stream: InputStream, with urlRequest: URLRequestConvertible
|
|
|
/// - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
|
|
|
/// - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
|
|
|
/// `multipartFormDataEncodingMemoryThreshold` by default.
|
|
|
-/// - parameter urlString: The URL string.
|
|
|
+/// - parameter url: The URL.
|
|
|
/// - parameter method: The HTTP method. `.post` by default.
|
|
|
/// - parameter headers: The HTTP headers. `nil` by default.
|
|
|
/// - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
|
|
|
public func upload(
|
|
|
multipartFormData: @escaping (MultipartFormData) -> Void,
|
|
|
usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
|
|
|
- to urlString: URLStringConvertible,
|
|
|
+ to url: URLConvertible,
|
|
|
method: HTTPMethod = .post,
|
|
|
headers: HTTPHeaders? = nil,
|
|
|
encodingCompletion: ((SessionManager.MultipartFormDataEncodingResult) -> Void)?)
|
|
|
@@ -397,7 +380,7 @@ public func upload(
|
|
|
return SessionManager.default.upload(
|
|
|
multipartFormData: multipartFormData,
|
|
|
usingThreshold: encodingMemoryThreshold,
|
|
|
- to: urlString,
|
|
|
+ to: url,
|
|
|
method: method,
|
|
|
headers: headers,
|
|
|
encodingCompletion: encodingCompletion
|