User agent
This commit is contained in:
parent
575e2dbe67
commit
45e970e19a
3 changed files with 28 additions and 9 deletions
|
|
@ -13,7 +13,7 @@ struct AppMain: App {
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
Log.d(tag, "Launching ntfy 🥳. Welcome!")
|
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
|
// We must configure Firebase here, and not in the AppDelegate. For some reason
|
||||||
// configuring it there did not work.
|
// configuring it there did not work.
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class ApiService {
|
class ApiService {
|
||||||
private let tag = "ApiService"
|
|
||||||
static let shared = 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) {
|
func poll(subscription: Subscription, completionHandler: @escaping ([Message]?, Error?) -> Void) {
|
||||||
guard let url = URL(string: subscription.urlString()) else {
|
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)")!
|
let url = URL(string: "\(subscription.urlString())/json?poll=1&id=\(messageId)")!
|
||||||
Log.d(tag, "Polling single message from \(url)")
|
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 {
|
if let error = error {
|
||||||
completionHandler(nil, error)
|
completionHandler(nil, error)
|
||||||
return
|
return
|
||||||
|
|
@ -47,6 +52,7 @@ class ApiService {
|
||||||
Log.d(tag, "Publishing to \(url)")
|
Log.d(tag, "Publishing to \(url)")
|
||||||
|
|
||||||
request.httpMethod = "POST"
|
request.httpMethod = "POST"
|
||||||
|
request.setValue(ApiService.userAgent, forHTTPHeaderField: "User-Agent")
|
||||||
request.setValue(title, forHTTPHeaderField: "Title")
|
request.setValue(title, forHTTPHeaderField: "Title")
|
||||||
request.setValue(String(priority), forHTTPHeaderField: "Priority")
|
request.setValue(String(priority), forHTTPHeaderField: "Priority")
|
||||||
request.setValue(tags.joined(separator: ","), forHTTPHeaderField: "Tags")
|
request.setValue(tags.joined(separator: ","), forHTTPHeaderField: "Tags")
|
||||||
|
|
@ -62,15 +68,15 @@ class ApiService {
|
||||||
|
|
||||||
private func fetchJsonData<T: Decodable>(urlString: String, completionHandler: @escaping ([T]?, Error?) -> ()) {
|
private func fetchJsonData<T: Decodable>(urlString: String, completionHandler: @escaping ([T]?, Error?) -> ()) {
|
||||||
guard let url = URL(string: urlString) else { return }
|
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
|
URLSession.shared.dataTask(with: request) { (data, response, error) in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
print(error)
|
Log.e(self.tag, "Error fetching data", error)
|
||||||
completionHandler(nil, error)
|
completionHandler(nil, error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let lines = String(decoding: data!, as: UTF8.self).split(whereSeparator: \.isNewline)
|
let lines = String(decoding: data!, as: UTF8.self).split(whereSeparator: \.isNewline)
|
||||||
var notifications: [T] = []
|
var notifications: [T] = []
|
||||||
|
|
@ -79,7 +85,7 @@ class ApiService {
|
||||||
}
|
}
|
||||||
completionHandler(notifications, nil)
|
completionHandler(notifications, nil)
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
Log.e(self.tag, "Error fetching data", error)
|
||||||
completionHandler(nil, error)
|
completionHandler(nil, error)
|
||||||
}
|
}
|
||||||
}.resume()
|
}.resume()
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,20 @@ enum Config {
|
||||||
static var appBaseUrl: String {
|
static var appBaseUrl: String {
|
||||||
string(for: "AppBaseURL")
|
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 {
|
static private func string(for key: String) -> String {
|
||||||
Bundle.main.infoDictionary?[key] as! String
|
Bundle.main.infoDictionary?[key] as! String
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue