/
APIRequest.swift
160 lines (135 loc) · 6.03 KB
/
APIRequest.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// APIRequest.swift
// APIClient
//
// Created by Jonathan Bailey on 9/6/18.
// Copyright © 2018 Jonathan Bailey. All rights reserved.
//
import Foundation
/// The protocol to which all API request types adhere.
public protocol APIRequest {
/// Placeholder `Decodable` representing what the expected response type to this `APIRequest` should be.
associatedtype ResponseType: Decodable
/// The HTTP request method to use for this request.
var method: HTTPMethod { get }
/// Optional data to include in the request body, usually for HTTP POST/PUT requests.
var body: Data? { get set }
/// Optional headers to include in your request.
var headers: [String : String] { get set }
/// The endpoint resource of this API request e.g. `scheme://host<RESOURCE>` where <RESOURCE> could be `/info`, etc. NOTE: The preceeding forward slash is required.
var resource: String { get set }
/// The URL query parameters of this request, if any.
var urlQueryParameters: [URLQueryItem] { get }
/// The URL path parameters of this request, as name:value pairs, if any.
var urlPathParameters: [String : String] { get set }
}
/// A generic HTTP GET request.
open class GetRequest<T: Decodable> : APIRequest {
public typealias ResponseType = T
public var method: HTTPMethod { return .get }
public var resource: String
public var body: Data?
public var headers: [String : String]
public var urlQueryParameters: [URLQueryItem]
public var urlPathParameters: [String : String]
public init(resource: String, body: Data? = nil, headers: [String : String] = [:], urlQueryParameters: [URLQueryItem] = [], urlPathParameters: [String : String] = [:]) {
self.resource = resource
self.body = body
self.headers = headers
self.urlQueryParameters = urlQueryParameters
self.urlPathParameters = urlPathParameters
if !urlPathParameters.isEmpty {
for urlPathParameter in urlPathParameters {
self.resource = self.resource.replacingOccurrences(of: urlPathParameter.key, with: urlPathParameter.value)
}
}
}
}
/// A generic HTTP POST request.
open class PostRequest<T: Decodable> : APIRequest {
public typealias ResponseType = T
public var method: HTTPMethod { return .post }
public var resource: String
public var body: Data?
public var headers: [String : String]
public var urlQueryParameters: [URLQueryItem]
public var urlPathParameters: [String : String]
public init(resource: String, body: Data? = nil, headers: [String : String] = [:], urlQueryParameters: [URLQueryItem] = [], urlPathParameters: [String : String] = [:]) {
self.resource = resource
self.body = body
self.headers = headers
self.urlQueryParameters = urlQueryParameters
self.urlPathParameters = urlPathParameters
if !urlPathParameters.isEmpty {
for urlPathParameter in urlPathParameters {
self.resource = self.resource.replacingOccurrences(of: urlPathParameter.key, with: urlPathParameter.value)
}
}
}
}
/// A generic HTTP PUT request.
open class PutRequest<T: Decodable> : APIRequest {
public typealias ResponseType = T
public var method: HTTPMethod { return .put }
public var resource: String
public var body: Data?
public var headers: [String : String]
public var urlQueryParameters: [URLQueryItem]
public var urlPathParameters: [String : String]
public init(resource: String, body: Data? = nil, headers: [String : String] = [:], urlQueryParameters: [URLQueryItem] = [], urlPathParameters: [String : String] = [:]) {
self.resource = resource
self.body = body
self.headers = headers
self.urlQueryParameters = urlQueryParameters
self.urlPathParameters = urlPathParameters
if !urlPathParameters.isEmpty {
for urlPathParameter in urlPathParameters {
self.resource = self.resource.replacingOccurrences(of: urlPathParameter.key, with: urlPathParameter.value)
}
}
}
}
/// A generic HTTP PATCH request.
open class PatchRequest<T: Decodable> : APIRequest {
public typealias ResponseType = T
public var method: HTTPMethod { return .patch }
public var resource: String
public var body: Data?
public var headers: [String : String]
public var urlQueryParameters: [URLQueryItem]
public var urlPathParameters: [String : String]
public init(resource: String, body: Data? = nil, headers: [String : String] = [:], urlQueryParameters: [URLQueryItem] = [], urlPathParameters: [String : String] = [:]) {
self.resource = resource
self.body = body
self.headers = headers
self.urlQueryParameters = urlQueryParameters
self.urlPathParameters = urlPathParameters
if !urlPathParameters.isEmpty {
for urlPathParameter in urlPathParameters {
self.resource = self.resource.replacingOccurrences(of: urlPathParameter.key, with: urlPathParameter.value)
}
}
}
}
/// A generic HTTP DELETE request.
open class DeleteRequest<T: Decodable> : APIRequest {
public typealias ResponseType = T
public var method: HTTPMethod { return .delete }
public var resource: String
public var body: Data?
public var headers: [String : String]
public var urlQueryParameters: [URLQueryItem]
public var urlPathParameters: [String : String]
public init(resource: String, body: Data? = nil, headers: [String : String] = [:], urlQueryParameters: [URLQueryItem] = [], urlPathParameters: [String : String] = [:]) {
self.resource = resource
self.body = body
self.headers = headers
self.urlQueryParameters = urlQueryParameters
self.urlPathParameters = urlPathParameters
if !urlPathParameters.isEmpty {
for urlPathParameter in urlPathParameters {
self.resource = self.resource.replacingOccurrences(of: urlPathParameter.key, with: urlPathParameter.value)
}
}
}
}