@gotake/gotake-sdk
Version:
SDK for interacting with GoTake blockchain contracts
139 lines (138 loc) • 4.9 kB
JavaScript
/**
* Video API module for interacting with the backend API
*/
import { VideoStatus } from '../types.js';
import { StatusTracker } from '../utils/statusTracker.js';
/**
* Video API client for interacting with backend video services
*/
export class VideoApi {
/**
* Create VideoApi instance
* @param config API configuration
*/
constructor(config) {
this.config = config;
this.statusTracker = StatusTracker.getInstance();
}
/**
* Upload a video to the platform
* @param file Video file to upload
* @param metadata Video metadata
* @returns Promise resolving to video ID
*/
async uploadVideo(file, metadata) {
var _a;
const formData = new FormData();
formData.append('file', file);
if (metadata.title) {
formData.append('title', metadata.title);
}
if (metadata.description) {
formData.append('description', metadata.description);
}
if (metadata.tags) {
if (Array.isArray(metadata.tags)) {
formData.append('tags', metadata.tags.join(','));
}
else {
formData.append('tags', metadata.tags);
}
}
try {
const response = await fetch(`${this.config.endpoint}/videos/upload`, {
method: 'POST',
headers: this.config.headers,
body: formData
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `Upload failed with status ${response.status}`);
}
const data = await response.json();
// Update status tracker
this.statusTracker.updateStatus(data.id, {
id: data.id,
status: this.mapApiStatusToVideoStatus(data.status),
assetInfo: {
assetId: data.asset_id,
playbackUrl: (_a = data.livepeerResponse) === null || _a === void 0 ? void 0 : _a.url
}
});
return data.id;
}
catch (error) {
console.error('Error uploading video:', error);
throw error;
}
}
/**
* Get video information from the API
* @param videoId Video ID
* @returns Promise resolving to video status information
*/
async getVideoInfo(videoId) {
try {
const response = await fetch(`${this.config.endpoint}/videos/${videoId}`, {
method: 'GET',
headers: this.config.headers
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `Failed to get video info with status ${response.status}`);
}
const data = await response.json();
// Map API response to VideoStatusInfo
const statusInfo = {
id: data.id,
status: this.mapApiStatusToVideoStatus(data.status),
message: `Video is ${data.status}`,
assetInfo: {
assetId: data.asset_id,
playbackUrl: data.playback_url,
thumbnailUrl: data.thumbnail_url
},
updated_at: data.updated_at
};
// Update status tracker
this.statusTracker.updateStatus(videoId, statusInfo);
return statusInfo;
}
catch (error) {
console.error(`Error getting video info for ${videoId}:`, error);
// If we have cached status, return it with error flag
const cachedStatus = this.statusTracker.getStatus(videoId);
if (cachedStatus) {
return {
...cachedStatus,
status: VideoStatus.ERROR,
error: {
code: 'API_ERROR',
message: error instanceof Error ? error.message : 'Unknown error'
}
};
}
throw error;
}
}
/**
* Map API status string to VideoStatus enum
* @param apiStatus Status string from API
* @returns Mapped VideoStatus
*/
mapApiStatusToVideoStatus(apiStatus) {
switch (apiStatus.toLowerCase()) {
case 'uploading':
return VideoStatus.UPLOADING;
case 'processing':
return VideoStatus.PROCESSING;
case 'ready':
return VideoStatus.READY;
case 'error':
return VideoStatus.ERROR;
default:
console.warn(`Unknown API status: ${apiStatus}, defaulting to PROCESSING`);
return VideoStatus.PROCESSING;
}
}
}