SPB Git

Repository Public

Endpoints.swift 4.0 KB 88 lines swift

1//2//  Food-Ka iOS v23//  File: FoodKa/Core/API/Endpoints.swift4//  Description: les endpoints de l'API Food-Ka v2 utilisés par l'app (contrat : docs/API.md du dépôt food-ka-v2).5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//910import Foundation1112extension APIClient {13    /// Identité et règles du service ; les règles sont gardées pour le hors-ligne.14    func version() async throws -> ServiceInfo {15        let v: ServiceInfo = try await get("api/version")16        ServiceInfo.store(v)17        return v18    }1920    /// Recherche unifiée (tolérante aux fautes, filtres, tris).21    func search(_ f: SearchFilters, page: Int = 1, pageSize: Int = 30, facets: Bool = false) async throws -> Fetched<SearchResponse> {22        try await fetch("api/search", query: f.query(page: page, pageSize: pageSize, facets: facets))23    }2425    /// Autocomplétion, dès 2 caractères : produits, marques, rayons, enseignes.26    func suggest(_ q: String) async throws -> Suggestions {27        try await get("api/suggest", query: ["q": q.trimmingCharacters(in: .whitespacesAndNewlines)])28    }2930    /// Recherche par code-barres (UPC-A ou EAN-13). Peu de produits portent un code : `total` vaut souvent 0.31    func barcode(_ code: String) async throws -> SearchResponse {32        try await get("api/barcode/" + APIClient.segment(code))33    }3435    func categories() async throws -> Fetched<CategoriesResponse> { try await fetch("api/categories") }3637    func facets(_ f: SearchFilters) async throws -> Facets {38        var q = f.query(page: 1)39        q["page"] = nil; q["page_size"] = nil; q["sort"] = nil40        return try await get("api/facets", query: q)41    }4243    /// Fiche complète d'un produit.44    func product(_ id: String) async throws -> Fetched<ProductDetail> {45        try await fetch("api/products/" + APIClient.segment(id))46    }4748    /// Lot léger de produits (100 identifiants au plus) : prix frais des produits suivis et de la liste.49    func products(ids: [String]) async throws -> ProductBatch {50        try await get("api/products", query: ["ids": ids.prefix(100).joined(separator: ",")])51    }5253    /// Soldes par statut (`verifiee` par défaut) ; ordre diversifié par enseigne, tel que le service le rend.54    func deals(status: String? = nil, store: String? = nil, category: String? = nil, kind: String? = nil, page: Int = 1, pageSize: Int = 30) async throws -> Fetched<DealsResponse> {55        try await fetch("api/deals", query: ["status": status, "store": store, "category": category, "kind": kind, "page": String(page), "page_size": String(pageSize)])56    }5758    func flyers() async throws -> Fetched<FlyersResponse> { try await fetch("api/circulaires") }5960    /// Circulaire en cours d'une enseigne, avec tous ses rabais.61    func flyer(_ store: String) async throws -> Fetched<Flyer> {62        try await fetch("api/circulaires/" + APIClient.segment(store))63    }6465    func stores() async throws -> Fetched<StoresResponse> { try await fetch("api/enseignes") }6667    func store(_ id: String) async throws -> Fetched<Store> {68        try await fetch("api/enseignes/" + APIClient.segment(id))69    }7071    func market() async throws -> Fetched<MarketStats> { try await fetch("api/marche") }7273    func marketIndex() async throws -> Fetched<MarketIndex> { try await fetch("api/marche/indice") }7475    func movements(days: Int = 30, limit: Int = 20) async throws -> Fetched<MarketMovements> {76        try await fetch("api/marche/mouvements", query: ["days": String(days), "limit": String(limit)])77    }7879    /// Coût d'une liste par enseigne. La liste est envoyée le temps du calcul ; le serveur n'en garde rien.80    func compareBasket(_ request: BasketRequest) async throws -> (BasketComparison, Data) {81        let body = try JSONEncoder().encode(request)82        let data = try await postRaw("api/panier/comparer", body: body)83        do { return (try JSONDecoder().decode(BasketComparison.self, from: data), data) } catch {84            throw APIError.decoding(String(describing: error).prefix(300).description)85        }86    }87}88
Files main
Menu

Theme