Created
July 3, 2017 14:25
-
-
Save malcolmkmd/c203656cab74afcd3a4b717e6606c534 to your computer and use it in GitHub Desktop.
Movie API
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
import Foundation | |
import Moya | |
enum MovieApi { | |
case reco(id:Int) | |
case topRated(page:Int) | |
case newMovies(page:Int) | |
case video(id:Int) | |
} | |
extension MovieApi: TargetType { | |
var baseURL: URL { | |
guard let url = URL(string: "https://api.themoviedb.org/3/movie/") else { fatalError("baseURL could not be configured") } | |
return url | |
} | |
var path: String { | |
switch self { | |
case .reco(let id): | |
return "\(id)/recommendations" | |
case .topRated: | |
return "popular" | |
case .newMovies: | |
return "now_playing" | |
case .video(let id): | |
return "\(id)/videos" | |
} | |
} | |
var method: Moya.Method { | |
switch self { | |
case .reco, .topRated, .newMovies, .video: | |
return .get | |
} | |
} | |
var parameters: [String : Any]? { | |
switch self { | |
case .reco, .video: | |
return ["api_key": API.apiKey] | |
case .topRated(let page), .newMovies(let page): | |
return ["page": page, "api_key": API.apiKey] | |
} | |
} | |
var parameterEncoding: ParameterEncoding { | |
switch self { | |
case .reco, .topRated, .newMovies, .video: | |
return URLEncoding.queryString | |
} | |
} | |
var task: Task { | |
switch self { | |
case .reco, .topRated, .newMovies, .video: | |
return .request | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment