Skip to content

Commit f3d0d1c

Browse files
authored
feat(request): [#11] Rename Endpoint -> Path (#15)
1 parent ba61a94 commit f3d0d1c

File tree

11 files changed

+94
-86
lines changed

11 files changed

+94
-86
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension Request {
3838
}
3939
```
4040

41-
This defines a `Request.login(_:)` method which create a request targeting "login" endpoint by sending a `UserBody` and expecting a `UserResponse` as response.
41+
This defines a `Request.login(_:)` method which create a request targeting "login" path by sending a `UserBody` and expecting a `UserResponse` as response.
4242

4343
### Sending a request
4444

@@ -64,7 +64,7 @@ try await session.response(for: .login(UserBody(username: "pjechris", password:
6464

6565
A few words about Session:
6666

67-
- `baseURL` will be prepended to all call endpoints
67+
- `baseURL` will be prepended to all call paths
6868
- You can skip encoder and decoder if you use JSON
6969
- You can provide a custom `URLSession` instance if ever needed
7070

@@ -138,13 +138,13 @@ extension Request {
138138
}
139139
```
140140

141-
## Constant endpoints
141+
## Constant paths
142142

143-
You can declare constant endpoints if needed (refer to Endpoint documentation to see more):
143+
You can declare constant paths if needed (refer to Path documentation to see more):
144144

145145
```swift
146-
extension Endpoint {
147-
static let login: Endpoint = "login"
146+
extension Path {
147+
static let login: Path = "login"
148148
}
149149

150150
extension Request {

Sources/SimpleHTTP/Request/Endpoint.swift

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Foundation
2+
3+
/// A path represents a path a request can query to
4+
///
5+
/// You can create paths using plain String, for instance:
6+
/// ```swift
7+
/// extension Path {
8+
/// static let user = "v1/users"
9+
/// }
10+
///
11+
/// If you want to regroup a set of paths you can use your own "namespace" and add a forward declaration in `Path`.
12+
/// Adding a declaration provide autocompletion when using it in `Request`.
13+
/// ```swift
14+
/// enum MyPaths {
15+
/// static let user: Path = "v1/users"
16+
/// }
17+
///
18+
/// extension Path {
19+
/// static let myPaths = MyPaths.self
20+
/// }
21+
///
22+
/// let user: Path = .myPaths.user
23+
/// ```
24+
public struct Path: Equatable, ExpressibleByStringLiteral, ExpressibleByStringInterpolation {
25+
/// relative path
26+
public let value: String
27+
28+
init(value: String) {
29+
self.value = value
30+
}
31+
32+
public init(stringLiteral value: StringLiteralType) {
33+
self.init(value: value)
34+
}
35+
36+
public init(stringInterpolation: DefaultStringInterpolation) {
37+
self.init(value: stringInterpolation.description)
38+
}
39+
40+
public static func ==(lhs: Path, rhs: String) -> Bool {
41+
lhs.value == rhs
42+
}
43+
}
44+

Sources/SimpleHTTP/Request/Request.swift

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,76 @@ public enum Body {
1717
///
1818
/// Highly inspired by https://swiftwithmajid.com/2021/02/10/building-type-safe-networking-in-swift/
1919
public struct Request<Output> {
20-
/// request relative endpoint
21-
public let endpoint: Endpoint
20+
/// request relative path
21+
public let path: Path
2222
public let method: Method
2323
public let body: Body?
2424
public let query: [String: QueryParam]
25-
public private(set) var cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy
26-
public private(set) var headers: HTTPHeaderFields = [:]
25+
public var cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy
26+
public var headers: HTTPHeaderFields = [:]
2727

2828
/// Creates a request suitable for a HTTP GET
29-
public static func get(_ endpoint: Endpoint, query: [String: QueryParam] = [:]) -> Self {
30-
self.init(endpoint: endpoint, method: .get, query: query, body: nil)
29+
public static func get(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
30+
self.init(path: path, method: .get, query: query, body: nil)
3131
}
3232

3333
/// Creates a request suitable for a HTTP POST with a `Encodable` body
34-
public static func post(_ endpoint: Endpoint, body: Encodable?, query: [String: QueryParam] = [:])
34+
public static func post(_ path: Path, body: Encodable?, query: [String: QueryParam] = [:])
3535
-> Self {
36-
self.init(endpoint: endpoint, method: .post, query: query, body: body.map(Body.encodable))
36+
self.init(path: path, method: .post, query: query, body: body.map(Body.encodable))
3737
}
3838

3939
/// Creates a request suitable for a HTTP POST with a `MultipartFormData` body
4040
@_disfavoredOverload
41-
public static func post(_ endpoint: Endpoint, body: MultipartFormData?, query: [String: QueryParam] = [:])
41+
public static func post(_ path: Path, body: MultipartFormData?, query: [String: QueryParam] = [:])
4242
-> Self {
43-
self.init(endpoint: endpoint, method: .post, query: query, body: body.map(Body.multipart))
43+
self.init(path: path, method: .post, query: query, body: body.map(Body.multipart))
4444
}
4545

4646
/// Creates a request suitable for a HTTP PUT with a `Encodable` body
47-
public static func put(_ endpoint: Endpoint, body: Encodable, query: [String: QueryParam] = [:])
47+
public static func put(_ path: Path, body: Encodable, query: [String: QueryParam] = [:])
4848
-> Self {
49-
self.init(endpoint: endpoint, method: .put, query: query, body: .encodable(body))
49+
self.init(path: path, method: .put, query: query, body: .encodable(body))
5050
}
5151

5252
/// Creates a request suitable for a HTTP PUT with a `MultipartFormData` body
53-
public static func put(_ endpoint: Endpoint, body: MultipartFormData, query: [String: QueryParam] = [:])
53+
public static func put(_ path: Path, body: MultipartFormData, query: [String: QueryParam] = [:])
5454
-> Self {
55-
self.init(endpoint: endpoint, method: .put, query: query, body: .multipart(body))
55+
self.init(path: path, method: .put, query: query, body: .multipart(body))
56+
}
57+
58+
/// Create a HTTP PUT request with no body
59+
public static func put(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
60+
self.init(path: path, method: .put, query: query, body: nil)
5661
}
5762

5863
/// Creates a request suitable for a HTTP PATCH with a `Encodable` body
59-
public static func patch(_ endpoint: Endpoint, body: Encodable, query: [String: QueryParam] = [:])
64+
public static func patch(_ path: Path, body: Encodable, query: [String: QueryParam] = [:])
6065
-> Self {
61-
self.init(endpoint: endpoint, method: .patch, query: query, body: .encodable(body))
66+
self.init(path: path, method: .patch, query: query, body: .encodable(body))
6267
}
6368

6469
/// Creates a request suitable for a HTTP PATCH with a `MultipartFormData` body
65-
public static func patch(_ endpoint: Endpoint, body: MultipartFormData, query: [String: QueryParam] = [:])
70+
public static func patch(_ path: Path, body: MultipartFormData, query: [String: QueryParam] = [:])
6671
-> Self {
67-
self.init(endpoint: endpoint, method: .patch, query: query, body: .multipart(body))
72+
self.init(path: path, method: .patch, query: query, body: .multipart(body))
6873
}
6974

7075
/// Creates a request suitable for a HTTP DELETE
71-
/// Default implementation does not allow for sending a body. If you need such a case extend Request with your
72-
/// own init method
73-
public static func delete(_ endpoint: Endpoint, query: [String: QueryParam] = [:]) -> Self {
74-
self.init(endpoint: endpoint, method: .delete, query: query, body: nil)
76+
public static func delete(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
77+
self.init(path: path, method: .delete, query: query, body: nil)
78+
}
79+
80+
/// Creates a DELETE request with a Encodable body
81+
public static func delete(_ path: Path, body: Encodable, query: [String: QueryParam] = [:]) -> Self {
82+
self.init(path: path, method: .delete, query: query, body: nil)
7583
}
7684

7785
/// Creates a Request.
7886
///
7987
/// Use this init only if default provided static initializers (`.get`, `.post`, `.put`, `patch`, `.delete`) do not suit your needs.
80-
public init(endpoint: Endpoint, method: Method, query: [String: QueryParam], body: Body?) {
81-
self.endpoint = endpoint
88+
public init(path: Path, method: Method, query: [String: QueryParam], body: Body?) {
89+
self.path = path
8290
self.method = method
8391
self.body = body
8492
self.query = query

Sources/SimpleHTTP/Request/URL+Request.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import FoundationNetworking
66

77
extension URL {
88
init<Output>(from request: Request<Output>) throws {
9-
guard var components = URLComponents(string: request.endpoint.path) else {
10-
throw URLComponents.Error.invalid(endpoint: request.endpoint)
9+
guard var components = URLComponents(string: request.path.value) else {
10+
throw URLComponents.Error.invalid(path: request.path)
1111
}
1212

1313
let queryItems = (components.queryItems ?? []) + request.query.queryItems
@@ -24,7 +24,7 @@ extension URL {
2424

2525
extension URLComponents {
2626
public enum Error: Swift.Error {
27-
case invalid(endpoint: Endpoint)
27+
case invalid(path: Path)
2828
case cannotGenerateURL(components: URLComponents)
2929
}
3030
}

Sources/SimpleHTTPFoundation/Foundation/URLRequest/URLRequest+URL.swift

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

33
extension URLRequest {
4-
/// Return a new URLRequest whose endpoint is relative to `baseURL`
4+
/// Return a new URLRequest whose path is relative to `baseURL`
55
public func relativeTo(_ baseURL: URL) -> URLRequest {
66
var urlRequest = self
77
var components = URLComponents(string: baseURL.appendingPathComponent(url?.path ?? "").absoluteString)

Tests/SimpleHTTPFoundationTests/Foundation/URLRequest/URLRequestTests+URL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22
import XCTest
3-
@testable import SimpleHTTP
3+
@testable import SimpleHTTPFoundation
44

55
class URLRequestURLTests: XCTestCase {
66
func test_relativeTo_requestURLHasBaseURL() {

Tests/SimpleHTTPFoundationTests/Foundation/URLRequest/URLRequestsTests+Encode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
import SimpleHTTP
2+
import SimpleHTTPFoundation
33

44
#if canImport(FoundationNetworking)
55
import FoundationNetworking

Tests/SimpleHTTPFoundationTests/Foundation/URLResponseValidateTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
import SimpleHTTP
2+
import SimpleHTTPFoundation
33

44
#if canImport(FoundationNetworking)
55
import FoundationNetworking

Tests/SimpleHTTPTests/Request/RequestTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import XCTest
22
@testable import SimpleHTTP
33

4-
extension Endpoint {
5-
fileprivate static let test: Endpoint = "test"
4+
extension Path {
5+
fileprivate static let test: Path = "test"
66
}
77

88
class RequestTests: XCTestCase {
99
let baseURL = URL(string: "https://google.fr")!
1010

1111
func test_init_withPathAsString() {
12-
XCTAssertEqual(Request<Void>.get("hello_world").endpoint, "hello_world")
12+
XCTAssertEqual(Request<Void>.get("hello_world").path, "hello_world")
1313
}
1414

1515
func test_toURLRequest_setHttpMethod() throws {

0 commit comments

Comments
 (0)