peertube-plugin-auto-import.../lib/peertubeRequester.ts

126 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2021-07-09 14:26:23 +02:00
// import { ImplementableApi } from './implementableApi';
// Api request lib
import fetch, { FetchError, Headers } from "node-fetch";
2021-07-12 17:02:39 +02:00
import { URL, URLSearchParams } from "url";
2021-07-09 14:26:23 +02:00
import FormData from "form-data";
// import dedent from "ts-dedent";
namespace PeerTubeRequester {
export type Config = {
2021-07-12 17:02:39 +02:00
domain_name: string | URL;
2021-07-09 14:26:23 +02:00
username: string;
password: string;
};
}
type UploadInstruction = {
[key: string]: string;
channelId: string;
targetUrl: string;
};
type ClientToken = {
client_id: string;
client_secret: string;
grant_type: "password";
response_type: "code";
username: string;
password: string;
};
type UserToken = {
access_token: string;
token_type: string;
expires_in: string;
refresh_token: string;
};
class PeerTubeRequester {
2021-07-12 17:02:39 +02:00
readonly domain_name: URL;
2021-07-09 14:26:23 +02:00
readonly username: string;
readonly password: string;
constructor(readonly config: PeerTubeRequester.Config) {
2021-07-12 17:02:39 +02:00
this.domain_name = new URL("/", config.domain_name);
2021-07-09 14:26:23 +02:00
this.username = config.username;
this.password = config.password;
}
async apiRequest(message: UploadInstruction): Promise<void> {
let response = await fetch(
2021-07-12 17:02:39 +02:00
new URL(`/api/v1/oauth-clients/local`, this.domain_name)
2021-07-09 14:26:23 +02:00
);
if (!response.ok) {
throw new Error(response.statusText); // CRASH
}
const { client_id, client_secret } = await response.json();
const client_info: { [key: string]: string } = {
client_id,
client_secret,
grant_type: "password",
response_type: "code",
username: this.username,
password: this.password,
};
let myParams = new URLSearchParams();
for (const key in client_info) myParams.append(key, client_info[key]);
2021-07-12 17:02:39 +02:00
response = await fetch(new URL(`/api/v1/users/token`, this.domain_name), {
2021-07-09 14:26:23 +02:00
method: "post",
body: myParams,
});
if (!response.ok) {
throw new Error(response.statusText); // CRASH
}
const { access_token } = await response.json();
// Upload
const myUploadForm = new URLSearchParams();
const myHeader = new Headers();
myHeader.append("Authorization", `Bearer ${access_token}`);
for (const key in message) myUploadForm.append(key, message[key]);
2021-07-12 17:02:39 +02:00
response = await fetch(
new URL(`/api/v1/videos/imports`, this.domain_name),
{
method: "post",
headers: myHeader,
body: myUploadForm,
}
);
2021-07-09 14:26:23 +02:00
if (!response.ok) {
switch (response.status) {
case 400:
throw new Error(
`Your target URL was not accepted by the API.\
Actualy it's : ${message.targetUrl}
${response.statusText}`
);
break;
case 403:
throw new Error(response.statusText);
break;
case 409:
throw new Error(
`Oops, your instance had not allowed the HTTPS import.\
Contact your administrator.
${response.statusText}`
);
break;
default:
throw new Error(
`Oh, you resolved an undocumented issues.\
Please report this on the git if you have the time.
ERROR: ${response.statusText}`
);
break;
}
}
}
}
export { PeerTubeRequester };