From 9905f13ddc250fdbad88c1a10ad18435112ab7a7 Mon Sep 17 00:00:00 2001 From: Thibaud Moustier Date: Fri, 27 Feb 2026 22:34:00 +0100 Subject: [PATCH] "Mobile : Mis en place fichier api (toujours en mocks pour le moment)" --- Wallette/mobile/src/config/api.ts | 41 +++++++++++++++++++ Wallette/mobile/src/config/env.ts | 40 ++++++++++++++++-- Wallette/mobile/src/services/api/alertsApi.ts | 21 ++++++++++ Wallette/mobile/src/services/api/authApi.ts | 0 Wallette/mobile/src/services/api/priceApi.ts | 15 +++++++ Wallette/mobile/src/services/api/signalApi.ts | 15 +++++++ .../mobile/src/services/api/strategyApi.ts | 17 ++++++++ 7 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 Wallette/mobile/src/config/api.ts create mode 100644 Wallette/mobile/src/services/api/alertsApi.ts create mode 100644 Wallette/mobile/src/services/api/authApi.ts create mode 100644 Wallette/mobile/src/services/api/priceApi.ts create mode 100644 Wallette/mobile/src/services/api/signalApi.ts create mode 100644 Wallette/mobile/src/services/api/strategyApi.ts diff --git a/Wallette/mobile/src/config/api.ts b/Wallette/mobile/src/config/api.ts new file mode 100644 index 0000000..19f8d94 --- /dev/null +++ b/Wallette/mobile/src/config/api.ts @@ -0,0 +1,41 @@ +import { Platform } from "react-native"; + +/** + * API Gateway (microservices) + * --------------------------- + * - Téléphone physique : utiliser l'IP du PC sur le réseau local + * - Android émulateur : 10.0.2.2 + * - iOS simulateur : localhost (souvent OK) + * + * IMPORTANT : le mobile doit appeler UNIQUEMENT le gateway: + * http://:3000/api/... + * Socket.IO passe aussi via le gateway: + * http://:3000 (proxy /socket.io/*) + */ + +// ✅ Change uniquement cette valeur pour ton PC +const DEV_LAN_IP = "192.168.129.121"; + +export function getGatewayHost(): string { + if (__DEV__) { + // Téléphone physique / LAN + return DEV_LAN_IP; + } + // En prod (ou build), tu mettras un vrai domaine/IP si nécessaire + return DEV_LAN_IP; +} + +export function getGatewayBaseUrl(): string { + const host = + Platform.OS === "android" && !__DEV__ + ? "10.0.2.2" + : getGatewayHost(); + + return `http://${host}:3000`; +} + +// REST base +export const API_BASE_URL = `${getGatewayBaseUrl()}/api`; + +// Socket base (pas /api) +export const SOCKET_BASE_URL = `${getGatewayBaseUrl()}`; \ No newline at end of file diff --git a/Wallette/mobile/src/config/env.ts b/Wallette/mobile/src/config/env.ts index 36a9d9a..e394a9b 100644 --- a/Wallette/mobile/src/config/env.ts +++ b/Wallette/mobile/src/config/env.ts @@ -1,3 +1,37 @@ -// ⚠️ En dev téléphone réel : URL = IP du PC sur le même réseau -export const SERVER_URL = "http://192.168.129.121:3000"; -export const WS_URL = "ws://192.168.129.121:3000"; // optionnel, Socket.IO peut aussi utiliser http(s) \ No newline at end of file +import { Platform } from "react-native"; + +/** + * env.ts + * ------ + * Objectif : centraliser les URLs réseau pour le mobile. + * + * IMPORTANT (microservices + gateway) : + * - Le mobile parle UNIQUEMENT au gateway : http://:3000 + * - REST = http://:3000/api/... + * - Socket.IO = http://:3000 (proxy /socket.io/* via gateway) + * + * Téléphone physique : + * - = IP du PC sur le Wi-Fi (ex: 192.168.x.x) + * + * Émulateurs (si un jour) : + * - Android: 10.0.2.2 + * - iOS: localhost + */ + +const DEV_LAN_IP = "192.168.129.121"; + +function resolveHost(): string { + // On part sur téléphone physique (ton cas). + // Si un jour tu testes sur émulateur Android, tu peux mettre une condition: + // if (Platform.OS === "android" && __DEV__ && isEmulator) return "10.0.2.2"; + return DEV_LAN_IP; +} + +// Base gateway (HTTP) +export const GATEWAY_BASE_URL = `http://${resolveHost()}:3000`; + +// REST (via gateway) +export const API_BASE_URL = `${GATEWAY_BASE_URL}/api`; + +// Socket.IO (via gateway) +export const SERVER_URL = GATEWAY_BASE_URL; \ No newline at end of file diff --git a/Wallette/mobile/src/services/api/alertsApi.ts b/Wallette/mobile/src/services/api/alertsApi.ts new file mode 100644 index 0000000..3c9e7d2 --- /dev/null +++ b/Wallette/mobile/src/services/api/alertsApi.ts @@ -0,0 +1,21 @@ +import type { Alert } from "../../types/Alert"; +import { alertStore } from "../alertStore"; + +/** + * alertsApi + * --------- + * Contrat futur (gateway): + * - GET /api/alerts/events?userId=...&limit=10 + * - POST /api/alerts + * - POST /api/alerts/:id/toggle + * + * Pour l'instant : on lit ce qu'on a en local (alertStore alimenté par Socket). + */ +export async function getRecentAlerts(limit = 10): Promise { + const all = alertStore.getAll?.() ?? []; + return all.slice(0, limit); +} + +export async function clearLocalAlerts(): Promise { + alertStore.clear?.(); +} \ No newline at end of file diff --git a/Wallette/mobile/src/services/api/authApi.ts b/Wallette/mobile/src/services/api/authApi.ts new file mode 100644 index 0000000..e69de29 diff --git a/Wallette/mobile/src/services/api/priceApi.ts b/Wallette/mobile/src/services/api/priceApi.ts new file mode 100644 index 0000000..224660d --- /dev/null +++ b/Wallette/mobile/src/services/api/priceApi.ts @@ -0,0 +1,15 @@ +import type { DashboardSummary } from "../../types/DashboardSummary"; +import { fetchDashboardSummary } from "../dashboardService"; + +/** + * priceApi + * -------- + * Contrat futur (gateway): + * - GET /api/price/current?pair=BTC/EUR + * + * Pour l'instant : on renvoie la donnée mock déjà utilisée par le Dashboard. + */ +export async function getCurrentPriceForDashboard(): Promise> { + const d = await fetchDashboardSummary(); + return { pair: d.pair, price: d.price, timestamp: d.timestamp }; +} \ No newline at end of file diff --git a/Wallette/mobile/src/services/api/signalApi.ts b/Wallette/mobile/src/services/api/signalApi.ts new file mode 100644 index 0000000..8a2d075 --- /dev/null +++ b/Wallette/mobile/src/services/api/signalApi.ts @@ -0,0 +1,15 @@ +import type { Signal } from "../../types/Signal"; +import { fetchRecentSignals } from "../signalService"; + +/** + * signalApi + * --------- + * Contrat futur (gateway): + * - GET /api/signal/current?userId=...&pair=BTC/EUR + * - (option) GET /api/signal/recent?userId=...&limit=20 + * + * Pour l'instant : on utilise les mocks existants. + */ +export async function getRecentSignals(limit = 20): Promise { + return await fetchRecentSignals(limit); +} \ No newline at end of file diff --git a/Wallette/mobile/src/services/api/strategyApi.ts b/Wallette/mobile/src/services/api/strategyApi.ts new file mode 100644 index 0000000..5f5b366 --- /dev/null +++ b/Wallette/mobile/src/services/api/strategyApi.ts @@ -0,0 +1,17 @@ +import { loadSettings, saveSettings } from "../../utils/settingsStorage"; +import type { UserSettings } from "../../models/UserSettings"; + +/** + * strategyApi + * ----------- + * Contrat futur (gateway): + * - POST /api/strategy/select { userId, pair, strategyKey, params... } + * + * Pour l'instant : on sauvegarde localement dans settingsStorage. + */ +export async function selectStrategy(strategyKey: string): Promise { + const s = await loadSettings(); + const next: UserSettings = { ...s, selectedStrategyKey: strategyKey }; + await saveSettings(next); + return next; +} \ No newline at end of file -- 2.50.1