func
fetchCurrentExchangeRate
(currencyCode: String, handler: @escaping (ExchangeRate?)
-> ()) {
guard
let
currencyURL =
URL
(string: currencyConversionBaseURLString + currencyAccessKey +
"¤cies=(currencyCode)&format=1"
)
else
{
print
(
"
!
currency url error"
)
return
}
let
urlRequest =
URLRequest
(url: currencyURL)
let
session =
URLSession
.shared
let
task = session.dataTask(with: urlRequest, completionHandler: { data, response, error
in
guard
error ==
nil
else
{
print
(
"
!
request error: (String(describing: error))"
)
return
}
guard
let
responseData = data
else
{
print
(
"
!
data response error"
)
return
}
guard
let
exchangeRate:
ExchangeRate
self
.decodeExchangeRateData(currencyData: responseData)
else
{
print
(
"
!
decoding error"
)
return
}
handler(exchangeRate)
})
task.resume()
}