Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tech(package): Introduce a foundation package #10

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
}
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)
}
}
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