This commit is contained in:
simojenki
2022-04-23 16:54:30 +10:00
parent 730524d7a1
commit eb66393fe6
4 changed files with 20 additions and 76 deletions

View File

@@ -1,19 +1,17 @@
import { AxiosPromise, AxiosRequestConfig, ResponseType } from "axios";
import _ from "underscore";
export interface RequestModifier {
(config: AxiosRequestConfig): AxiosRequestConfig;
}
export const no_op = (config: AxiosRequestConfig) => config;
export interface Http {
(config: AxiosRequestConfig): AxiosPromise<any>;
}
export type RequestParams = { baseURL: string; url: string, params: any, headers: any, responseType: ResponseType }
export type RequestParams = {
baseURL: string;
url: string;
params: any;
headers: any;
responseType: ResponseType;
};
// todo: rename to http
export const http =
(base: Http, defaults: Partial<RequestParams>): Http =>
(config: AxiosRequestConfig) => {

View File

@@ -101,7 +101,7 @@ export class Subsonic implements MusicService {
// todo: why is this in here?
externalImageFetcher: ImageFetcher;
base: Http;
subsonicHttp: Http;
constructor(
url: string,
@@ -111,15 +111,15 @@ export class Subsonic implements MusicService {
this.url = url;
this.streamClientApplication = streamClientApplication;
this.externalImageFetcher = externalImageFetcher;
this.base = http2(axios, {
this.subsonicHttp = http2(axios, {
baseURL: this.url,
params: { v: "1.16.1", c: DEFAULT_CLIENT_APPLICATION },
headers: { "User-Agent": "bonob" },
});
}
authenticated = (credentials: Credentials, wrap: Http = this.base) =>
http2(wrap, {
authenticatedSubsonicHttp = (credentials: Credentials) =>
http2(this.subsonicHttp, {
params: {
u: credentials.username,
...t_and_s(credentials.password),
@@ -130,12 +130,12 @@ export class Subsonic implements MusicService {
credentials: Credentials,
url: string,
params: {} = {}
): Promise<T> => getJSON2(http2(this.authenticated(credentials), { url, params }));
): Promise<T> => getJSON2(http2(this.authenticatedSubsonicHttp(credentials), { url, params }));
generateToken = (credentials: Credentials) =>
pipe(
TE.tryCatch(
() => getJSON2<PingResponse>(http2(this.authenticated(credentials), { url: "/rest/ping.view" })),
() => getJSON2<PingResponse>(http2(this.authenticatedSubsonicHttp(credentials), { url: "/rest/ping.view" })),
(e) => new AuthFailure(e as string)
),
TE.chain(({ type }) =>
@@ -170,7 +170,7 @@ export class Subsonic implements MusicService {
): Promise<SubsonicMusicLibrary> => {
const subsonicGenericLibrary = new SubsonicGenericMusicLibrary(
this.streamClientApplication,
this.authenticated(credentials, this.base)
this.authenticatedSubsonicHttp(credentials)
);
if (credentials.type == "navidrome") {
return Promise.resolve(

View File

@@ -277,19 +277,19 @@ const maybeAsGenre = (genreName: string | undefined): Genre | undefined =>
export class SubsonicGenericMusicLibrary implements SubsonicMusicLibrary {
streamClientApplication: StreamClientApplication;
http: Http;
subsonicHttp: Http;
constructor(
streamClientApplication: StreamClientApplication,
http: Http
subsonicHttp: Http
) {
this.streamClientApplication = streamClientApplication;
this.http = http;
this.subsonicHttp = subsonicHttp;
}
GET = (query: Partial<RequestParams>) => ({
asRAW: () => getRaw2(http2(this.http, query)),
asJSON: <T>() => getJSON2<T>(http2(this.http, query)),
asRAW: () => getRaw2(http2(this.subsonicHttp, query)),
asJSON: <T>() => getJSON2<T>(http2(this.subsonicHttp, query)),
});
flavour = () => "subsonic";

View File

@@ -1,45 +1,9 @@
import axios, { AxiosPromise, AxiosRequestConfig } from "axios";
import {
DEFAULT_CLIENT_APPLICATION,
isError,
SubsonicEnvelope,
t_and_s,
USER_AGENT,
} from ".";
// todo: rename http2 to http
import { Http, http as http2 } from "../http";
import { Credentials } from "../music_service";
import { asURLSearchParams } from "../utils";
export const http = (base: string, credentials: Credentials): HTTP => ({
get: async (
path: string,
params: Partial<{ q: {}; config: AxiosRequestConfig | undefined }>
) =>
axios
.get(`${base}${path}`, {
params: asURLSearchParams({
u: credentials.username,
v: "1.16.1",
c: DEFAULT_CLIENT_APPLICATION,
...t_and_s(credentials.password),
f: "json",
...(params.q || {}),
}),
headers: {
"User-Agent": USER_AGENT,
},
...(params.config || {}),
})
.catch((e) => {
throw `Subsonic failed with: ${e}`;
})
.then((response) => {
if (response.status != 200 && response.status != 206) {
throw `Subsonic failed with a ${response.status || "no!"} status`;
} else return response;
}),
});
export type HttpResponse = {
data: any;
@@ -47,24 +11,6 @@ export type HttpResponse = {
headers: any;
};
export interface HTTP {
get(
path: string,
params: Partial<{ q: {}; config: AxiosRequestConfig | undefined }>
): Promise<HttpResponse>;
}
export const raw = (response: AxiosPromise<any>) =>
response
.catch((e) => {
throw `Subsonic failed with: ${e}`;
})
.then((response) => {
if (response.status != 200 && response.status != 206) {
throw `Subsonic failed with a ${response.status || "no!"} status`;
} else return response;
});
export const getRaw2 = (http: Http) =>
http({ method: "get" })
.catch((e) => {
@@ -88,4 +34,4 @@ export const asJSON = <T>(response: HttpResponse): T => {
else return subsonicResponse as unknown as T;
};
export default http;