Skip to content

Commit 41d761f

Browse files
committed
feat: Quality of life
1 parent b6cadd2 commit 41d761f

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

Sources/GoodNetworking/Models/Header.swift

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ public struct HTTPHeader: Equatable, Hashable, Sendable, HeaderConvertible {
1616
public let name: String
1717
public let value: String
1818

19-
/// Try to initialize ``HTTPHeader`` from string value. If string value cannot be parsed
20-
/// as a valid header, initialization fails with `nil`.
19+
/// Try to initialize ``HTTPHeader`` from string value. If string is `nil`
20+
/// or cannot be parsed as a valid header, initializer will fail.
21+
///
2122
/// - Parameter string: String to parse as a HTTP header
22-
public init?(from string: String) {
23-
guard !string.isEmpty else { return nil }
23+
public init?(from string: String?) {
24+
guard let string, !string.isEmpty else { return nil }
2425

2526
let split = string.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: true)
2627

@@ -34,6 +35,7 @@ public struct HTTPHeader: Equatable, Hashable, Sendable, HeaderConvertible {
3435

3536
/// Initialize ``HTTPHeader`` from string value. String must be a valid header,
3637
/// otherwise the initialization will trip an assertion.
38+
///
3739
/// - Parameter string: String representation of a HTTP header
3840
public init(_ string: String) {
3941
assert(!string.isEmpty)
@@ -49,6 +51,7 @@ public struct HTTPHeader: Equatable, Hashable, Sendable, HeaderConvertible {
4951
}
5052

5153
/// Initialize ``HTTPHeader`` as a name-value pair.
54+
///
5255
/// - Parameters:
5356
/// - name: Name of the header (part before colon)
5457
/// - value: Value of the header (part after colon)
@@ -57,7 +60,7 @@ public struct HTTPHeader: Equatable, Hashable, Sendable, HeaderConvertible {
5760
self.value = value
5861
}
5962

60-
public func resolveHeader() -> HTTPHeader {
63+
public func resolveHeader() -> HTTPHeader? {
6164
self
6265
}
6366

@@ -91,10 +94,22 @@ public struct HTTPHeaders: Sendable {
9194

9295
/// Create collection of ``HTTPHeader``-s from key-value dictionary mapped as
9396
/// name-value header pairs.
97+
///
9498
/// - Parameter headers: Dictionary, where keys are header names
9599
public init(_ headers: [String: String]) {
96100
self.headers = headers.map(HTTPHeader.init).reduce(into: [], { $0.append($1) })
97101
}
102+
103+
/// Creates HTTPHeaders struct from array(s) of ``HeaderConvertible``.
104+
///
105+
/// Individual elements will not be resolved to ``HTTPHeader`` instances
106+
/// during initialization.
107+
///
108+
/// - Parameter elements: Single or multiple arrays of elements to be treated as headers
109+
public init(_ elements: [any HeaderConvertible]...) {
110+
self.headers = []
111+
elements.forEach { headers.append(contentsOf: $0) }
112+
}
98113

99114
/// Get the value of a header with name `name`
100115
public subscript(_ name: String) -> String? {
@@ -116,15 +131,17 @@ public struct HTTPHeaders: Sendable {
116131

117132
/// Appends a new entity to the header collection. Does not resolve
118133
/// the header name or value.
134+
///
119135
/// - Parameter header: New header
120136
public mutating func add(header: any HeaderConvertible) {
121137
headers.append(header)
122138
}
123139

124140
/// Resolve all headers to their final values.
141+
///
125142
/// - Returns: Array of resolved headers as ``HTTPHeader``-s
126143
public func resolve() -> [HTTPHeader] {
127-
headers.map { $0.resolveHeader() }
144+
headers.compactMap { $0.resolveHeader() }
128145
}
129146

130147
}
@@ -206,14 +223,16 @@ public protocol HeaderConvertible: Sendable {
206223
/// This function will be called every time for each header
207224
/// before a network request is sent.
208225
///
209-
/// - Returns: Valid HTTP header (name-value pair)
210-
func resolveHeader() -> HTTPHeader
226+
/// If header cannot be resolved, this function can return `nil`.
227+
///
228+
/// - Returns: Valid HTTP header (name-value pair) or nil
229+
func resolveHeader() -> HTTPHeader?
211230

212231
}
213232

214233
extension String: HeaderConvertible {
215234

216-
public func resolveHeader() -> HTTPHeader {
235+
public func resolveHeader() -> HTTPHeader? {
217236
HTTPHeader(self)
218237
}
219238

Sources/GoodNetworking/Models/URLConvertible.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111

1212
/// `URLConvertible` defines a function that asynchronously resolves the base URL used for network requests.
1313
/// Classes or structs that conform to this protocol can implement their own logic for determining and returning the base URL.
14-
public protocol URLConvertible {
14+
public protocol URLConvertible: Sendable {
1515

1616
/// Resolves and returns the base URL for network requests asynchronously.
1717
///

0 commit comments

Comments
 (0)