Skip to content

Commit 0f85b8a

Browse files
committed
init poc
1 parent d295188 commit 0f85b8a

File tree

6 files changed

+259
-11
lines changed

6 files changed

+259
-11
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
# SURL
22

3-
A description of this package.
3+
Swifty URL
4+
5+
6+
```swift
7+
"https://raw.githubusercontent.com/0xLeif/0xLeif/master/README.md"
8+
.url?
9+
.get { (data, response, error) in
10+
11+
print(String(data: data!, encoding: .utf8))
12+
13+
}
14+
```

Sources/SURL/SURL.swift

Lines changed: 0 additions & 3 deletions
This file was deleted.

Sources/SURL/String+.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// String+.swift
3+
//
4+
//
5+
// Created by Zach Eriksen on 11/20/20.
6+
//
7+
8+
import Foundation
9+
10+
public extension String {
11+
var url: URL? {
12+
URL(string: self)
13+
}
14+
}

Sources/SURL/URL+.swift

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
}

Sources/SURL/URLRequest+.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// URLRequest+.swift
3+
//
4+
//
5+
// Created by Zach Eriksen on 11/20/20.
6+
//
7+
8+
import Foundation
9+
10+
public extension URLRequest {
11+
12+
// MARK: HTTPRequestMethod
13+
14+
enum HTTPRequestMethod: String {
15+
case GET
16+
case HEAD
17+
case POST
18+
case PUT
19+
case DELETE
20+
case CONNECT
21+
case OPTIONS
22+
case TRACE
23+
case PATCH
24+
}
25+
26+
27+
28+
var dataTask: URLSessionDataTask {
29+
URLSession.shared.dataTask(with: self)
30+
}
31+
32+
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
33+
var dataTaskPublisher: URLSession.DataTaskPublisher {
34+
URLSession.shared.dataTaskPublisher(for: self)
35+
}
36+
37+
38+
func dataTask(withHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
39+
URLSession.shared.dataTask(with: self, completionHandler: handler)
40+
}
41+
}

Tests/SURLTests/SURLTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import XCTest
22
@testable import SURL
33

44
final class SURLTests: XCTestCase {
5-
func testExample() {
6-
// This is an example of a functional test case.
7-
// Use XCTAssert and related functions to verify your tests produce the correct
8-
// results.
9-
XCTAssertEqual(SURL().text, "Hello, World!")
5+
func testStringURLInit() {
6+
XCTAssertEqual(
7+
URL(string: "https://something.com:8080")?.host,
8+
"https://something.com:8080".url?.host
9+
)
1010
}
11-
11+
1212
static var allTests = [
13-
("testExample", testExample),
13+
("testStringURLInit", testStringURLInit),
1414
]
1515
}

0 commit comments

Comments
 (0)