ApiService.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import Foundation
  2. class ApiService {
  3. private let tag = "ApiService"
  4. static let shared = ApiService()
  5. func poll(subscription: Subscription, completionHandler: @escaping ([Message]?, Error?) -> Void) {
  6. guard let url = URL(string: subscription.urlString()) else {
  7. // FIXME
  8. return
  9. }
  10. let since = subscription.lastNotificationId ?? "all"
  11. let urlString = "\(url)/json?poll=1&since=\(since)"
  12. Log.d(tag, "Polling from \(urlString)")
  13. fetchJsonData(urlString: urlString, completionHandler: completionHandler)
  14. }
  15. func poll(subscription: Subscription, messageId: String, completionHandler: @escaping (Message?, Error?) -> Void) {
  16. let url = URL(string: "\(subscription.urlString())/json?poll=1&id=\(messageId)")!
  17. Log.d(tag, "Polling single message from \(url)")
  18. URLSession.shared.dataTask(with: URLRequest(url: url)) { (data, response, error) in
  19. if let error = error {
  20. completionHandler(nil, error)
  21. return
  22. }
  23. do {
  24. let message = try JSONDecoder().decode(Message.self, from: data!)
  25. completionHandler(message, nil)
  26. } catch {
  27. completionHandler(nil, error)
  28. }
  29. }.resume()
  30. }
  31. func publish(
  32. subscription: Subscription,
  33. message: String,
  34. title: String,
  35. priority: Int = 3,
  36. tags: [String] = []
  37. ) {
  38. guard let url = URL(string: subscription.urlString()) else { return }
  39. var request = URLRequest(url: url)
  40. Log.d(tag, "Publishing to \(url)")
  41. request.httpMethod = "POST"
  42. request.setValue(title, forHTTPHeaderField: "Title")
  43. request.setValue(String(priority), forHTTPHeaderField: "Priority")
  44. request.setValue(tags.joined(separator: ","), forHTTPHeaderField: "Tags")
  45. request.httpBody = message.data(using: String.Encoding.utf8)
  46. URLSession.shared.dataTask(with: request) { (data, response, error) in
  47. guard error == nil else {
  48. Log.e(self.tag, "Error publishing message", error!)
  49. return
  50. }
  51. Log.d(self.tag, "Publishing message succeeded", response)
  52. }.resume()
  53. }
  54. private func fetchJsonData<T: Decodable>(urlString: String, completionHandler: @escaping ([T]?, Error?) -> ()) {
  55. guard let url = URL(string: urlString) else { return }
  56. let request = URLRequest(url: url)
  57. URLSession.shared.dataTask(with: request) { (data, response, error) in
  58. if let error = error {
  59. print(error)
  60. completionHandler(nil, error)
  61. return
  62. }
  63. do {
  64. let lines = String(decoding: data!, as: UTF8.self).split(whereSeparator: \.isNewline)
  65. var notifications: [T] = []
  66. for jsonLine in lines {
  67. notifications.append(try JSONDecoder().decode(T.self, from: jsonLine.data(using: .utf8)!))
  68. }
  69. completionHandler(notifications, nil)
  70. } catch {
  71. print(error)
  72. completionHandler(nil, error)
  73. }
  74. }.resume()
  75. }
  76. }