Skip to content

Commit

Permalink
Send Coindesk related code to its own folder. Create CoindeskApiClient
Browse files Browse the repository at this point in the history
  • Loading branch information
penafernando committed Oct 27, 2022
1 parent 5fe4daa commit aacf5af
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
16 changes: 16 additions & 0 deletions CurrenciesApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
8598357629088EA200EDE361 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8598357529088EA200EDE361 /* AppDelegate.swift */; };
85BC44C5290AFC5A00A4B078 /* CoindeskResponseDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85BC44C4290AFC5A00A4B078 /* CoindeskResponseDTO.swift */; };
85BC44C7290AFC8600A4B078 /* CoindeskApiClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85BC44C6290AFC8600A4B078 /* CoindeskApiClient.swift */; };
85D0BA0F29082A2200D7309E /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85D0BA0E29082A2200D7309E /* SceneDelegate.swift */; };
85D0BA1129082A2200D7309E /* CurrenciesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85D0BA1029082A2200D7309E /* CurrenciesViewController.swift */; };
85D0BA1629082A2300D7309E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 85D0BA1529082A2300D7309E /* Assets.xcassets */; };
Expand All @@ -16,6 +18,8 @@

/* Begin PBXFileReference section */
8598357529088EA200EDE361 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
85BC44C4290AFC5A00A4B078 /* CoindeskResponseDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoindeskResponseDTO.swift; sourceTree = "<group>"; };
85BC44C6290AFC8600A4B078 /* CoindeskApiClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoindeskApiClient.swift; sourceTree = "<group>"; };
85D0BA0929082A2200D7309E /* CurrenciesApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CurrenciesApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
85D0BA0E29082A2200D7309E /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
85D0BA1029082A2200D7309E /* CurrenciesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrenciesViewController.swift; sourceTree = "<group>"; };
Expand All @@ -35,6 +39,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
85BC44C3290AFC4800A4B078 /* Coindesk */ = {
isa = PBXGroup;
children = (
85BC44C4290AFC5A00A4B078 /* CoindeskResponseDTO.swift */,
85BC44C6290AFC8600A4B078 /* CoindeskApiClient.swift */,
);
path = Coindesk;
sourceTree = "<group>";
};
85D0BA0029082A2200D7309E = {
isa = PBXGroup;
children = (
Expand All @@ -54,6 +67,7 @@
85D0BA0B29082A2200D7309E /* CurrenciesApp */ = {
isa = PBXGroup;
children = (
85BC44C3290AFC4800A4B078 /* Coindesk */,
85D0BA1029082A2200D7309E /* CurrenciesViewController.swift */,
85D0BA0E29082A2200D7309E /* SceneDelegate.swift */,
8598357529088EA200EDE361 /* AppDelegate.swift */,
Expand Down Expand Up @@ -136,6 +150,8 @@
files = (
85D0BA1129082A2200D7309E /* CurrenciesViewController.swift in Sources */,
85D0BA0F29082A2200D7309E /* SceneDelegate.swift in Sources */,
85BC44C5290AFC5A00A4B078 /* CoindeskResponseDTO.swift in Sources */,
85BC44C7290AFC8600A4B078 /* CoindeskApiClient.swift in Sources */,
8598357629088EA200EDE361 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
30 changes: 30 additions & 0 deletions CurrenciesApp/Coindesk/CoindeskApiClient.swift
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
}
19 changes: 19 additions & 0 deletions CurrenciesApp/Coindesk/CoindeskResponseDTO.swift
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
}
}
24 changes: 6 additions & 18 deletions CurrenciesApp/CurrenciesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,23 @@

import UIKit

struct CoindeskResponseDTO: Decodable {
let statusCode: Int
let message: String
let data: [String: Coin]

struct Coin: Decodable {
let iso, name, slug: String
let rate: Double
}
}

class CurrenciesViewController: UITableViewController {

var response: CoindeskResponseDTO?

override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://production.api.coindesk.com/v2/exchange-rates")!
URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in
guard let data = data else { return }
do {
self?.response = try JSONDecoder().decode(CoindeskResponseDTO.self, from: data)
CoindeskApiClient().fetchCurrencies { [weak self] result in
switch result {
case .success(let response):
self?.response = response
DispatchQueue.main.async {
self?.tableView.reloadData()
}
} catch {
case .failure(let error):
print(error)
}
}.resume()
}
}

// MARK: - UITableViewDataSource
Expand Down

0 comments on commit aacf5af

Please sign in to comment.