Various improvements #3

Merged
amaury.joly merged 16 commits from various-improvements into master 2021-07-27 18:39:49 +02:00
Showing only changes of commit 51126e7f0f - Show all commits

View File

@ -1,9 +1,6 @@
// import { ImplementableApi } from './implementableApi';
// Api request lib
import fetch, { FetchError, Headers } from "node-fetch";
import { URL, URLSearchParams } from "url";
import FormData from "form-data";
// import dedent from "ts-dedent";
namespace PeerTubeRequester {
export type Config = {
@ -19,22 +16,6 @@ type UploadInstruction = {
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 {
readonly domain_name: URL;
readonly username: string;
@ -46,7 +27,7 @@ class PeerTubeRequester {
this.password = config.password;
}
async apiRequest(message: UploadInstruction): Promise<void> {
private async requestAuthToken(): Promise<void> {
let response = await fetch(
new URL(`/api/v1/oauth-clients/local`, this.domain_name)
);
@ -74,15 +55,18 @@ class PeerTubeRequester {
if (!response.ok) {
throw new Error(response.statusText); // CRASH
}
const { access_token } = await response.json();
const { access_token: accessToken } = await response.json();
return accessToken;
}
// Upload
async uploadFromUrl(message: UploadInstruction): Promise<void> {
const accessToken = this.requestAuthToken();
const myUploadForm = new URLSearchParams();
const myHeader = new Headers();
myHeader.append("Authorization", `Bearer ${access_token}`);
myHeader.append("Authorization", `Bearer ${accessToken}`);
for (const key in message) myUploadForm.append(key, message[key]);
response = await fetch(
const response = await fetch(
new URL(`/api/v1/videos/imports`, this.domain_name),
{
method: "post",
@ -95,9 +79,9 @@ class PeerTubeRequester {
switch (response.status) {
case 400:
throw new Error(
`Your target URL was not accepted by the API.\
Actualy it's : ${message.targetUrl}
${response.statusText}`
`Bad or malformed request. Probably because your target URL (from Youtube?) was not accepted by the API.\
The target URL you attempted to pass: ${message.targetUrl}.
Response from the server: ${response.statusText}`
);
break;
case 403:
@ -105,15 +89,15 @@ class PeerTubeRequester {
break;
case 409:
throw new Error(
`Oops, your instance had not allowed the HTTPS import.\
`Oops, your instance did 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.
`Oh, you encountered an undocumented issues.\
Please create an issue to the plugin project.
ERROR: ${response.statusText}`
);
break;