Skip to content

Commit

Permalink
move download media to new client
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan committed Jul 7, 2024
1 parent 5227aea commit 58ec411
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions NotificationService/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ class NotificationService: UNNotificationServiceExtension {
}

private func downloadAndAttachMedia(url: URL, completion: @escaping (UNNotificationAttachment?) -> Void) {
let task = URLSession.shared.downloadTask(with: url) { localURL, response, error in
let client = HTTPClient(username: Preferences.username, password: Preferences.username) // lets not always send auth with this
client.downloadFile(url: url) { localURL, response, error in
guard let localURL else {
os_log("Error downloading media %{PUBLIC}@", log: .default, type: .error, error?.localizedDescription ?? "Unknown error")
completion(nil)
return
}
self.attachFile(localURL: localURL, mimeType: response?.mimeType, completion: completion)
}
task.resume()
}

func downloadAndAttachItemImage(attachmentURL: URL, completion: @escaping (UNNotificationAttachment?) -> Void) {
Expand All @@ -158,7 +158,7 @@ class NotificationService: UNNotificationServiceExtension {

let itemName = String(attachmentURL.absoluteString.dropFirst(scheme.count + 1))

let client = HTTPClient(username: Preferences.username, password: Preferences.username)
let client = HTTPClient(username: Preferences.username, password: Preferences.username, alwaysSendBasicAuth: Preferences.alwaysSendCreds)
client.getItem(baseURLs: [Preferences.localUrl, Preferences.remoteUrl], itemName: itemName) { item, error in
guard let item else {
os_log("Could not find item %{PUBLIC}@", log: .default, type: .info, itemName)
Expand Down
17 changes: 12 additions & 5 deletions OpenHABCore/Sources/OpenHABCore/Util/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public class HTTPClient: NSObject, URLSessionDelegate {
}
}

public func doGet(baseURLs: [String], path: String, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
public func doGet(baseURLs: [String], path: String?, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
doRequest(baseURLs: baseURLs, path: path, method: "GET", completion: completion)
}

public func doPost(baseURLs: [String], path: String, body: String, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
public func doPost(baseURLs: [String], path: String?, body: String, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
doRequest(baseURLs: baseURLs, path: path, method: "POST", body: body, completion: completion)
}

public func baseURLs(baseURLs: [String], path: String, body: String, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
public func doPut(baseURLs: [String], path: String?, body: String, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
doRequest(baseURLs: baseURLs, path: path, method: "PUT", body: body, completion: completion)
}

Expand Down Expand Up @@ -123,6 +123,11 @@ public class HTTPClient: NSObject, URLSessionDelegate {
}
}

public func downloadFile(url: URL, completionHandler: @escaping @Sendable (URL?, URLResponse?, (any Error)?) -> Void) {
let task = session.downloadTask(with: url, completionHandler: completionHandler)
task.resume()
}

// MARK: - Private Methods

// MARK: - Basic Authentication
Expand All @@ -144,11 +149,13 @@ public class HTTPClient: NSObject, URLSessionDelegate {
}

// General function to perform HTTP requests
private func doRequest(baseURLs: [String], path: String, method: String, body: String? = nil, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
private func doRequest(baseURLs: [String], path: String?, method: String, body: String? = nil, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
var urls: [URL] = []
for urlString in baseURLs {
if var urlComponent = URLComponents(string: urlString) {
urlComponent.path = path
if let path {
urlComponent.path = path
}
if let url = urlComponent.url {
urls.append(url)
}
Expand Down

0 comments on commit 58ec411

Please sign in to comment.