Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 06fad9b

Browse files
committed
Restored HTTP Client and URL
1 parent f274032 commit 06fad9b

File tree

13 files changed

+1085
-527
lines changed

13 files changed

+1085
-527
lines changed

Sources/SwiftFoundation/HTTP.swift

100644100755
Lines changed: 4 additions & 519 deletions
Large diffs are not rendered by default.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//
2+
// HTTPClient.swift
3+
// SwiftFoundation
4+
//
5+
// Created by Alsey Coleman Miller on 9/02/15.
6+
// Copyright © 2015 PureSwift. All rights reserved.
7+
//
8+
9+
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && !XcodeLinux
10+
11+
import Foundation
12+
13+
// Dot notation syntax for class
14+
public extension HTTP {
15+
16+
/// Loads HTTP requests
17+
public final class Client {
18+
19+
public init(session: URLSession = URLSession.shared) {
20+
21+
self.session = session
22+
}
23+
24+
/// The backing ```NSURLSession```.
25+
public let session: URLSession
26+
27+
public func send(request: HTTP.Request) throws -> HTTP.Response {
28+
29+
var dataTask: URLSessionDataTask?
30+
31+
return try send(request: request, dataTask: &dataTask)
32+
}
33+
34+
public func send(request: HTTP.Request, dataTask: inout URLSessionDataTask?) throws -> HTTP.Response {
35+
36+
// build request...
37+
38+
guard let urlRequest = Foundation.URLRequest(request: request)
39+
else { throw Error.BadRequest }
40+
41+
// execute request
42+
43+
let semaphore = DispatchSemaphore(value: 0);
44+
45+
var error: Swift.Error?
46+
47+
var responseData: Data?
48+
49+
var urlResponse: HTTPURLResponse?
50+
51+
dataTask = self.session.dataTask(with: urlRequest) { (data: Foundation.Data?, response: Foundation.URLResponse?, responseError: Swift.Error?) -> () in
52+
53+
responseData = data
54+
55+
urlResponse = response as? Foundation.HTTPURLResponse
56+
57+
error = responseError
58+
59+
semaphore.signal()
60+
}
61+
62+
dataTask!.resume()
63+
64+
// wait for task to finish
65+
66+
let _ = semaphore.wait(timeout: DispatchTime.distantFuture);
67+
68+
guard urlResponse != nil else { throw error! }
69+
70+
var response = HTTP.Response()
71+
72+
response.statusCode = urlResponse!.statusCode
73+
74+
if let data = responseData, data.count > 0 {
75+
76+
response.body = data
77+
}
78+
79+
response.headers = urlResponse!.allHeaderFields as! [String: String]
80+
81+
return response
82+
}
83+
}
84+
}
85+
86+
87+
public extension HTTP.Client {
88+
89+
public enum Error: Swift.Error {
90+
91+
/// The provided request was malformed.
92+
case BadRequest
93+
}
94+
}
95+
96+
public extension Foundation.URLRequest {
97+
98+
init?(request: HTTP.Request) {
99+
100+
guard let url = NSURL(string: request.URL) else { return nil }
101+
102+
guard request.version == HTTP.Version(1, 1) else { return nil }
103+
104+
self.init(url: url as Foundation.URL, timeoutInterval: request.timeoutInterval)
105+
106+
if let data = request.body {
107+
108+
self.httpBody = data
109+
}
110+
111+
self.allHTTPHeaderFields = request.headers
112+
113+
self.httpMethod = request.method.rawValue
114+
}
115+
}
116+
117+
#endif
118+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// HTTPMethod.swift
3+
// SwiftFoundation
4+
//
5+
// Created by Alsey Coleman Miller on 6/29/15.
6+
// Copyright © 2015 PureSwift. All rights reserved.
7+
//
8+
9+
public extension HTTP {
10+
11+
/// HTTP Method.
12+
public enum Method: String {
13+
14+
case GET
15+
case PUT
16+
case DELETE
17+
case POST
18+
case OPTIONS
19+
case HEAD
20+
case TRACE
21+
case CONNECT
22+
case PATCH
23+
24+
init() { self = .GET }
25+
}
26+
}
27+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// HTTPRequest.swift
3+
// SwiftFoundation
4+
//
5+
// Created by Alsey Coleman Miller on 6/29/15.
6+
// Copyright © 2015 PureSwift. All rights reserved.
7+
//
8+
9+
public extension HTTP {
10+
11+
/// HTTP request.
12+
public struct Request: URLRequest {
13+
14+
public var URL: String
15+
16+
public var timeoutInterval: TimeInterval = 30
17+
18+
public var body: Data?
19+
20+
public var headers = [String: String]()
21+
22+
public var method: HTTP.Method = .GET
23+
24+
public var version: HTTP.Version = HTTP.Version()
25+
26+
public init(URL: String) {
27+
28+
self.URL = URL
29+
}
30+
}
31+
}
32+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// HTTPResponse.swift
3+
// SwiftFoundation
4+
//
5+
// Created by Alsey Coleman Miller on 6/29/15.
6+
// Copyright © 2015 PureSwift. All rights reserved.
7+
//
8+
9+
public extension HTTP {
10+
11+
/// HTTP URL response.
12+
public struct Response: URLResponse {
13+
14+
/// Returns a dictionary containing all the HTTP header fields.
15+
public var headers = [String: String]()
16+
17+
/// Returns the HTTP status code for the response.
18+
public var statusCode: Int = HTTP.StatusCode.OK.rawValue
19+
20+
/// The HTTP response body.
21+
public var body = Data()
22+
23+
public init() { }
24+
}
25+
}
26+

0 commit comments

Comments
 (0)