aboutsummaryrefslogtreecommitdiff
path: root/foray/Fetchers/ForayFetcher.swift
blob: 15db4c18a34d4dcfb382cc720a58799d9ee787c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
//  ForayNetworkManager.swift
//  foray
//
//  Created by Nicholas Tay on 20/3/2022.
//

import Foundation

class ForayFetcher {
    var basicUsername: String? = nil
    var basicPassword: String? = nil
    
    // Reuse JSON decoder, allows for customisation of things like date decode if required
    var jsonDecoder: JSONDecoder = {
        let jd = JSONDecoder()
        // Defaults to year-month-date format
        jd.dateDecodingStrategy = .custom({ (decoder) -> Date in
            let container = try decoder.singleValueContainer()
            let dateStr = try container.decode(String.self)
            
            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: 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 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)
        let items = try jsonDecoder.decode(T.self, from: data)
        return items
    }
}