Skip to content

Commit

Permalink
tech(package): Introduce a foundation package (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjechris committed Aug 19, 2022
1 parent bf380fb commit cd9957b
Show file tree
Hide file tree
Showing 30 changed files with 26 additions and 27 deletions.
18 changes: 7 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.4
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -7,28 +7,24 @@ let package = Package(
name: "SimpleHTTP",
platforms: [.iOS(.v13), .macOS(.v10_15)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "SimpleHTTP",
targets: ["SimpleHTTP"]),
.library(name: "SimpleHTTPFoundation", targets: ["SimpleHTTPFoundation"]),
.library(name: "SimpleHTTP", targets: ["SimpleHTTP"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "SimpleHTTP",
dependencies: []),
.target(name: "SimpleHTTPFoundation", dependencies: []),
.target(name: "SimpleHTTP", dependencies: ["SimpleHTTPFoundation"]),
.testTarget(name: "SimpleHTTPFoundationTests", dependencies: ["SimpleHTTPFoundation"]),
.testTarget(
name: "SimpleHTTPTests",
dependencies: ["SimpleHTTP"],
resources: [
.copy("Ressources/Images/swift.png"),
.copy("Ressources/Images/swiftUI.png")
]
),
)
]
)
1 change: 1 addition & 0 deletions Sources/SimpleHTTP/Foundation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@_exported import SimpleHTTPFoundation
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import CoreServices
#endif

struct Header: Hashable {

let name: HTTPHeader
let value: String

}

enum EncodingCharacters {
Expand Down Expand Up @@ -49,7 +47,6 @@ enum Boundary {
}

struct BodyPart {

let headers: [Header]
let stream: InputStream
let length: Int
Expand All @@ -61,16 +58,13 @@ struct BodyPart {
self.stream = stream
self.length = length
}

}

/// Constructs `multipart/form-data` for uploads within an HTTP or HTTPS body.
/// 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)
///
/// `Warning`: A Second approch to encode bigger dataset will be addes later

public struct MultipartFormData {

let boundary: String
let fileManager: FileManager
var bodyParts = [BodyPart]()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

struct MultipartFormDataEncoder {

let boundary: String
private var bodyParts: [BodyPart]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import FoundationNetworking
extension URLRequest {
public mutating func multipartBody(_ body: MultipartFormData) throws {
var multipartEncode = MultipartFormDataEncoder(body: body)

httpBody = try multipartEncode.encode()

setHeaders([.contentType: HTTPContentType.multipart(boundary: body.boundary).value])
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension URL {
}

extension URLComponents {
enum Error: Swift.Error {
public enum Error: Swift.Error {
case invalid(path: String)
case cannotGenerateURL(components: URLComponents)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SimpleHTTP/Response/DataResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct URLDataResponse {
extension URLDataResponse {
public func validate(errorDecoder: DataErrorDecoder? = nil) throws {
do {
try response.validateStatusCode()
try response.validate()
}
catch let error as HTTPError {
guard let decoder = errorDecoder, !data.isEmpty else {
Expand Down
13 changes: 13 additions & 0 deletions Sources/SimpleHTTP/Response/URLSession+DataResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

extension URLSession {
public func data(for urlRequest: URLRequest) async throws -> URLDataResponse {
let (data, response) = try await data(for: urlRequest)

return URLDataResponse(data: data, response: response as! HTTPURLResponse)
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,4 @@ extension URLSession {
.resume()
}
}

public func data(for urlRequest: URLRequest) async throws -> URLDataResponse {
let (data, response) = try await data(for: urlRequest)

return URLDataResponse(data: data, response: response as! HTTPURLResponse)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

/// A struct representing a http header content type value
public struct HTTPContentType: Hashable, ExpressibleByStringLiteral {
let value: String
public let value: String

public init(value: String) {
self.value = value
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit cd9957b

Please sign in to comment.