aboutsummaryrefslogtreecommitdiff
path: root/foray/Fetchers
diff options
context:
space:
mode:
authorNicholas Tay <nick@windblume.net>2022-07-17 02:25:12 +1000
committerNicholas Tay <nick@windblume.net>2022-07-17 02:25:12 +1000
commitbd7761216a065b0dd859cb19d709996739a240cd (patch)
tree8b1d33034f8c212613d7a07b864d1688b8c080d2 /foray/Fetchers
parent1adbed9f8b94521befd237c14d36325a55037a41 (diff)
downloadforayios-bd7761216a065b0dd859cb19d709996739a240cd.tar.gz
forayios-bd7761216a065b0dd859cb19d709996739a240cd.tar.bz2
forayios-bd7761216a065b0dd859cb19d709996739a240cd.zip
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.
Diffstat (limited to '')
-rw-r--r--foray/Fetchers/ForayFetcher.swift14
1 files changed, 8 insertions, 6 deletions
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<T: Decodable>(url: String) async throws -> T {
- var request = URLRequest(url: URL(string: url)!)
+ func fetch<T: Decodable>(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)