Skip to content

Commit 2488fc2

Browse files
committed
fix: Code style and documentation
1 parent df0275d commit 2488fc2

4 files changed

Lines changed: 66 additions & 38 deletions

File tree

Sources/GoodNetworking/Models/Endpoint.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ public protocol Endpoint {
2828
@available(*, deprecated, message: "Encoding will be automatically determined by the kind of `parameters` in the future.")
2929
var encoding: ParameterEncoding { get }
3030

31-
/// Creates a URL by combining `path` with `baseUrl`.
31+
/// Creates a URL by resolving `path` over `baseUrl`.
32+
///
3233
/// This function is a customization point for modifying the URL by current runtime,
3334
/// for example for API versioning or platform separation.
35+
///
36+
/// Note that this function will be only called if the ``path`` resolved
37+
/// is a relative URL. If ``path`` specifies an absolute URL, it will be
38+
/// used instead, without any modifications.
39+
///
3440
/// - Parameter baseUrl: Base URL for the request to combine with.
35-
/// - Throws: If creating a concrete URL fails.
36-
/// - Returns: URL for the request.
41+
/// - Returns: URL for the request or `nil` if such URL cannot be constructed.
3742
@NetworkActor func url(on baseUrl: URLConvertible) async -> URL?
3843

3944
}
4045

41-
@available(*, deprecated, message: "Default values for deprecated properties")
4246
public extension Endpoint {
43-
44-
var encoding: ParameterEncoding { AutomaticEncoding.default }
45-
47+
4648
@NetworkActor func url(on baseUrl: URLConvertible) async -> URL? {
4749
let baseUrl = await baseUrl.resolveUrl()
4850
let path = await path.resolveUrl()
@@ -53,6 +55,13 @@ public extension Endpoint {
5355

5456
}
5557

58+
@available(*, deprecated, message: "Default values for deprecated properties")
59+
public extension Endpoint {
60+
61+
var encoding: ParameterEncoding { AutomaticEncoding.default }
62+
63+
}
64+
5665
// MARK: - Parameters
5766

5867
/// Enum that represents the data to be sent with the request,

Sources/GoodNetworking/Models/EndpointFactory.swift renamed to Sources/GoodNetworking/Models/EndpointBuilder.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// EndpointFactory.swift
2+
// EndpointBuilder.swift
33
// GoodNetworking
44
//
55
// Created by Filip Šašala on 06/08/2025.
@@ -10,7 +10,7 @@ import Foundation
1010
/// Modified implementation of factory pattern to build
1111
/// endpoint as a series of function calls instead of conforming
1212
/// to a protocol.
13-
public final class EndpointFactory: Endpoint {
13+
public final class EndpointBuilder: Endpoint {
1414

1515
public var path: URLConvertible
1616
public var method: HTTPMethod = .get
@@ -29,7 +29,7 @@ public final class EndpointFactory: Endpoint {
2929

3030
}
3131

32-
public extension EndpointFactory {
32+
public extension EndpointBuilder {
3333

3434
func method(_ method: HTTPMethod) -> Self {
3535
self.method = method
@@ -82,14 +82,17 @@ public extension EndpointFactory {
8282

8383
}
8484

85-
private extension EndpointFactory {
85+
private extension EndpointBuilder {
8686

8787
func assertBothQueryAndBodyUsage() {
8888
assert(self.parameters == nil, "Support for query and body parameters at the same time is currently not available.")
8989
}
9090

9191
}
9292

93-
public func at(_ path: URLConvertible) -> EndpointFactory {
94-
EndpointFactory(at: path)
93+
public func at(_ path: URLConvertible) -> EndpointBuilder {
94+
EndpointBuilder(at: path)
9595
}
96+
97+
@available(*, deprecated, renamed: "EndpointBuilder")
98+
public typealias EndpointFactory = EndpointBuilder

Sources/GoodNetworking/Models/URLConvertible.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,33 @@ extension URL {
6363
self.init(string: string)
6464
}
6565

66+
/// Checks if URL contains a non-empty scheme.
67+
///
68+
/// Returns `true` if ``scheme`` is not `nil` and not empty, eg. `https://`
69+
var hasScheme: Bool {
70+
if let scheme, !scheme.isEmpty {
71+
return true
72+
} else {
73+
return false
74+
}
75+
}
76+
77+
/// Checks if URL contains a non-empty host.
78+
///
79+
/// Returns `true` if ``host`` is not `nil` and not empty, eg. `goodrequest.com`
80+
var hasHost: Bool {
81+
if let host, !host.isEmpty {
82+
return true
83+
} else {
84+
return false
85+
}
86+
}
87+
88+
/// Checks if URL can be considered an absolute URL.
89+
///
90+
/// Returns `true` if the URL is a file URL or has both host and scheme.
91+
var isAbsolute: Bool {
92+
return isFileURL || hasScheme && hasHost
93+
}
94+
6695
}

Sources/GoodNetworking/Session/NetworkSession.swift

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -249,37 +249,24 @@ extension NetworkSession {
249249
@discardableResult
250250
public func request(endpoint: Endpoint) async throws(NetworkError) -> Data {
251251
let endpointPath = await endpoint.path.resolveUrl()
252-
let baseUrl = await baseUrl.resolveUrl()
253252
let url: URL
254-
255-
// Check if endpointPath is absolute
256-
let endpointHasScheme: Bool = if let endpointPath,
257-
let endpointPathScheme = endpointPath.scheme,
258-
!endpointPathScheme.isEmpty { true } else { false }
259-
260-
let endpointHasHost: Bool = if let endpointPath,
261-
let endpointPathHost = endpointPath.host,
262-
!endpointPathHost.isEmpty { true } else { false }
263-
264-
// Check if baseUrl is resolvable
265-
let baseUrlHasScheme: Bool = if let baseUrl,
266-
let baseUrlScheme = baseUrl.scheme,
267-
!baseUrlScheme.isEmpty { true } else { false }
268-
269-
let baseUrlHasHost: Bool = if let baseUrl,
270-
let baseUrlHost = baseUrl.host,
271-
!baseUrlHost.isEmpty { true } else { false }
272-
273-
let endpointIsAbsolutePath = endpointHasScheme && endpointHasHost
274-
if endpointIsAbsolutePath, let endpointPath {
253+
254+
// If endpoint already contains an absolute path, do not concatenate
255+
// with baseURL and use that instead
256+
if let endpointPath, endpointPath.isAbsolute {
275257
url = endpointPath
276258
} else {
259+
// If endpoint has only relative path, resolve it over baseURL
260+
let baseUrl = await baseUrl.resolveUrl()
277261
let endpointResolvedUrl = await endpoint.url(on: baseUrl)
278-
if let endpointResolvedUrl {
279-
url = endpointResolvedUrl
280-
} else {
262+
263+
// If neither endpoint nor baseURL are specified, URL cannot be resolved
264+
guard let endpointResolvedUrl else {
281265
throw URLError(.badURL).asNetworkError()
282266
}
267+
268+
// URL is resolved
269+
url = endpointResolvedUrl
283270
}
284271

285272
// url + method

0 commit comments

Comments
 (0)