Advertisement
alfacentaury

ApiService

May 5th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. //
  2. // ApiService.swift
  3. // academylogsapps
  4. //
  5. // Created by Alfa Centaury on 04/05/21.
  6. //
  7.  
  8. import UIKit
  9.  
  10. class ApiService {
  11. private let apiKey = ""
  12.  
  13. func getListApps(completion: @escaping ([RecordsApps]) -> Void) {
  14. let urlApps = URLComponents(string: "https://api.airtable.com/v0/appl5upXW9B14V2S2/App")!
  15.  
  16. var request = URLRequest(url: urlApps.url!)
  17. request.addValue("Bearer " + apiKey, forHTTPHeaderField: "Authorization")
  18.  
  19. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  20. if error != nil {
  21. print("Failed : Some think wrong!")
  22. } else if let data = data, let response = response as? HTTPURLResponse, response.statusCode == 200 {
  23.  
  24. let decoder = JSONDecoder()
  25. decoder.dateDecodingStrategy = .iso8601
  26.  
  27. do {
  28. let listApps = try decoder.decode(Apps.self, from: data).records
  29. completion(listApps)
  30. } catch {
  31. print("Invalid Response \(error)")
  32. }
  33. }
  34. }
  35. task.resume()
  36. }
  37.  
  38. func getListExplorer(completion: @escaping ([RecordsExplorer]) -> Void) {
  39. let urlExplorer = URLComponents(string: "https://api.airtable.com/v0/appl5upXW9B14V2S2/Explorer")!
  40.  
  41. var request = URLRequest(url: urlExplorer.url!)
  42. request.addValue("Bearer " + apiKey, forHTTPHeaderField: "Authorization")
  43.  
  44. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  45. if error != nil {
  46. print("Failed : Some think wrong!")
  47. } else if let data = data, let response = response as? HTTPURLResponse, response.statusCode == 200 {
  48.  
  49. let decoder = JSONDecoder()
  50. decoder.dateDecodingStrategy = .iso8601
  51.  
  52. do {
  53. let listExplorer = try decoder.decode(Explorer.self, from: data).records
  54. completion(listExplorer)
  55. } catch {
  56. print("Invalid Response \(error)")
  57. }
  58. }
  59. }
  60. task.resume()
  61. }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement