-
Notifications
You must be signed in to change notification settings - Fork 1
/
DownloaderClient.swift
58 lines (45 loc) · 1.71 KB
/
DownloaderClient.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// DownloaderClient.swift
// MovieBook
//
// Created by Mehmet Ateş on 28.09.2021.
//
import Foundation
import SwiftUI
struct DownloaderClient {
func downloadMovie(searchKey : String , completion : @escaping (Result<[Film]? , DownloaderError>) -> Void){
guard let url = URL (string: "https://www.omdbapi.com/?s=\(searchKey)&apikey=Token")
else{
return completion(.failure(.wrongUrl))
}
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
return completion(.failure(.cantLoad))
}
guard let filmCevap = try? JSONDecoder().decode(Gelenler.self, from: data) else{
return completion(.failure(.cantBuild))
}
completion(.success(filmCevap.search))
}.resume()
}
func downloadDetailMovie(imdbID : String, completion : @escaping (Result<FilmDetay? , DownloaderError>) -> Void){
guard let url = URL (string: "https://www.omdbapi.com/?i=\(imdbID)&apikey=Token")
else{
return completion(.failure(.wrongUrl))
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data ,error == nil else {
return completion(.failure(.cantLoad))
}
guard let filmDetay = try? JSONDecoder().decode(FilmDetay.self, from: data) else{
return completion(.failure(.cantBuild))
}
completion(.success(filmDetay))
}.resume()
}
}
enum DownloaderError : Error {
case wrongUrl
case cantLoad
case cantBuild
}