Files
road-monitoring-ui/src/services/api/video.service.ts

118 lines
3.1 KiB
TypeScript

import axiosClient from '../axios/axios';
import { API_ROUTES } from '@/constants/apiRoutes';
import {
CompletedVideoResult,
PaginationParams,
SseTokenResponse,
UploadListResponse,
VideoAnnotationFramesParams,
VideoAnnotationFramesResponse,
VideoDetectionsParams,
VideoDetectionsResponse,
} from '@/types';
/**
* Video Service
*/
export const videoService = {
getUploads: async (
params?: PaginationParams,
): Promise<UploadListResponse> => {
const response = await axiosClient.get<UploadListResponse>(
API_ROUTES.VIDEOS.UPLOADS,
{
params: {
skip: params?.skip ?? 0,
limit: params?.limit ?? 20,
},
},
);
return response.data;
},
createUploadEventsToken: async (): Promise<SseTokenResponse> => {
const response = await axiosClient.post<SseTokenResponse>(
API_ROUTES.VIDEOS.UPLOAD_EVENTS_TOKEN,
);
return response.data;
},
/**
* Upload a video for processing
*/
uploadVideo: async (formData: FormData): Promise<any> => {
const response = await axiosClient.post(
API_ROUTES.VIDEOS.UPLOAD,
formData,
{
headers: {
'Content-Type': 'multipart/form-data',
},
},
);
return response.data;
},
/**
* Get analysis results for a video
*/
getVideoResults: async (videoId: string): Promise<CompletedVideoResult> => {
const response = await axiosClient.get<CompletedVideoResult>(
API_ROUTES.VIDEOS.RESULTS(videoId),
);
return response.data;
},
getVideoDetections: async (
videoId: string,
params?: VideoDetectionsParams,
): Promise<VideoDetectionsResponse> => {
const response = await axiosClient.get<VideoDetectionsResponse>(
API_ROUTES.VIDEOS.DETECTIONS(videoId),
{
params: {
skip: params?.skip ?? 0,
limit: params?.limit ?? 1,
class_name: params?.class_name,
proof_status: params?.proof_status,
min_confidence: params?.min_confidence,
sort: params?.sort ?? 'timestamp_asc',
search: params?.search,
},
paramsSerializer: {
indexes: null,
},
},
);
return response.data;
},
getVideoAnnotationFrames: async (
videoId: string,
params: VideoAnnotationFramesParams,
): Promise<VideoAnnotationFramesResponse> => {
const response = await axiosClient.get<VideoAnnotationFramesResponse>(
API_ROUTES.VIDEOS.ANNOTATION_FRAMES(videoId),
{
params,
},
);
return response.data;
},
/**
* Fetch a protected video as a blob through the authenticated axios client.
*
* The browser's native `<video src>` request cannot carry the Bearer token,
* which makes the protected media endpoint respond with 401. Proxying the
* download through axios attaches the auth header (and benefits from the
* refresh-on-401 interceptor) so the bytes can be played back locally.
*/
getProtectedVideo: async (url: string): Promise<Blob> => {
const response = await axiosClient.get<Blob>(url, {
responseType: 'blob',
});
return response.data;
},
};