Files
jarvis/Jarvis/Networking/APIClient.swift

223 lines
8.2 KiB
Swift
Raw Permalink Normal View History

// APIClient.swift
// Jarvis REST networking layer
import Foundation
enum APIError: Error, LocalizedError {
case invalidURL
case serverError(code: String, message: String, status: Int)
case decodingError(Error)
case networkError(Error)
case notConnected
var errorDescription: String? {
switch self {
case .invalidURL: return "Invalid server URL"
case .notConnected: return "Not connected to a Jarvis server"
case .serverError(_, let msg, _): return msg
case .decodingError(let e): return "Decode error: \(e.localizedDescription)"
case .networkError(let e): return e.localizedDescription
}
}
/// A benign URLSession cancellation (-999), e.g. a request superseded or
/// dropped on a network-path change. Should not be shown or wipe state.
var isCancelled: Bool {
if case .networkError(let e) = self { return (e as? URLError)?.code == .cancelled }
return false
}
}
private struct APIErrorResponse: Decodable {
struct Inner: Decodable { let code, message: String; let status: Int }
let error: Inner
}
private struct EmptyResponse: Decodable {}
actor APIClient {
static let shared = APIClient()
private var baseURL: URL?
private let session: URLSession
private let decoder: JSONDecoder
private let deviceId: String
private init() {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 10
self.session = URLSession(configuration: config)
self.deviceId = APIClient.loadDeviceId()
self.decoder = JSONDecoder()
self.decoder.dateDecodingStrategy = .iso8601
self.decoder.keyDecodingStrategy = .convertFromSnakeCase
}
private static func loadDeviceId() -> String {
let key = "jarvis.device.id"
if let existing = UserDefaults.standard.string(forKey: key), !existing.isEmpty {
return existing
}
let value = UUID().uuidString.lowercased()
UserDefaults.standard.set(value, forKey: key)
return value
}
func configure(host: String) throws {
guard let url = URL(string: "http://\(host)/api/v1") else {
throw APIError.invalidURL
}
self.baseURL = url
}
// MARK: - Health
func checkHealth() async throws -> ServerHealth {
try await get("/health")
}
// MARK: - Stories
func fetchStories(
cursor: String? = nil,
topic: String? = nil,
tags: Set<String> = [],
minSignal: Int? = nil,
limit: Int = 20,
includeRead: Bool = false
) async throws -> PaginatedStories {
var params: [String: String] = ["limit": "\(limit)", "include_read": includeRead ? "true" : "false"]
if let cursor { params["after"] = cursor }
if let topic { params["topic"] = topic }
if !tags.isEmpty { params["tags"] = tags.sorted().joined(separator: ",") }
if let minSignal { params["min_signal"] = "\(minSignal)" }
return try await get("/stories", params: params)
}
func fetchStory(id: String) async throws -> StoryDetail {
try await get("/stories/\(id)")
}
func markStoryRead(id: String) async throws {
try await postVoid("/stories/\(id)/read", body: [
"device_id": deviceId,
"read_at": ISO8601DateFormatter().string(from: Date()),
"source": "ios",
])
}
func markStoryUnread(id: String) async throws {
try await delete("/stories/\(id)/read")
}
func markAllRead(storyIds: [String], pill: String) async throws {
try await postVoid("/me/read/bulk", body: [
"storyIds": storyIds,
"readAt": ISO8601DateFormatter().string(from: Date()),
"source": "mark_all_as_read",
"pill": pill,
"deviceId": deviceId,
])
}
// MARK: - Articles
func fetchArticle(id: String) async throws -> Article {
try await get("/articles/\(id)")
}
// MARK: - Feeds
func fetchFeeds() async throws -> [Feed] {
struct FeedsResponse: Decodable { let data: [Feed] }
let response: FeedsResponse = try await get("/feeds")
return response.data
}
func addFeed(url: String, name: String, pollInterval: Int = 900) async throws -> Feed {
let body = ["url": url, "name": name, "pollIntervalSeconds": pollInterval] as [String: Any]
return try await post("/feeds", body: body)
}
func deleteFeed(id: String) async throws {
try await delete("/feeds/\(id)")
}
// MARK: - Private helpers
private func get<T: Decodable>(_ path: String, params: [String: String] = [:]) async throws -> T {
guard let base = baseURL else { throw APIError.notConnected }
var components = URLComponents(url: base.appendingPathComponent(path), resolvingAgainstBaseURL: false)!
if !params.isEmpty {
components.queryItems = params.map { URLQueryItem(name: $0.key, value: $0.value) }
}
guard let url = components.url else { throw APIError.invalidURL }
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue(deviceId, forHTTPHeaderField: "X-Jarvis-Device-Id")
return try await execute(request)
}
private func post<T: Decodable>(_ path: String, body: [String: Any]) async throws -> T {
guard let base = baseURL else { throw APIError.notConnected }
let url = base.appendingPathComponent(path)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue(deviceId, forHTTPHeaderField: "X-Jarvis-Device-Id")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONSerialization.data(withJSONObject: body)
return try await execute(request)
}
private func postVoid(_ path: String, body: [String: Any]) async throws {
guard let base = baseURL else { throw APIError.notConnected }
let url = base.appendingPathComponent(path)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue(deviceId, forHTTPHeaderField: "X-Jarvis-Device-Id")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONSerialization.data(withJSONObject: body)
let (_, response) = try await session.data(for: request)
guard let http = response as? HTTPURLResponse, (200...299).contains(http.statusCode) else {
throw APIError.serverError(code: "post_failed", message: "Post failed", status: 500)
}
}
private func delete(_ path: String) async throws {
guard let base = baseURL else { throw APIError.notConnected }
let url = base.appendingPathComponent(path)
var request = URLRequest(url: url)
request.httpMethod = "DELETE"
request.setValue(deviceId, forHTTPHeaderField: "X-Jarvis-Device-Id")
let (_, response) = try await session.data(for: request)
guard let http = response as? HTTPURLResponse, http.statusCode == 204 else {
throw APIError.serverError(code: "delete_failed", message: "Delete failed", status: 500)
}
}
private func execute<T: Decodable>(_ request: URLRequest) async throws -> T {
do {
let (data, response) = try await session.data(for: request)
guard let http = response as? HTTPURLResponse else {
throw APIError.networkError(URLError(.badServerResponse))
}
if !(200...299).contains(http.statusCode) {
if let err = try? decoder.decode(APIErrorResponse.self, from: data) {
throw APIError.serverError(code: err.error.code, message: err.error.message, status: err.error.status)
}
throw APIError.serverError(code: "unknown", message: "HTTP \(http.statusCode)", status: http.statusCode)
}
do {
return try decoder.decode(T.self, from: data)
} catch {
throw APIError.decodingError(error)
}
} catch let error as APIError {
throw error
} catch {
throw APIError.networkError(error)
}
}
}