-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiService.ts
More file actions
77 lines (62 loc) · 2.16 KB
/
Copy pathApiService.ts
File metadata and controls
77 lines (62 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import axios from 'axios';
import base64 from 'base-64';
import {Constants } from '../Constants/ApiKeys';
interface UploadResponse {
url: string;
uploadId: string;
}
class ApiService {
private static readonly BASE_URL = Constants.Base_Api_Url;
private static readonly ACCESS_TOKEN_ID = Constants.Access_Token_Id
private static readonly SECRET_KEY = Constants.Secret_Key;
// Generates the Basic Auth header string
private static getAuthHeader(): string {
const credentials = `${this.ACCESS_TOKEN_ID}:${this.SECRET_KEY}`;
const basicAuthCredential = base64.encode(credentials);
return `Basic ${basicAuthCredential}`;
}
// Creates a direct upload link with FastPix
public static async createDirectUpload(): Promise<UploadResponse | null> {
const url = `${this.BASE_URL}/upload`;
const parameters = {
corsOrigin: "*",
pushMediaSettings: {
accessPolicy: "public",
generateSubtitles: true,
normalizeAudio: true,
maxResolution: "1080p",
}
};
try {
const response = await axios.post(url, parameters, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': this.getAuthHeader(),
},
});
const dataDic = response.data?.data;
console.log(`response : ${dataDic}`);
const uploadId = dataDic?.uploadId;
const uploadUrl = dataDic?.url;
if (uploadId && uploadUrl) {
console.log(`got the upload details: ${uploadUrl} : ${uploadId}`);
return { url: uploadUrl, uploadId: uploadId };
}
console.log(`got nothing returning null : ${dataDic}`);
return null;
} catch (error: any) {
if (error.response) {
const statusCode = error.response.status;
const errorMessage = typeof error.response.data === 'string'
? error.response.data
: JSON.stringify(error.response.data);
console.error(`Upload POST failed: HTTP ${statusCode}:\n${errorMessage}`);
} else {
console.error("CreateUploadError:", error.message);
}
throw error;
}
}
}
export default ApiService;