· 6 years ago · Nov 11, 2019, 08:44 PM
1class NetworkManager {
2
3 private init() {}
4 static let shared = NetworkManager()
5
6 private let apikey = "Your Api Key"
7 private let url = "https://free.currconv.com/api/v7/convert"
8
9 func getCurrentCurrency(complition: @escaping (Response)->Void) {
10 var urlComponents = URLComponents(string: url)
11 urlComponents?.queryItems = [
12 URLQueryItem(name: "q", value: "USD_BYN,BYN_USD"),
13 URLQueryItem(name: "compact", value: "ultra"),
14 URLQueryItem(name: "apiKey", value: "\(apikey)")
15 ]
16 let url = urlComponents?.url
17 let task = URLSession.shared.dataTask(with: url!) {
18 (data, response, error) in
19
20 guard let data = data else { return }
21 guard let currency: Response = try? JSONDecoder().decode(Response.self, from: data) else {
22 print("Error can't parse")
23 return
24 }
25 return complition(currency)
26 }
27 task.resume()
28
29 }
30
31}