User agent

This commit is contained in:
Philipp Heckel 2022-05-28 21:50:28 -04:00
parent 575e2dbe67
commit 45e970e19a
3 changed files with 28 additions and 9 deletions

View file

@ -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.

View file

@ -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<T: Decodable>(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()

View file

@ -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
}