Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  URLConvertible+URLRequestConvertible.swift
3
//
4
//  Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
5
//
6
//  Permission is hereby granted, free of charge, to any person obtaining a copy
7
//  of this software and associated documentation files (the "Software"), to deal
8
//  in the Software without restriction, including without limitation the rights
9
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
//  copies of the Software, and to permit persons to whom the Software is
11
//  furnished to do so, subject to the following conditions:
12
//
13
//  The above copyright notice and this permission notice shall be included in
14
//  all copies or substantial portions of the Software.
15
//
16
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
//  THE SOFTWARE.
23
//
24
 
25
import Foundation
26
 
27
/// Types adopting the `URLConvertible` protocol can be used to construct `URL`s, which can then be used to construct
28
/// `URLRequests`.
29
public protocol URLConvertible {
30
    /// Returns a `URL` from the conforming instance or throws.
31
    ///
32
    /// - Returns: The `URL` created from the instance.
33
    /// - Throws:  Any error thrown while creating the `URL`.
34
    func asURL() throws -> URL
35
}
36
 
37
extension String: URLConvertible {
38
    /// Returns a `URL` if `self` can be used to initialize a `URL` instance, otherwise throws.
39
    ///
40
    /// - Returns: The `URL` initialized with `self`.
41
    /// - Throws:  An `AFError.invalidURL` instance.
42
    public func asURL() throws -> URL {
43
        guard let url = URL(string: self) else { throw AFError.invalidURL(url: self) }
44
 
45
        return url
46
    }
47
}
48
 
49
extension URL: URLConvertible {
50
    /// Returns `self`.
51
    public func asURL() throws -> URL { self }
52
}
53
 
54
extension URLComponents: URLConvertible {
55
    /// Returns a `URL` if the `self`'s `url` is not nil, otherwise throws.
56
    ///
57
    /// - Returns: The `URL` from the `url` property.
58
    /// - Throws:  An `AFError.invalidURL` instance.
59
    public func asURL() throws -> URL {
60
        guard let url = url else { throw AFError.invalidURL(url: self) }
61
 
62
        return url
63
    }
64
}
65
 
66
// MARK: -
67
 
68
/// Types adopting the `URLRequestConvertible` protocol can be used to safely construct `URLRequest`s.
69
public protocol URLRequestConvertible {
70
    /// Returns a `URLRequest` or throws if an `Error` was encountered.
71
    ///
72
    /// - Returns: A `URLRequest`.
73
    /// - Throws:  Any error thrown while constructing the `URLRequest`.
74
    func asURLRequest() throws -> URLRequest
75
}
76
 
77
extension URLRequestConvertible {
78
    /// The `URLRequest` returned by discarding any `Error` encountered.
79
    public var urlRequest: URLRequest? { try? asURLRequest() }
80
}
81
 
82
extension URLRequest: URLRequestConvertible {
83
    /// Returns `self`.
84
    public func asURLRequest() throws -> URLRequest { self }
85
}
86
 
87
// MARK: -
88
 
89
extension URLRequest {
90
    /// Creates an instance with the specified `url`, `method`, and `headers`.
91
    ///
92
    /// - Parameters:
93
    ///   - url:     The `URLConvertible` value.
94
    ///   - method:  The `HTTPMethod`.
95
    ///   - headers: The `HTTPHeaders`, `nil` by default.
96
    /// - Throws:    Any error thrown while converting the `URLConvertible` to a `URL`.
97
    public init(url: URLConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) throws {
98
        let url = try url.asURL()
99
 
100
        self.init(url: url)
101
 
102
        httpMethod = method.rawValue
103
        allHTTPHeaderFields = headers?.dictionary
104
    }
105
}