Guest User

Untitled

a guest
May 3rd, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.55 KB | None | 0 0
  1. class ViewModel : ObservableObject {
  2.     @Published var searchedSongs = [SongDetails]()
  3.         d
  4.     @Published var apiResponse : APIResponse?
  5.     @Published var lyricsHistory : SongDetails?
  6.     @Published var apiError: APIError?
  7.    
  8.     func loadApiSongData2(songName : String, artistName : String) {
  9.         let rawUrl = "https://api.lyrics.ovh/v1/\(artistName)/\(songName)"
  10.         let fixedUrl = rawUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  11.         print("Old url: \(rawUrl)")
  12.         print("New url: \(fixedUrl!)")
  13.        
  14.         guard let url = URL(string: fixedUrl!) else {
  15.             print("Invalid URL")
  16.             return
  17.         }
  18.        
  19.         let request = URLRequest(url: url)
  20.        
  21.         URLSession.shared.dataTask(with: request) { data, response, error in
  22.                 DispatchQueue.main.async {
  23.                     self.responseHandler(data, response, error, songName, artistName)
  24.                 }
  25.             }.resume()
  26.     }
  27.    
  28.    
  29.     private func responseHandler(_ data: Data?,
  30.                          _ response: URLResponse?,
  31.                          _ error: Error?,
  32.                          _ songName: String,
  33.                          _ artistName: String) {
  34.         if let error = error {
  35.             if error._code == -1009 {
  36.                 apiError = .offline
  37.             } else {
  38.                 apiError = .sessionError
  39.             }
  40.             return
  41.         }
  42.         guard let response = response as? HTTPURLResponse, let data = data else {
  43.             apiError = .missingDataError
  44.             return
  45.         }
  46.         guard (200..<300).contains(response.statusCode) else {
  47.             switch Status(rawValue: response.statusCode) {
  48.             case .requestTimeout:
  49.                 apiError = .timeoutError
  50.             case .internalServerError:
  51.                 apiError = .internalServerError
  52.             case .notFound:
  53.                 apiError = .notFound
  54.             default:
  55.                 apiError = .requestError
  56.             }
  57.            
  58.             return
  59.         }
  60.         do {
  61.             let decodedResponse = try JSONDecoder().decode(Song.self, from: data)
  62.             if(!songAlreadySearched(songName: songName)) {
  63.                 let song = SongDetails(songName: songName, artistName: artistName, lyrics: decodedResponse.lyrics)
  64.                 searchedSongs.append(song)
  65.             }
  66.             apiResponse = APIResponse(apiValues: [decodedResponse.lyrics])
  67.            
  68.         } catch {
  69.             apiError = .parsingError
  70.         }
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment