import Foundation /// Uploads the social.org file to a WebDAV server via HTTP PUT. /// /// Supports optional Basic Auth. The `url` must point directly to the /// `social.org` file on the WebDAV server. public struct WebDAVUploader: FeedUploader { public let url: URL public let username: String? public let password: String? private let session: URLSession public init(url: URL, username: String? = nil, password: String? = nil, session: URLSession = .shared) { self.url = url self.username = username self.password = password self.session = session } public func upload(content: String) async throws { var request = URLRequest(url: url) request.httpMethod = "PUT" request.setValue("text/plain; charset=utf-8", forHTTPHeaderField: "Content-Type") request.httpBody = Data(content.utf8) if let username, let password, !username.isEmpty { let credentials = Data("\(username):\(password)".utf8).base64EncodedString() request.setValue("Basic \(credentials)", forHTTPHeaderField: "Authorization") } let (_, response): (Data, URLResponse) do { (_, response) = try await session.data(for: request) } catch { throw UploadError.networkError(underlying: error.localizedDescription) } if let http = response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) { throw UploadError.uploadFailed(statusCode: http.statusCode) } } }