-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send Coindesk related code to its own folder. Create
CoindeskApiClient
- Loading branch information
penafernando
committed
Oct 27, 2022
1 parent
5fe4daa
commit aacf5af
Showing
4 changed files
with
71 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// CoindeskApiClient.swift | ||
// CurrenciesApp | ||
// | ||
// Created by Fernando Pena on 27/10/2022. | ||
// | ||
|
||
import Foundation | ||
|
||
class CoindeskApiClient { | ||
func fetchCurrencies(completionHandler: @escaping (Result<CoindeskResponseDTO, Error>) -> Void) { | ||
let url = URL(string: "https://production.api.coindesk.com/v2/exchange-rates")! | ||
URLSession.shared.dataTask(with: url) { (data, response, error) in | ||
guard let data = data else { | ||
completionHandler(.failure(error ?? ApiClientError.emptyData)) | ||
return | ||
} | ||
do { | ||
let dto = try JSONDecoder().decode(CoindeskResponseDTO.self, from: data) | ||
completionHandler(.success(dto)) | ||
} catch { | ||
completionHandler(.failure(error)) | ||
} | ||
}.resume() | ||
} | ||
} | ||
|
||
enum ApiClientError: Error { | ||
case emptyData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// CoindeskResponseDTO.swift | ||
// CurrenciesApp | ||
// | ||
// Created by Fernando Pena on 27/10/2022. | ||
// | ||
|
||
import Foundation | ||
|
||
struct CoindeskResponseDTO: Decodable { | ||
let statusCode: Int | ||
let message: String | ||
let data: [String: Coin] | ||
|
||
struct Coin: Decodable { | ||
let iso, name, slug: String | ||
let rate: Double | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters