krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.0.4: Hutch/Networking/HutchStatsService.swift · raw

  1import Foundation
  2
  3protocol ContributionCalendarServing: Sendable {
  4    func fetchContributionCalendar(actor: String, endingOn endDate: Date) async throws -> ContributionCalendarResponse
  5    func fetchContributionStats(actor: String, endingOn endDate: Date) async throws -> ContributionStatsResponse
  6}
  7
  8struct HutchStatsService: ContributionCalendarServing {
  9    private let session: URLSession
 10    private let decoder: JSONDecoder
 11    private let baseURL: URL
 12    private let currentActor: String?
 13
 14    init(
 15        session: URLSession = .shared,
 16        configuration: AppConfiguration,
 17        currentActor: String? = nil
 18    ) {
 19        self.session = session
 20        self.baseURL = configuration.hutchStatsBaseURL
 21        self.decoder = JSONDecoder()
 22        self.currentActor = currentActor
 23    }
 24
 25    func fetchContributionCalendar(actor: String, endingOn endDate: Date) async throws -> ContributionCalendarResponse {
 26        return try await fetch(
 27            path: "api/contributions/\(actor)",
 28            queryItems: contributionQueryItems(actor: actor, endingOn: endDate),
 29            responseType: ContributionCalendarResponse.self
 30        )
 31    }
 32
 33    func fetchContributionStats(actor: String, endingOn endDate: Date) async throws -> ContributionStatsResponse {
 34        return try await fetch(
 35            path: "api/contributions/\(actor)/stats",
 36            queryItems: contributionQueryItems(actor: actor, endingOn: endDate),
 37            responseType: ContributionStatsResponse.self
 38        )
 39    }
 40
 41    private func fetch<Response: Decodable>(
 42        path: String,
 43        queryItems: [URLQueryItem],
 44        responseType: Response.Type
 45    ) async throws -> Response {
 46        guard var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: false) else {
 47            throw URLError(.badURL)
 48        }
 49
 50        components.path = normalizedPath(basePath: components.path, appendedPath: path)
 51        components.queryItems = queryItems
 52
 53        guard let url = components.url else {
 54            throw URLError(.badURL)
 55        }
 56
 57        let (data, response): (Data, URLResponse)
 58        do {
 59            (data, response) = try await session.data(from: url)
 60        } catch {
 61            throw SRHTError.networkError(error)
 62        }
 63
 64        if let httpResponse = response as? HTTPURLResponse {
 65            if !(200...299).contains(httpResponse.statusCode) {
 66                throw SRHTError.httpError(httpResponse.statusCode)
 67            }
 68        }
 69
 70        do {
 71            return try decoder.decode(responseType, from: data)
 72        } catch {
 73            throw SRHTError.decodingError(error)
 74        }
 75    }
 76
 77    private func normalizedPath(basePath: String, appendedPath: String) -> String {
 78        let trimmedBase = basePath.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
 79        let trimmedAppendix = appendedPath.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
 80
 81        let pathComponents = [trimmedBase, trimmedAppendix].filter { !$0.isEmpty }
 82        return "/" + pathComponents.joined(separator: "/")
 83    }
 84
 85    func trailingRange(endingOn endDate: Date) -> ClosedRange<Date> {
 86        let normalizedEndDate = Calendar.contributionCalendar.startOfDay(for: endDate)
 87        let oneYearBack = Calendar.contributionCalendar.date(byAdding: .year, value: -1, to: normalizedEndDate) ?? normalizedEndDate
 88        let normalizedStartDate = Calendar.contributionCalendar.date(byAdding: .day, value: 1, to: oneYearBack) ?? oneYearBack
 89        return normalizedStartDate...normalizedEndDate
 90    }
 91
 92    func contributionQueryItems(actor: String, endingOn endDate: Date) -> [URLQueryItem] {
 93        let range = trailingRange(endingOn: endDate)
 94        let start = Self.rangeFormatter.string(from: range.lowerBound)
 95        let end = Self.rangeFormatter.string(from: range.upperBound)
 96
 97        var queryItems = [
 98            URLQueryItem(name: "from", value: start),
 99            URLQueryItem(name: "to", value: end)
100        ]
101
102        if actor == currentActor {
103            queryItems.append(URLQueryItem(name: "prioritize", value: "self"))
104        }
105
106        return queryItems
107    }
108
109    private static let rangeFormatter: DateFormatter = {
110        let formatter = DateFormatter()
111        formatter.calendar = .contributionCalendar
112        formatter.locale = Locale(identifier: "en_US_POSIX")
113        formatter.timeZone = TimeZone(secondsFromGMT: 0)
114        formatter.dateFormat = "yyyy-MM-dd"
115        return formatter
116    }()
117
118}