Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ApiManager {
- let headers = [
- "X-RapidAPI-Key" : "<my_api_key>",
- "X-RapidAPI-Host" : "imdb-top-100-movies.p.rapidapi.com"
- ]
- let url = URL(string: "https://imdb-top-100-movies.p.rapidapi.com/")!
- func getMovieTitle(completion: @escaping (String) -> Void) {
- var request = URLRequest(url: url)
- request.httpMethod = "GET"
- request.allHTTPHeaderFields = headers
- let task = URLSession.shared.dataTask(with: request) { data, response, error in
- guard let data = data else{ print("no data"); return }
- do {
- let movieElement = try JSONDecoder().decode(MovieElement.self, from: data)
- print(movieElement.title)
- } catch {
- print("Error in decoding: \(error) with response: \(String(data: data, encoding: .utf8))")
- }
- // if let data = data,
- // let movieElement = try? JSONDecoder().decode(MovieElement.self, from: data) {
- // print(movieElement.title)
- // } else {
- // print("FAIL")
- // }
- }
- task.resume()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement