From bd7761216a065b0dd859cb19d709996739a240cd Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Sun, 17 Jul 2022 02:25:12 +1000 Subject: Clean up force unwraps and lets Wow, I didn't know `if let` was a thing back then, haha. Also made UIImage a bit safer in case asset is missing by unwrapping in one common place. --- foray/Fetchers/ForayFetcher.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'foray/Fetchers/ForayFetcher.swift') diff --git a/foray/Fetchers/ForayFetcher.swift b/foray/Fetchers/ForayFetcher.swift index e4c6fd9..15db4c1 100644 --- a/foray/Fetchers/ForayFetcher.swift +++ b/foray/Fetchers/ForayFetcher.swift @@ -21,20 +21,22 @@ class ForayFetcher { let dateFormat = DateFormatter() dateFormat.dateFormat = "yyyy-MM-dd" - + + // OK to throw as I believe it just errors out the decode; it isn't what we expected schema wise return dateFormat.date(from: dateStr)! }) return jd }() - func fetch(url: String) async throws -> T { - var request = URLRequest(url: URL(string: url)!) + func fetch(url: URL) async throws -> T { + var request = URLRequest(url: url) request.cachePolicy = .reloadRevalidatingCacheData // Needed otherwise default caching policy seems not to check properly // Basic auth if required - if (self.basicUsername != nil && self.basicPassword != nil) { - let authData = (self.basicUsername! + ":" + self.basicPassword!).data(using: .utf8)!.base64EncodedString() - request.addValue("Basic \(authData)", forHTTPHeaderField: "Authorization") + if let basicUsername = basicUsername, + let basicPassword = basicPassword, + let authData = (basicUsername + ":" + basicPassword).data(using: .utf8) { + request.addValue("Basic \(authData.base64EncodedString())", forHTTPHeaderField: "Authorization") } let (data, _) = try await URLSession.shared.data(for: request) -- cgit