SPB Git

Repository Public

Endpoints.swift 5.4 KB 125 lines swift

1//2//  Auto-Ka iOS v23//  File: AutoKa/Core/API/Endpoints.swift4//  Description: points d'accès typés de l'API v2 (contrat : /tmp/autoka-v2/API.md).5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//910import Foundation1112extension APIClient {13    /// Recherche unifiée. `points` ajoute les points de carte de tous les résultats (un point par position).14    func search(_ filters: SearchFilters, sort: SearchSort = .recent, page: Int = 1, pageSize: Int = 24, points: Bool = false) async throws -> SearchResponse {15        var q = filters.query16        // le tri par distance exige un point ; sans lui, le service refuserait la requête17        q["sort"] = (sort == .distance && filters.near == nil) ? SearchSort.recent.rawValue : sort.rawValue18        q["page"] = String(page)19        q["page_size"] = String(pageSize)20        if points { q["points"] = "1" }21        return try await get("api/search", query: q)22    }2324    func count(_ filters: SearchFilters) async throws -> Int {25        try await get("api/count", query: filters.query, as: CountResponse.self).total26    }2728    func facets(_ filters: SearchFilters) async throws -> Facets {29        try await get("api/facets", query: filters.query)30    }3132    /// Véhicules d'une position de carte (un commerce, ou le centre d'une ville).33    func vehicles(at lat: Double, _ lng: Double, filters: SearchFilters, sort: SearchSort, page: Int = 1) async throws -> SearchResponse {34        var f = filters.withoutBounds35        f.near = NearPoint(lat: lat, lng: lng, label: "")36        var q = f.query37        q["radius_km"] = "0.3"38        q["sort"] = sort == .distance ? SearchSort.recent.rawValue : sort.rawValue39        q["page"] = String(page)40        q["page_size"] = "24"41        return try await get("api/search", query: q)42    }4344    func vehicle(_ uid: String) async throws -> Vehicle {45        try await get("api/listings/\(Fmt.pathComponent(uid))")46    }4748    func recalls(uid: String) async throws -> RecallCheck {49        try await get("api/listings/\(Fmt.pathComponent(uid))/recalls")50    }5152    /// Adresse de la fiche PDF d'un véhicule.53    func pdfURL(uid: String) async -> URL? {54        await url("api/listings/\(Fmt.pathComponent(uid))/pdf")55    }5657    func vin(_ niv: String) async throws -> VINResponse {58        try await get("api/vin/\(Fmt.pathComponent(niv.uppercased()))")59    }6061    func recalls(make: String, model: String, year: Int) async throws -> RecallsResponse {62        try await get("api/recalls", query: ["make": make, "model": model, "year": String(year)])63    }6465    func dealers(q: String = "", region: String? = nil, city: String? = nil, make: String? = nil, sort: String = "listings",66                 page: Int = 1, pageSize: Int = 60) async throws -> DealersResponse {67        try await get("api/concessionnaires", query: ["q": q.isEmpty ? nil : q, "region": region, "city": city, "make": make,68                                                       "sort": sort, "page": String(page), "page_size": String(pageSize)])69    }7071    func dealer(_ id: String, sort: SearchSort = .recent, page: Int = 1, pageSize: Int = 24) async throws -> DealerPage {72        try await get("api/concessionnaires/\(Fmt.pathComponent(id))",73                      query: ["sort": sort == .distance ? "recent" : sort.rawValue, "page": String(page), "page_size": String(pageSize)])74    }7576    func makes(kind: String = "auto") async throws -> MakesResponse {77        try await get("api/marques", query: ["kind": kind == "auto" ? nil : kind])78    }7980    func make(_ slug: String) async throws -> MakePage {81        try await get("api/marques/\(Fmt.pathComponent(slug))")82    }8384    func model(make: String, model: String) async throws -> ModelPage {85        try await get("api/marques/\(Fmt.pathComponent(make))/\(Fmt.pathComponent(model))")86    }8788    func cities(min: Int = 1, region: String? = nil) async throws -> CitiesResponse {89        try await get("api/villes", query: ["min": String(min), "region": region])90    }9192    func city(_ slug: String) async throws -> CityPage {93        try await get("api/villes/\(Fmt.pathComponent(slug))")94    }9596    func regions() async throws -> RegionsResponse { try await get("api/regions") }9798    func stats() async throws -> Stats { try await get("api/stats") }99100    func marketTable(make: String? = nil, model: String? = nil, min: Int = 5) async throws -> MarketTable {101        try await get("api/stats/marche", query: ["make": make, "model": model, "min": String(min)])102    }103104    func depreciation(make: String? = nil, model: String? = nil) async throws -> Depreciation {105        try await get("api/stats/depreciation", query: ["make": make, "model": model])106    }107108    func priceDrops(days: Int = 30, limit: Int = 24) async throws -> PriceDrops {109        try await get("api/stats/baisses", query: ["days": String(days), "limit": String(limit)])110    }111112    func electrics() async throws -> Electrics { try await get("api/stats/electriques") }113114    func saleDelays(make: String? = nil, days: Int = 90) async throws -> SaleDelays {115        try await get("api/stats/delais", query: ["make": make, "days": String(days)])116    }117118    /// Tableau de bord par période : `auj`, `7j`, `30j`, `3m`, `6m`, `12m`, `annee`, `tout`.119    func dashboard(period: String = "30j") async throws -> Dashboard {120        try await get("api/stats/dashboard", query: ["period": period])121    }122123    func version() async throws -> APIVersion { try await get("api/version") }124}125
Files main
Menu

Theme