-
Notifications
You must be signed in to change notification settings - Fork 2
/
APIs.swift
269 lines (225 loc) · 10.9 KB
/
APIs.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// APIs.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//
import Foundation
import Alamofire
//@available(*, deprecated, renamed: "BlockfrostSDK")
//public typealias OpenAPIClientAPI = BlockfrostSDK
open class BlockfrostConfig {
public static let USER_AGENT = "BlockfrostSwiftSDK"
public static let URL_MAINNET = "https://cardano-mainnet.blockfrost.io/api/v0"
public static let URL_TESTNET = "https://cardano-testnet.blockfrost.io/api/v0"
public static let URL_IPFS = "https://ipfs.blockfrost.io/api/v0"
public static var DEFAULT_COUNT = 100
public static var DEFAULT_PAGE = 1
public static var DEFAULT_SORT_ORDER = SortOrder.asc
public static var DEFAULT_BATCH_SIZE = 10
public var basePath = URL_MAINNET
public var customHeaders: [String: String] = [:]
public var credential: URLCredential?
public var projectId: String?
public var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
//public var requestBuilderFactory: RequestBuilderFactory = URLSessionRequestBuilderFactory()
public var apiResponseQueue: DispatchQueue = .main
public var batchSize: Int = DEFAULT_BATCH_SIZE
// Alamofire dependent
public var retryPolicy: RetryPolicy? = BlockfrostRetryPolicy()
public init(basePath: String? = nil, projectId: String? = nil, apiResponseQueue: DispatchQueue? = nil, batchSize: Int? = nil, retryPolicy: RetryPolicy? = nil) {
if let x = basePath { self.basePath = x }
if let x = projectId { self.projectId = x }
if let x = apiResponseQueue { self.apiResponseQueue = x }
if let x = batchSize { self.batchSize = x }
if let x = retryPolicy { self.retryPolicy = x }
}
private static var sharedInstance: BlockfrostConfig = {
let instance = BlockfrostConfig(basePath: BlockfrostConfig.URL_MAINNET, projectId: BlockfrostConfig.getEnvProjectId() ?? BlockfrostConfig.getEnvProjectIdMainnet())
return instance
}()
private static var sharedInstanceMainnet: BlockfrostConfig = {
let instance = BlockfrostConfig(basePath: BlockfrostConfig.URL_MAINNET, projectId: BlockfrostConfig.getEnvProjectId() ?? BlockfrostConfig.getEnvProjectIdMainnet())
return instance
}()
private static var sharedInstanceTestnet: BlockfrostConfig = {
let instance = BlockfrostConfig(basePath: BlockfrostConfig.URL_TESTNET, projectId: BlockfrostConfig.getEnvProjectId() ?? BlockfrostConfig.getEnvProjectIdTestnet())
return instance
}()
private static var sharedInstanceIpfs: BlockfrostConfig = {
let instance = BlockfrostConfig(basePath: BlockfrostConfig.URL_IPFS, projectId: BlockfrostConfig.getEnvProjectId() ?? BlockfrostConfig.getEnvIpfsProjectId())
return instance
}()
public class func shared() -> BlockfrostConfig {
sharedInstance
}
public class func testnetDefault() -> BlockfrostConfig {
sharedInstanceMainnet
}
public class func mainnetDefault() -> BlockfrostConfig {
sharedInstanceTestnet
}
public class func ipfsDefault() -> BlockfrostConfig {
sharedInstanceIpfs
}
public func clone() -> BlockfrostConfig {
let r = BlockfrostConfig()
r.basePath = basePath
r.customHeaders = customHeaders
r.credential = credential
r.projectId = projectId
r.requestBuilderFactory = requestBuilderFactory
r.apiResponseQueue = apiResponseQueue
r.retryPolicy = retryPolicy
return r
}
public class func getEnvProjectId() -> String? {
ProcessInfo.processInfo.environment["BF_PROJECT_ID"]
}
public class func getEnvProjectIdMainnet() -> String? {
ProcessInfo.processInfo.environment["BF_MAINNET_PROJECT_ID"]
}
public class func getEnvProjectIdTestnet() -> String? {
ProcessInfo.processInfo.environment["BF_TESTNET_PROJECT_ID"]
}
public class func getEnvIpfsProjectId() -> String? {
ProcessInfo.processInfo.environment["BF_IPFS_PROJECT_ID"]
}
}
open class BlockfrostStaticConfig {
public static var basePath: String {
get { BlockfrostConfig.shared().basePath }
set { BlockfrostConfig.shared().basePath = newValue }}
public static var customHeaders: [String: String] {
get { BlockfrostConfig.shared().customHeaders }
set { BlockfrostConfig.shared().customHeaders = newValue }}
public static var credential: URLCredential? {
get { BlockfrostConfig.shared().credential }
set { BlockfrostConfig.shared().credential = newValue }}
public static var projectId: String? {
get { BlockfrostConfig.shared().projectId }
set { BlockfrostConfig.shared().projectId = newValue }}
public static var requestBuilderFactory: RequestBuilderFactory {
get { BlockfrostConfig.shared().requestBuilderFactory }
set { BlockfrostConfig.shared().requestBuilderFactory = newValue }}
public static var apiResponseQueue: DispatchQueue {
get { BlockfrostConfig.shared().apiResponseQueue }
set { BlockfrostConfig.shared().apiResponseQueue = newValue }}
public static var batchSize: Int {
get { BlockfrostConfig.shared().batchSize }
set { BlockfrostConfig.shared().batchSize = newValue }}
}
open class BlockfrostRetryPolicy : RetryPolicy {
/// The default HTTP status codes to retry.
/// See [RFC 2616 - Section 10](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10) for more information.
public static let bfDefaultRetryableHTTPStatusCodes: Set<Int> = [408, // [Request Timeout](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9)
429, // [Rate limited](https://docs.blockfrost.io/#section/Limits)
500, // [Internal Server Error](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1)
502, // [Bad Gateway](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3)
503, // [Service Unavailable](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4)
504 // [Gateway Timeout](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5)
]
public override init(
retryLimit: UInt = RetryPolicy.defaultRetryLimit,
exponentialBackoffBase: UInt = RetryPolicy.defaultExponentialBackoffBase,
exponentialBackoffScale: Double = RetryPolicy.defaultExponentialBackoffScale,
retryableHTTPMethods: Set<Alamofire.HTTPMethod> = RetryPolicy.defaultRetryableHTTPMethods,
retryableHTTPStatusCodes: Set<Int> = bfDefaultRetryableHTTPStatusCodes,
retryableURLErrorCodes: Set<URLError.Code> = RetryPolicy.defaultRetryableURLErrorCodes
) {
precondition(exponentialBackoffBase >= 2, "The `exponentialBackoffBase` must be a minimum of 2.")
super.init(retryLimit: retryLimit, exponentialBackoffBase: exponentialBackoffBase, exponentialBackoffScale: exponentialBackoffScale, retryableHTTPMethods: retryableHTTPMethods, retryableHTTPStatusCodes: retryableHTTPStatusCodes, retryableURLErrorCodes: retryableURLErrorCodes)
}
}
open class APIRequest {
open func cancel() -> Bool { false }
}
open class RequestBuilder<T> {
var credential: URLCredential?
var headers: [String: String]
var data: Data? = nil
var config: BlockfrostConfig = BlockfrostConfig.shared()
public let parameters: [String: Any]?
public let method: String
public let URLString: String
/// Optional block to obtain a reference to the request's progress instance when available.
/// With the URLSession http client the request's progress only works on iOS 11.0, macOS 10.13, macCatalyst 13.0, tvOS 11.0, watchOS 4.0.
/// If you need to get the request's progress in older OS versions, please use Alamofire http client.
public var onProgressReady: ((Progress) -> Void)?
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], data: Data? = nil, config: BlockfrostConfig? = nil) {
self.method = method
self.URLString = URLString
self.parameters = parameters
self.headers = headers
self.data = data
if let cfg = config { self.config = cfg }
addHeaders(self.config.customHeaders)
}
open func addHeaders(_ aHeaders: [String: String]) {
for (header, value) in aHeaders {
headers[header] = value
}
}
open func execute(_ apiResponseQueue: DispatchQueue = BlockfrostConfig.shared().apiResponseQueue,
_ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) -> APIRequest {
fatalError()
}
public func addHeader(name: String, value: String) -> Self {
if !value.isEmpty {
headers[name] = value
}
return self
}
open func addCredential() -> Self {
credential = BlockfrostConfig.shared().credential
return self
}
}
public protocol RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type
}
open class BaseService {
var config: BlockfrostConfig = BlockfrostConfig.shared()
public init(config: BlockfrostConfig? = nil) {
self.config = config ?? BlockfrostConfig.shared()
}
}
public enum SortOrder: String, CaseIterable {
case asc = "asc"
case desc = "desc"
}
public struct PaginationSetting: CustomStringConvertible, Hashable {
public var count: Int = BlockfrostConfig.DEFAULT_COUNT
public var page: Int = BlockfrostConfig.DEFAULT_PAGE
public var order = BlockfrostConfig.DEFAULT_SORT_ORDER
public init(count: Int? = nil, page: Int? = nil, order: SortOrder? = nil) {
if let _count = count {
self.count = _count
}
if let _page = page {
self.page = _page
}
if let _order = order {
self.order = _order
}
}
public var description: String {
"PaginationSetting(count: \(count), page: \(page), order: \(order))"
}
public func hash(into hasher: inout Hasher) {
hasher.combine(count)
hasher.combine(page)
hasher.combine(order)
}
}
public struct AllMethodOptions: CustomStringConvertible {
public var batchSize = BlockfrostConfig.DEFAULT_BATCH_SIZE
public var order = BlockfrostConfig.DEFAULT_SORT_ORDER
public init(batchSize: Int? = nil, order: SortOrder? = nil) {
if let x = batchSize { self.batchSize = x }
if let x = order { self.order = x }
}
public var description: String {
"AllMethodOptions(batchSize: \(batchSize), order: \(order))"
}
}