Advertisement
Larme

Untitled

Mar 16th, 2023
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.15 KB | None | 0 0
  1. class ApiManager {
  2.     let headers = [
  3.         "X-RapidAPI-Key" : "<my_api_key>",
  4.         "X-RapidAPI-Host" : "imdb-top-100-movies.p.rapidapi.com"
  5.     ]
  6.     let url = URL(string: "https://imdb-top-100-movies.p.rapidapi.com/")!
  7.     func getMovieTitle(completion: @escaping (String) -> Void) {
  8.         var request = URLRequest(url: url)
  9.         request.httpMethod = "GET"
  10.         request.allHTTPHeaderFields = headers
  11.         let task = URLSession.shared.dataTask(with: request) { data, response, error in
  12.             guard let data = data else{ print("no data"); return }
  13.             do {
  14.                 let movieElement = try JSONDecoder().decode(MovieElement.self, from: data)
  15.                 print(movieElement.title)
  16.             } catch {
  17.                 print("Error in decoding: \(error) with response: \(String(data: data, encoding: .utf8))")
  18.             }
  19. //            if let data = data,
  20. //               let movieElement = try? JSONDecoder().decode(MovieElement.self, from: data) {
  21. //                print(movieElement.title)
  22. //            } else {
  23. //                print("FAIL")
  24. //            }
  25.         }
  26.         task.resume()
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement