tokenary/Shared/Services/GasService.swift

51 lines
1.2 KiB
Swift
Raw Normal View History

2021-11-30 15:56:00 +03:00
// Copyright © 2021 Tokenary. All rights reserved.
2021-06-28 16:20:08 +03:00
import Foundation
2021-06-28 17:22:41 +03:00
class GasService {
struct Message: Codable {
let data: Info
}
2021-06-28 16:20:08 +03:00
struct Info: Codable {
2021-06-28 17:22:41 +03:00
let standard: UInt
let slow: UInt
let fast: UInt
let rapid: UInt
var sortedValues: [UInt] {
return Set([slow, standard, fast, rapid]).sorted()
}
}
2021-06-28 16:20:08 +03:00
static let shared = GasService()
2021-06-28 17:22:41 +03:00
private let jsonDecoder = JSONDecoder()
2021-06-29 01:11:04 +03:00
private let urlSession = URLSession(configuration: .default)
2021-06-28 17:22:41 +03:00
2021-06-29 01:11:04 +03:00
private init() {}
2021-06-28 16:20:08 +03:00
var currentInfo: Info?
2021-06-28 16:20:08 +03:00
func start() {
2021-06-28 17:22:41 +03:00
getMessage()
}
private func getMessage() {
2021-11-28 13:52:53 +03:00
let url = URL(string: "https://etherchain.org/api/gasnow")!
let dataTask = urlSession.dataTask(with: url) { [weak self] (data, _, _) in
if let data = data, let info = try? self?.jsonDecoder.decode(Message.self, from: data).data {
2021-06-28 17:22:41 +03:00
DispatchQueue.main.async {
self?.currentInfo = info
}
2021-11-28 13:52:53 +03:00
}
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(30)) {
2021-06-29 01:11:04 +03:00
self?.getMessage()
2021-06-28 17:22:41 +03:00
}
}
2021-11-28 13:52:53 +03:00
dataTask.resume()
2021-06-28 16:20:08 +03:00
}
}