Last active
March 27, 2020 00:11
-
-
Save clc80/54c0dfbff08efcea0a267503457b960f to your computer and use it in GitHub Desktop.
Cocktail Results Controller
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
// | |
// CocktailResultController.swift | |
// cocktailMaker | |
// | |
// Created by Claudia Contreras on 3/25/20. | |
// Copyright © 2020 thecoderpilot. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class CocktailResultController { | |
// MARK: - Properties | |
//To store our cocktails | |
var cocktailResults: [CocktailResults] = [] | |
//We want to keep track of our possible errors | |
enum NetworkError: Error { | |
case otherError(Error) | |
case noData | |
case decodeFailed | |
} | |
enum HTTPMethod: String { | |
case get = "GET" | |
case post = "POST" | |
case pull = "PULL" | |
case delete = "DELETE" | |
} | |
enum Endpoints { | |
static let baseURL = "https://www.thecocktaildb.com/api/json/v1/1/" | |
//This is where we store the different endpoint cases. They are named based on their functionality | |
case getRandomCocktail | |
case getImage(String) | |
var stringValue: String { | |
switch self { | |
case .getRandomCocktail: | |
return Endpoints.baseURL + "/random.php" | |
case.getImage(let imagePath): | |
return imagePath | |
} | |
} | |
var url: URL { | |
return URL(string: stringValue)! | |
} | |
} | |
// MARK: - Functions | |
// Function to get a random Drink | |
func getRandomCocktail(completion: @escaping (Result<[CocktailResults], NetworkError>) -> Void) { | |
//Build up the URL with necessary information | |
var request = URLRequest(url: Endpoints.getRandomCocktail.url) | |
request.httpMethod = HTTPMethod.get.rawValue | |
//Request the data | |
URLSession.shared.dataTask(with: request) { (data, response, error) in | |
guard error == nil else { | |
DispatchQueue.main.async { | |
completion(.failure(.otherError(error!))) | |
} | |
return | |
} | |
guard let data = data else { | |
DispatchQueue.main.async { | |
completion(.failure(.noData)) | |
} | |
return | |
} | |
//Decode the data | |
let decoder = JSONDecoder() | |
do { | |
self.cocktailResults = try decoder.decode([CocktailResults].self, from: data) | |
DispatchQueue.main.async { | |
completion(.success(self.cocktailResults)) | |
} | |
} catch { | |
DispatchQueue.main.async { | |
completion(.failure(.decodeFailed)) | |
} | |
return | |
} | |
}.resume() | |
} | |
func downloadCocktailImage(path: String, completion: @escaping (Result<Data, NetworkError>) -> Void) { | |
//Build up the URL with necessary information | |
var request = URLRequest(url: Endpoints.getImage(path).url) | |
request.httpMethod = HTTPMethod.get.rawValue | |
//Request the image | |
URLSession.shared.dataTask(with: request) { (data, response, error) in | |
guard error == nil else { | |
DispatchQueue.main.async { | |
completion(.failure(.otherError(error!))) | |
} | |
return | |
} | |
guard let data = data else { | |
DispatchQueue.main.async { | |
completion(.failure(.noData)) | |
} | |
return | |
} | |
DispatchQueue.main.async { | |
completion(.success(data)) | |
} | |
}.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment