From 45e970e19a8d4253968610808e3adf1cc0882fa6 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sat, 28 May 2022 21:50:28 -0400 Subject: [PATCH] User agent --- ntfy/App/AppMain.swift | 2 +- ntfy/Utils/ApiService.swift | 20 +++++++++++++------- ntfy/Utils/Config.swift | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ntfy/App/AppMain.swift b/ntfy/App/AppMain.swift index 2cd58cb..3bf1265 100644 --- a/ntfy/App/AppMain.swift +++ b/ntfy/App/AppMain.swift @@ -13,7 +13,7 @@ struct AppMain: App { init() { Log.d(tag, "Launching ntfy 🥳. Welcome!") - Log.d(tag, "Base URL is \(Config.appBaseUrl)") + Log.d(tag, "Base URL is \(Config.appBaseUrl), user agent is \(ApiService.userAgent)") // We must configure Firebase here, and not in the AppDelegate. For some reason // configuring it there did not work. diff --git a/ntfy/Utils/ApiService.swift b/ntfy/Utils/ApiService.swift index 469c591..b398c46 100644 --- a/ntfy/Utils/ApiService.swift +++ b/ntfy/Utils/ApiService.swift @@ -1,8 +1,10 @@ import Foundation class ApiService { - private let tag = "ApiService" static let shared = ApiService() + static let userAgent = "ntfy/\(Config.version) (build \(Config.build); iOS \(Config.osVersion))" + + private let tag = "ApiService" func poll(subscription: Subscription, completionHandler: @escaping ([Message]?, Error?) -> Void) { guard let url = URL(string: subscription.urlString()) else { @@ -20,7 +22,10 @@ class ApiService { let url = URL(string: "\(subscription.urlString())/json?poll=1&id=\(messageId)")! Log.d(tag, "Polling single message from \(url)") - URLSession.shared.dataTask(with: URLRequest(url: url)) { (data, response, error) in + var request = URLRequest(url: url) + request.setValue(ApiService.userAgent, forHTTPHeaderField: "User-Agent") + + URLSession.shared.dataTask(with: request) { (data, response, error) in if let error = error { completionHandler(nil, error) return @@ -47,6 +52,7 @@ class ApiService { Log.d(tag, "Publishing to \(url)") request.httpMethod = "POST" + request.setValue(ApiService.userAgent, forHTTPHeaderField: "User-Agent") request.setValue(title, forHTTPHeaderField: "Title") request.setValue(String(priority), forHTTPHeaderField: "Priority") request.setValue(tags.joined(separator: ","), forHTTPHeaderField: "Tags") @@ -62,15 +68,15 @@ class ApiService { private func fetchJsonData(urlString: String, completionHandler: @escaping ([T]?, Error?) -> ()) { guard let url = URL(string: urlString) else { return } - let request = URLRequest(url: url) - + var request = URLRequest(url: url) + request.setValue(ApiService.userAgent, forHTTPHeaderField: "User-Agent") + URLSession.shared.dataTask(with: request) { (data, response, error) in if let error = error { - print(error) + Log.e(self.tag, "Error fetching data", error) completionHandler(nil, error) return } - do { let lines = String(decoding: data!, as: UTF8.self).split(whereSeparator: \.isNewline) var notifications: [T] = [] @@ -79,7 +85,7 @@ class ApiService { } completionHandler(notifications, nil) } catch { - print(error) + Log.e(self.tag, "Error fetching data", error) completionHandler(nil, error) } }.resume() diff --git a/ntfy/Utils/Config.swift b/ntfy/Utils/Config.swift index f1b808c..4349f27 100644 --- a/ntfy/Utils/Config.swift +++ b/ntfy/Utils/Config.swift @@ -4,7 +4,20 @@ enum Config { static var appBaseUrl: String { string(for: "AppBaseURL") } - + + static var build: String { + string(for: "CFBundleVersion") + } + + static var version: String { + string(for: "CFBundleShortVersionString") + } + + static var osVersion: String { + let os = ProcessInfo.processInfo.operatingSystemVersion + return String(os.majorVersion) + "." + String(os.minorVersion) + "." + String(os.patchVersion) + } + static private func string(for key: String) -> String { Bundle.main.infoDictionary?[key] as! String }