Refactor PeertubeAPI usage
This commit is contained in:
parent
bf87557b84
commit
5c35ce031d
|
@ -21,6 +21,7 @@ import { MessageKind, RedirectType } from './types';
|
|||
import constants from './constants';
|
||||
import Preferences from './preferences';
|
||||
import InvidiousAPI from './invidious-api';
|
||||
import PeertubeAPI from './peertube-api';
|
||||
import { getPeertubeVideoURL } from './util';
|
||||
|
||||
/**
|
||||
|
@ -53,13 +54,8 @@ const redirectYoutube = async (r) => {
|
|||
throw new Error('No results.');
|
||||
};
|
||||
|
||||
const buildSearchByNameURL = (instance: string, query: string): string => `https://${instance}/api/v1/search/videos?search=${encodeURIComponent(query)}`;
|
||||
|
||||
const searchByName = query => new Promise(async (resolve, reject) => {
|
||||
const instance = _.getOr(constants.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString();
|
||||
|
||||
fetch(buildSearchByNameURL(instance, query))
|
||||
.then(res => res.json())
|
||||
PeertubeAPI.searchVideo({ search: query })
|
||||
.then(function(data) {
|
||||
if (data.total > 0) {
|
||||
const video = data.data[0]
|
||||
|
@ -71,15 +67,12 @@ const searchByName = query => new Promise(async (resolve, reject) => {
|
|||
});
|
||||
});
|
||||
|
||||
const buildSearchByIDURL = (instance: string, id: string): string => `https://${instance}/api/v1/videos/${id}`;
|
||||
|
||||
const searchByID = id => new Promise(async (resolve, reject) => {
|
||||
const instance = _.getOr(constants.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString();
|
||||
const prefs = await Preferences.getPreferences();
|
||||
|
||||
fetch(buildSearchByIDURL(instance, id))
|
||||
.then(res => res.json())
|
||||
PeertubeAPI.getVideo(id)
|
||||
.then(function(video) {
|
||||
resolve({ url: `https://${instance}/videos/watch/${id}`, video });
|
||||
resolve({ url: getPeertubeVideoURL(video, prefs), video });
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
export default {
|
||||
defaultInstance: 'peertube.social',
|
||||
peertubeAPI: {
|
||||
defaultInstance: 'peertube.social',
|
||||
endpoint: 'api/v1',
|
||||
},
|
||||
invidiousAPI: {
|
||||
url: 'https://invidio.us/api/v1',
|
||||
videos: 'videos',
|
||||
|
|
|
@ -12,7 +12,7 @@ export default class InvidiousAPI {
|
|||
return fetch(`${constants.invidiousAPI.url}/${action}/${paramString}`)
|
||||
.then(res => res.json())
|
||||
.catch(e => console.error(
|
||||
'An error occured while trying to fetch API used by PeerTubeify: '
|
||||
'An error occured while trying to fetch Invidious API used by PeerTubeify: '
|
||||
+ e.message
|
||||
));
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
import * as _ from 'lodash/fp';
|
||||
import constants from './constants';
|
||||
|
||||
export default class PeertubeAPI {
|
||||
private static async fetchAPI(path: string, query?: Object) {
|
||||
const instance = _.getOr(constants.peertubeAPI.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString();
|
||||
|
||||
let url = `https://${constants.peertubeAPI.defaultInstance}/${constants.peertubeAPI.endpoint}/${path}`;
|
||||
|
||||
if (query) {
|
||||
url = url + '?' + Object.keys(query).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key])).join('&');
|
||||
}
|
||||
|
||||
return fetch(url)
|
||||
.then(res => res.json())
|
||||
.catch(e => console.error(
|
||||
`An error occured while trying to fetch ${instance} API used by PeerTubeify: `
|
||||
+ e.message
|
||||
));
|
||||
}
|
||||
|
||||
static async getVideo(id: string) {
|
||||
return this.fetchAPI('videos/' + id);
|
||||
}
|
||||
|
||||
static async searchVideo(query: Object) {
|
||||
return this.fetchAPI('search/videos', query);
|
||||
}
|
||||
}
|
|
@ -28,7 +28,7 @@ export default class Preferences {
|
|||
redirectPeertube: RedirectType;
|
||||
|
||||
constructor(localStorage) {
|
||||
this.searchInstance = _.defaultTo(constants.defaultInstance, localStorage.searchInstance as string);
|
||||
this.searchInstance = _.defaultTo(constants.peertubeAPI.defaultInstance, localStorage.searchInstance as string);
|
||||
this.openInOriginalInstance = _.defaultTo(true, localStorage.openInOriginalInstance as boolean);
|
||||
this.redirectYoutube = _.defaultTo(RedirectType.Show, localStorage.redirectYoutube)
|
||||
this.redirectPeertube = _.defaultTo(RedirectType.None, localStorage.redirectPeertube)
|
||||
|
@ -54,6 +54,6 @@ export default class Preferences {
|
|||
}
|
||||
|
||||
set searchInstance(instance) {
|
||||
this._searchInstance = _.isEmpty(instance) ? constants.defaultInstance : stripProtocol(instance)
|
||||
this._searchInstance = _.isEmpty(instance) ? constants.peertubeAPI.defaultInstance : stripProtocol(instance)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue