refactor: switch from Axios for Fetch API (#840)

* refactor: switch ExternalAPI to Fetch API

* fix: add missing auth token in Plex request

* fix: send proper URL params

* ci: try to fix format checker

* ci: ci: try to fix format checker

* ci: try to fix format checker

* refactor: make tautulli use the ExternalAPI class

* refactor: add rate limit to fetch api

* refactor: add rate limit to fetch api

* refactor: switch server from axios to fetch api

* refactor: switch frontend from axios to fetch api

* fix: switch from URL objects to strings

* fix: use the right search params for ExternalAPI

* fix: better log for ExternalAPI errors

* feat: add retry to external API requests

* fix: try to fix network errors with IPv6

* fix: imageProxy rate limit

* revert: remove retry to external API requests

* feat: set IPv4 first as an option

* fix(jellyfinapi): add missing argument in JellyfinAPI constructor

* refactor: clean the rate limit utility
This commit is contained in:
Gauthier
2024-07-14 19:04:36 +02:00
committed by GitHub
parent ae955e9e7c
commit b36bb3fa58
100 changed files with 5380 additions and 10870 deletions

View File

@@ -1,6 +1,3 @@
import type { AxiosError, AxiosResponse } from 'axios';
import axios from 'axios';
interface JellyfinAuthenticationResult {
Id: string;
AccessToken: string;
@@ -18,30 +15,34 @@ class JellyAPI {
resolve: (result: JellyfinAuthenticationResult) => void,
reject: (e: Error) => void
) => {
axios
.post(
Hostname + '/Users/AuthenticateByName',
{
Username: Username,
Pw: Password,
},
{
headers: {
'X-Emby-Authorization':
'MediaBrowser Client="Jellyfin Web", Device="Firefox", DeviceId="TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NDsgcnY6ODUuMCkgR2Vja28vMjAxMDAxMDEgRmlyZWZveC84NS4wfDE2MTI5MjcyMDM5NzM1", Version="10.8.0"',
},
fetch(Hostname + '/Users/AuthenticateByName', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Emby-Authorization':
'MediaBrowser Client="Jellyfin Web", Device="Firefox", DeviceId="TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NDsgcnY6ODUuMCkgR2Vja28vMjAxMDAxMDEgRmlyZWZveC84NS4wfDE2MTI5MjcyMDM5NzM1", Version="10.8.0"',
},
body: JSON.stringify({
Username: Username,
Pw: Password,
}),
})
.then((res) => {
if (!res.ok) {
throw new Error('Network response was not ok');
}
)
.then((resp: AxiosResponse) => {
return res.json();
})
.then((data) => {
const response: JellyfinAuthenticationResult = {
Id: resp.data.User.Id,
AccessToken: resp.data.AccessToken,
ServerId: resp.data.ServerId,
Id: data.User.Id,
AccessToken: data.AccessToken,
ServerId: data.ServerId,
};
resolve(response);
})
.catch((e: AxiosError) => {
reject(e);
.catch((error) => {
reject(error);
});
}
);

View File

@@ -1,4 +1,3 @@
import axios from 'axios';
import Bowser from 'bowser';
interface PlexHeaders extends Record<string, string> {
@@ -78,13 +77,14 @@ class PlexOAuth {
'You must initialize the plex headers clientside to login'
);
}
const response = await axios.post(
'https://plex.tv/api/v2/pins?strong=true',
undefined,
{ headers: this.plexHeaders }
);
const res = await fetch('https://plex.tv/api/v2/pins?strong=true', {
method: 'POST',
headers: this.plexHeaders,
});
if (!res.ok) throw new Error();
const data = await res.json();
this.pin = { id: response.data.id, code: response.data.code };
this.pin = { id: data.id, code: data.code };
return this.pin;
}
@@ -136,16 +136,17 @@ class PlexOAuth {
throw new Error('Unable to poll when pin is not initialized.');
}
const response = await axios.get(
`https://plex.tv/api/v2/pins/${this.pin.id}`,
{ headers: this.plexHeaders }
);
const res = await fetch(`https://plex.tv/api/v2/pins/${this.pin.id}`, {
headers: this.plexHeaders,
});
if (!res.ok) throw new Error();
const data = await res.json();
if (response.data?.authToken) {
this.authToken = response.data.authToken as string;
if (data?.authToken) {
this.authToken = data.authToken as string;
this.closePopup();
resolve(this.authToken);
} else if (!response.data?.authToken && !this.popup?.closed) {
} else if (!data?.authToken && !this.popup?.closed) {
setTimeout(executePoll, 1000, resolve, reject);
} else {
reject(new Error('Popup closed without completing login'));