REFACTOR external service and improve websocket training
This commit is contained in:
130
frontend/src/api/services/external.ts
Normal file
130
frontend/src/api/services/external.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
// frontend/src/api/services/external.ts
|
||||
/**
|
||||
* External Data API Service
|
||||
* Handles weather and traffic data operations
|
||||
*/
|
||||
|
||||
import { apiClient } from '../client';
|
||||
import type {
|
||||
CityInfoResponse,
|
||||
DataAvailabilityResponse,
|
||||
WeatherDataResponse,
|
||||
TrafficDataResponse,
|
||||
HistoricalWeatherRequest,
|
||||
HistoricalTrafficRequest,
|
||||
} from '../types/external';
|
||||
|
||||
class ExternalDataService {
|
||||
/**
|
||||
* List all supported cities
|
||||
*/
|
||||
async listCities(): Promise<CityInfoResponse[]> {
|
||||
const response = await apiClient.get<CityInfoResponse[]>(
|
||||
'/api/v1/external/cities'
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data availability for a specific city
|
||||
*/
|
||||
async getCityAvailability(cityId: string): Promise<DataAvailabilityResponse> {
|
||||
const response = await apiClient.get<DataAvailabilityResponse>(
|
||||
`/api/v1/external/operations/cities/${cityId}/availability`
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get historical weather data (optimized city-based endpoint)
|
||||
*/
|
||||
async getHistoricalWeatherOptimized(
|
||||
tenantId: string,
|
||||
params: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
start_date: string;
|
||||
end_date: string;
|
||||
}
|
||||
): Promise<WeatherDataResponse[]> {
|
||||
const response = await apiClient.get<WeatherDataResponse[]>(
|
||||
`/api/v1/tenants/${tenantId}/external/operations/historical-weather-optimized`,
|
||||
{ params }
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get historical traffic data (optimized city-based endpoint)
|
||||
*/
|
||||
async getHistoricalTrafficOptimized(
|
||||
tenantId: string,
|
||||
params: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
start_date: string;
|
||||
end_date: string;
|
||||
}
|
||||
): Promise<TrafficDataResponse[]> {
|
||||
const response = await apiClient.get<TrafficDataResponse[]>(
|
||||
`/api/v1/tenants/${tenantId}/external/operations/historical-traffic-optimized`,
|
||||
{ params }
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current weather for a location (real-time)
|
||||
*/
|
||||
async getCurrentWeather(
|
||||
tenantId: string,
|
||||
params: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
}
|
||||
): Promise<WeatherDataResponse> {
|
||||
const response = await apiClient.get<WeatherDataResponse>(
|
||||
`/api/v1/tenants/${tenantId}/external/operations/weather/current`,
|
||||
{ params }
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get weather forecast
|
||||
*/
|
||||
async getWeatherForecast(
|
||||
tenantId: string,
|
||||
params: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
days?: number;
|
||||
}
|
||||
): Promise<WeatherDataResponse[]> {
|
||||
const response = await apiClient.get<WeatherDataResponse[]>(
|
||||
`/api/v1/tenants/${tenantId}/external/operations/weather/forecast`,
|
||||
{ params }
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current traffic conditions (real-time)
|
||||
*/
|
||||
async getCurrentTraffic(
|
||||
tenantId: string,
|
||||
params: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
}
|
||||
): Promise<TrafficDataResponse> {
|
||||
const response = await apiClient.get<TrafficDataResponse>(
|
||||
`/api/v1/tenants/${tenantId}/external/operations/traffic/current`,
|
||||
{ params }
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
}
|
||||
|
||||
export const externalDataService = new ExternalDataService();
|
||||
export default externalDataService;
|
||||
Reference in New Issue
Block a user