|
| 1 | +// |
| 2 | +// URL+.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Zach Eriksen on 11/20/20. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public extension URL { |
| 11 | + |
| 12 | + // MARK: URLSession DataTasks |
| 13 | + |
| 14 | + var dataTask: URLSessionDataTask { |
| 15 | + URLSession.shared.dataTask(with: self) |
| 16 | + } |
| 17 | + |
| 18 | + @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) |
| 19 | + var dataTaskPublisher: URLSession.DataTaskPublisher { |
| 20 | + URLSession.shared.dataTaskPublisher(for: self) |
| 21 | + } |
| 22 | + |
| 23 | + func dataTask(withHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask { |
| 24 | + URLSession.shared.dataTask(with: self, completionHandler: handler) |
| 25 | + } |
| 26 | + |
| 27 | + // MARK: URLRequest |
| 28 | + |
| 29 | + func urlRequest( |
| 30 | + forHTTPMethod httpMethod: URLRequest.HTTPRequestMethod? = .GET, |
| 31 | + cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy, |
| 32 | + timeoutInterval: TimeInterval = 60.0 |
| 33 | + ) -> URLRequest { |
| 34 | + var request = URLRequest(url: self, |
| 35 | + cachePolicy: cachePolicy, |
| 36 | + timeoutInterval: timeoutInterval) |
| 37 | + |
| 38 | + request.httpMethod = httpMethod?.rawValue |
| 39 | + |
| 40 | + return request |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// MARK: URL HTTPRequestMethod DataTasks |
| 45 | +public extension URL { |
| 46 | + |
| 47 | + func get( |
| 48 | + withHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void |
| 49 | + ) { |
| 50 | + urlRequest(forHTTPMethod: .GET).dataTask(withHandler: handler).resume() |
| 51 | + } |
| 52 | + |
| 53 | + func head( |
| 54 | + withHandler handler: @escaping (URLResponse?, Error?) -> Void |
| 55 | + ) { |
| 56 | + urlRequest(forHTTPMethod: .HEAD).dataTask { (data, response, error) in |
| 57 | + handler(response, error) |
| 58 | + }.resume() |
| 59 | + } |
| 60 | + |
| 61 | + func connect( |
| 62 | + withHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void |
| 63 | + ) { |
| 64 | + urlRequest(forHTTPMethod: .CONNECT).dataTask(withHandler: handler).resume() |
| 65 | + } |
| 66 | + |
| 67 | + func options( |
| 68 | + withHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void |
| 69 | + ) { |
| 70 | + urlRequest(forHTTPMethod: .OPTIONS).dataTask(withHandler: handler).resume() |
| 71 | + } |
| 72 | + |
| 73 | + func trace( |
| 74 | + withHandler handler: @escaping (URLResponse?, Error?) -> Void |
| 75 | + ) { |
| 76 | + urlRequest(forHTTPMethod: .TRACE).dataTask { (data, response, error) in |
| 77 | + handler(response, error) |
| 78 | + }.resume() |
| 79 | + } |
| 80 | + |
| 81 | + func post( |
| 82 | + data: Data?, |
| 83 | + withHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void |
| 84 | + ) { |
| 85 | + var request = urlRequest(forHTTPMethod: .POST) |
| 86 | + |
| 87 | + request.httpBody = data |
| 88 | + |
| 89 | + request.dataTask(withHandler: handler).resume() |
| 90 | + } |
| 91 | + |
| 92 | + func put( |
| 93 | + data: Data?, |
| 94 | + withHandler handler: @escaping (URLResponse?, Error?) -> Void |
| 95 | + ) { |
| 96 | + var request = urlRequest(forHTTPMethod: .PUT) |
| 97 | + |
| 98 | + request.httpBody = data |
| 99 | + |
| 100 | + request.dataTask { (data, response, error) in |
| 101 | + handler(response, error) |
| 102 | + }.resume() |
| 103 | + } |
| 104 | + |
| 105 | + func patch( |
| 106 | + data: Data?, |
| 107 | + withHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void |
| 108 | + ) { |
| 109 | + var request = urlRequest(forHTTPMethod: .PATCH) |
| 110 | + |
| 111 | + request.httpBody = data |
| 112 | + |
| 113 | + request.dataTask(withHandler: handler).resume() |
| 114 | + } |
| 115 | + |
| 116 | + func delete( |
| 117 | + data: Data?, |
| 118 | + withHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void |
| 119 | + ) { |
| 120 | + var request = urlRequest(forHTTPMethod: .DELETE) |
| 121 | + |
| 122 | + request.httpBody = data |
| 123 | + |
| 124 | + request.dataTask(withHandler: handler).resume() |
| 125 | + } |
| 126 | + |
| 127 | +} |
| 128 | + |
| 129 | +// MARK: URL HTTPRequestMethod DataTaskPublishers |
| 130 | +@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) |
| 131 | +public extension URL { |
| 132 | + |
| 133 | + func get() -> URLSession.DataTaskPublisher { |
| 134 | + urlRequest(forHTTPMethod: .GET).dataTaskPublisher |
| 135 | + } |
| 136 | + |
| 137 | + func head() -> URLSession.DataTaskPublisher { |
| 138 | + urlRequest(forHTTPMethod: .HEAD).dataTaskPublisher |
| 139 | + } |
| 140 | + |
| 141 | + func connect() -> URLSession.DataTaskPublisher { |
| 142 | + urlRequest(forHTTPMethod: .CONNECT).dataTaskPublisher |
| 143 | + } |
| 144 | + |
| 145 | + func options() -> URLSession.DataTaskPublisher { |
| 146 | + urlRequest(forHTTPMethod: .OPTIONS).dataTaskPublisher |
| 147 | + } |
| 148 | + |
| 149 | + func trace() -> URLSession.DataTaskPublisher { |
| 150 | + urlRequest(forHTTPMethod: .TRACE).dataTaskPublisher |
| 151 | + } |
| 152 | + |
| 153 | + func post(data: Data?) -> URLSession.DataTaskPublisher { |
| 154 | + var request = urlRequest(forHTTPMethod: .POST) |
| 155 | + |
| 156 | + request.httpBody = data |
| 157 | + |
| 158 | + return request.dataTaskPublisher |
| 159 | + } |
| 160 | + |
| 161 | + func put(data: Data?) -> URLSession.DataTaskPublisher { |
| 162 | + var request = urlRequest(forHTTPMethod: .PUT) |
| 163 | + |
| 164 | + request.httpBody = data |
| 165 | + |
| 166 | + return request.dataTaskPublisher |
| 167 | + } |
| 168 | + |
| 169 | + func patch(data: Data?) -> URLSession.DataTaskPublisher { |
| 170 | + var request = urlRequest(forHTTPMethod: .PATCH) |
| 171 | + |
| 172 | + request.httpBody = data |
| 173 | + |
| 174 | + return request.dataTaskPublisher |
| 175 | + } |
| 176 | + |
| 177 | + func delete(data: Data?) -> URLSession.DataTaskPublisher { |
| 178 | + var request = urlRequest(forHTTPMethod: .DELETE) |
| 179 | + |
| 180 | + request.httpBody = data |
| 181 | + |
| 182 | + return request.dataTaskPublisher |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments