-
Notifications
You must be signed in to change notification settings - Fork 32
/
HTTPResourceEndpoint.swift
66 lines (47 loc) · 1.91 KB
/
HTTPResourceEndpoint.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Foundation
/// A type representing an HTTP resource's endpoint, to generate its request.
///
/// Especially useful when conformed to by an enum, allowing a type safe modelling of an API's endpoints.
public protocol HTTPResourceEndpoint {
/// The HTTP method.
var method: HTTP.Method { get }
/// The base URL.
var baseURL: URL { get }
/// The URL's path subcomponent.
var path: String? { get }
/// The URL's query string items.
var queryItems: [URLQueryItem]? { get }
/// The HTTP header fields.
var headers: HTTP.Headers? { get }
// Makes the HTTP message body data.
func makeBody() throws -> Data?
// Makes the URL request.
func makeRequest() throws -> URLRequest
}
public extension HTTPResourceEndpoint {
var path: String? { nil }
var queryItems: [URLQueryItem]? { nil }
var headers: HTTP.Headers? { nil }
func makeBody() throws -> Data? { nil }
func makeRequest() throws -> URLRequest {
guard var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: false) else {
assertionFailure("😱 Failed to create components from URL: \(baseURL) on \(type(of: self))!")
return URLRequest(url: baseURL)
}
if let queryItems = queryItems {
components.queryItems = (components.queryItems ?? []) + queryItems
}
if let path = path {
components.path = components.path.appending(path).replacingOccurrences(of: "//", with: "/")
}
guard let url = components.url else {
assertionFailure("😱 Failed to extract URL from components: \(components) on \(type(of: self))!")
return URLRequest(url: baseURL)
}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = method.rawValue
urlRequest.allHTTPHeaderFields = headers
urlRequest.httpBody = try makeBody()
return urlRequest
}
}