Skip to content

Commit cd9957b

Browse files
authored
tech(package): Introduce a foundation package (#10)
1 parent bf380fb commit cd9957b

30 files changed

+26
-27
lines changed

Package.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.4
1+
// swift-tools-version:5.5
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
@@ -7,28 +7,24 @@ let package = Package(
77
name: "SimpleHTTP",
88
platforms: [.iOS(.v13), .macOS(.v10_15)],
99
products: [
10-
// Products define the executables and libraries a package produces, and make them visible to other packages.
11-
.library(
12-
name: "SimpleHTTP",
13-
targets: ["SimpleHTTP"]),
10+
.library(name: "SimpleHTTPFoundation", targets: ["SimpleHTTPFoundation"]),
11+
.library(name: "SimpleHTTP", targets: ["SimpleHTTP"])
1412
],
1513
dependencies: [
1614
// Dependencies declare other packages that this package depends on.
1715
// .package(url: /* package url */, from: "1.0.0"),
1816
],
1917
targets: [
20-
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
21-
// Targets can depend on other targets in this package, and on products in packages this package depends on.
22-
.target(
23-
name: "SimpleHTTP",
24-
dependencies: []),
18+
.target(name: "SimpleHTTPFoundation", dependencies: []),
19+
.target(name: "SimpleHTTP", dependencies: ["SimpleHTTPFoundation"]),
20+
.testTarget(name: "SimpleHTTPFoundationTests", dependencies: ["SimpleHTTPFoundation"]),
2521
.testTarget(
2622
name: "SimpleHTTPTests",
2723
dependencies: ["SimpleHTTP"],
2824
resources: [
2925
.copy("Ressources/Images/swift.png"),
3026
.copy("Ressources/Images/swiftUI.png")
3127
]
32-
),
28+
)
3329
]
3430
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@_exported import SimpleHTTPFoundation

Sources/SimpleHTTP/Request/MultipartFormData.swift renamed to Sources/SimpleHTTP/MultipartForm/MultipartFormData.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import CoreServices
88
#endif
99

1010
struct Header: Hashable {
11-
1211
let name: HTTPHeader
1312
let value: String
14-
1513
}
1614

1715
enum EncodingCharacters {
@@ -49,7 +47,6 @@ enum Boundary {
4947
}
5048

5149
struct BodyPart {
52-
5350
let headers: [Header]
5451
let stream: InputStream
5552
let length: Int
@@ -61,16 +58,13 @@ struct BodyPart {
6158
self.stream = stream
6259
self.length = length
6360
}
64-
6561
}
6662

6763
/// Constructs `multipart/form-data` for uploads within an HTTP or HTTPS body.
6864
/// We encode the data directly in memory. It's very efficient, but can lead to memory issues if the dataset is too large (eg: a Video)
6965
///
7066
/// `Warning`: A Second approch to encode bigger dataset will be addes later
71-
7267
public struct MultipartFormData {
73-
7468
let boundary: String
7569
let fileManager: FileManager
7670
var bodyParts = [BodyPart]()

Sources/SimpleHTTP/Encoder/MultipartFormDataEncoder.swift renamed to Sources/SimpleHTTP/MultipartForm/MultipartFormDataEncoder.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Foundation
22

33
struct MultipartFormDataEncoder {
4-
54
let boundary: String
65
private var bodyParts: [BodyPart]
76

Sources/SimpleHTTP/Foundation/URLRequest/URLRequest+Multipart.swift renamed to Sources/SimpleHTTP/MultipartForm/URLRequest+Multipart.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import FoundationNetworking
77
extension URLRequest {
88
public mutating func multipartBody(_ body: MultipartFormData) throws {
99
var multipartEncode = MultipartFormDataEncoder(body: body)
10+
1011
httpBody = try multipartEncode.encode()
12+
1113
setHeaders([.contentType: HTTPContentType.multipart(boundary: body.boundary).value])
1214
}
1315
}
File renamed without changes.
File renamed without changes.

Sources/SimpleHTTP/Foundation/URL+Request.swift renamed to Sources/SimpleHTTP/Request/URL+Request.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension URL {
2323
}
2424

2525
extension URLComponents {
26-
enum Error: Swift.Error {
26+
public enum Error: Swift.Error {
2727
case invalid(path: String)
2828
case cannotGenerateURL(components: URLComponents)
2929
}

Sources/SimpleHTTP/Response/DataResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public struct URLDataResponse {
88
extension URLDataResponse {
99
public func validate(errorDecoder: DataErrorDecoder? = nil) throws {
1010
do {
11-
try response.validateStatusCode()
11+
try response.validate()
1212
}
1313
catch let error as HTTPError {
1414
guard let decoder = errorDecoder, !data.isEmpty else {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Foundation
2+
3+
#if canImport(FoundationNetworking)
4+
import FoundationNetworking
5+
#endif
6+
7+
extension URLSession {
8+
public func data(for urlRequest: URLRequest) async throws -> URLDataResponse {
9+
let (data, response) = try await data(for: urlRequest)
10+
11+
return URLDataResponse(data: data, response: response as! HTTPURLResponse)
12+
}
13+
}

0 commit comments

Comments
 (0)