// SPDX-License-Identifier: MIT #pragma once #include "openpu/http.hpp" #include "openpu/models.hpp" #include #include namespace openpu { /// Backend endpoints. Defaults are those of the Peugeot build of the official application; /// every field can be overridden (other brands, or a local mock server). struct ApiConfig { std::string devicesUrl = "https://api.groupe-psa.com/applications/majesticf/v1/devices/"; std::string updatesUrl = "https://api.groupe-psa.com/applications/majesticf/v1/getAvailableUpdate"; std::string clientId = "20a4cf7c-f5fb-41d5-9175-a6e23b9880e5"; /// Defaults overridden by OPENPU_DEVICES_URL, OPENPU_UPDATES_URL and OPENPU_CLIENT_ID when set. static ApiConfig fromEnvironment(); }; class ApiError : public std::runtime_error { public: enum class Kind { Network, ///< no HTTP response (offline, DNS, TLS, timeout) Server, ///< unexpected HTTP status BadResponse, ///< unparsable / unexpected body InvalidVin, ///< rejected locally before any request }; ApiError(Kind kind, long status, const std::string& what) : std::runtime_error(what), kind_(kind), status_(status) {} Kind kind() const noexcept { return kind_; } long status() const noexcept { return status_; } private: Kind kind_; long status_; }; class ApiClient { public: ApiClient(Transport& transport, ApiConfig config); /// Does the VIN exist, and which head unit does it have? /// A 404 on the devices endpoint yields `exists == false` (not an error). VehicleCheck checkVehicle(const std::string& vin); /// Latest firmware / map update for a vehicle whose type is already known. AvailableUpdates getUpdates(const std::string& vin, HeadUnitType type); /// Raw calls, exposed for diagnostics (openpu-cli --raw) and tests. HttpResponse rawDevices(const std::string& vin); HttpResponse rawUpdates(const std::string& vin, const std::vector& softwareTypes); const ApiConfig& config() const noexcept { return config_; } private: HttpResponse send(HttpRequest request); static void requireValidVin(const std::string& vin); Transport& transport_; ApiConfig config_; }; } // namespace openpu