2021-03-03 09:06:44 +00:00
|
|
|
import { gettext } from "django";
|
|
|
|
import { showMessage } from "../elements/messages/MessageContainer";
|
2021-01-16 18:08:20 +00:00
|
|
|
import { getCookie } from "../utils";
|
2020-12-16 22:02:43 +00:00
|
|
|
import { NotFoundError, RequestError } from "./Error";
|
2020-11-26 21:37:41 +00:00
|
|
|
|
|
|
|
export const VERSION = "v2beta";
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
export interface QueryArguments {
|
2021-03-02 14:12:26 +00:00
|
|
|
page?: number;
|
|
|
|
page_size?: number;
|
|
|
|
[key: string]: number | string | boolean | undefined | null;
|
2020-12-01 16:27:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 09:15:59 +00:00
|
|
|
export interface BaseInheritanceModel {
|
|
|
|
|
2021-02-09 09:22:49 +00:00
|
|
|
object_type: string;
|
|
|
|
|
2021-02-08 09:15:59 +00:00
|
|
|
verbose_name: string;
|
|
|
|
verbose_name_plural: string;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-26 21:37:41 +00:00
|
|
|
export class Client {
|
2020-12-01 16:27:19 +00:00
|
|
|
makeUrl(url: string[], query?: QueryArguments): string {
|
2020-11-29 11:01:06 +00:00
|
|
|
let builtUrl = `/api/${VERSION}/${url.join("/")}/`;
|
|
|
|
if (query) {
|
2020-12-01 08:15:41 +00:00
|
|
|
const queryString = Object.keys(query)
|
2021-02-03 20:18:31 +00:00
|
|
|
.filter((k) => query[k] !== null)
|
2021-02-04 20:10:13 +00:00
|
|
|
// we default to a string in query[k] as we've filtered out the null above
|
|
|
|
// this is just for type-hinting
|
|
|
|
.map((k) => encodeURIComponent(k) + "=" + encodeURIComponent(query[k] || ""))
|
2020-11-29 11:01:06 +00:00
|
|
|
.join("&");
|
|
|
|
builtUrl += `?${queryString}`;
|
|
|
|
}
|
|
|
|
return builtUrl;
|
2020-11-26 21:37:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
fetch<T>(url: string[], query?: QueryArguments): Promise<T> {
|
2020-11-29 11:01:06 +00:00
|
|
|
const finalUrl = this.makeUrl(url, query);
|
|
|
|
return fetch(finalUrl)
|
2020-11-26 21:37:41 +00:00
|
|
|
.then((r) => {
|
|
|
|
if (r.status > 300) {
|
|
|
|
switch (r.status) {
|
2020-12-01 08:15:41 +00:00
|
|
|
case 404:
|
|
|
|
throw new NotFoundError(`URL ${finalUrl} not found`);
|
|
|
|
default:
|
|
|
|
throw new RequestError(r.statusText);
|
2020-11-26 21:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
})
|
2021-03-03 09:06:44 +00:00
|
|
|
.catch((e) => {
|
|
|
|
showMessage({
|
|
|
|
level_tag: "error",
|
|
|
|
message: gettext(`Unexpected error while fetching: ${e.toString()}`),
|
|
|
|
});
|
|
|
|
return e;
|
|
|
|
})
|
2020-11-26 21:37:41 +00:00
|
|
|
.then((r) => r.json())
|
|
|
|
.then((r) => <T>r);
|
|
|
|
}
|
2021-01-16 18:08:20 +00:00
|
|
|
|
2021-02-17 19:49:58 +00:00
|
|
|
private writeRequest<T>(url: string[], body: T, method: string, query?: QueryArguments): Promise<T> {
|
2021-01-16 18:08:20 +00:00
|
|
|
const finalUrl = this.makeUrl(url, query);
|
|
|
|
const csrftoken = getCookie("authentik_csrf");
|
|
|
|
const request = new Request(finalUrl, {
|
|
|
|
headers: {
|
|
|
|
"Accept": "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-CSRFToken": csrftoken,
|
2021-02-17 19:49:58 +00:00
|
|
|
},
|
2021-01-16 18:08:20 +00:00
|
|
|
});
|
|
|
|
return fetch(request, {
|
2021-02-17 19:49:58 +00:00
|
|
|
method: method,
|
2021-01-16 18:08:20 +00:00
|
|
|
mode: "same-origin",
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
})
|
|
|
|
.then((r) => {
|
|
|
|
if (r.status > 300) {
|
|
|
|
switch (r.status) {
|
|
|
|
case 404:
|
|
|
|
throw new NotFoundError(`URL ${finalUrl} not found`);
|
|
|
|
default:
|
|
|
|
throw new RequestError(r.statusText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
})
|
|
|
|
.then((r) => r.json())
|
|
|
|
.then((r) => <T>r);
|
|
|
|
}
|
2021-02-17 19:49:58 +00:00
|
|
|
|
|
|
|
update<T>(url: string[], body: T, query?: QueryArguments): Promise<T> {
|
|
|
|
return this.writeRequest(url, body, "PATCH", query);
|
|
|
|
}
|
2020-11-26 21:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const DefaultClient = new Client();
|
2020-11-27 17:37:56 +00:00
|
|
|
|
2020-11-29 21:14:48 +00:00
|
|
|
export interface PBPagination {
|
|
|
|
next?: number;
|
|
|
|
previous?: number;
|
|
|
|
|
2020-11-27 17:37:56 +00:00
|
|
|
count: number;
|
2020-11-29 21:14:48 +00:00
|
|
|
current: number;
|
|
|
|
total_pages: number;
|
|
|
|
|
|
|
|
start_index: number;
|
|
|
|
end_index: number;
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:04:55 +00:00
|
|
|
export interface AKResponse<T> {
|
2020-11-29 21:14:48 +00:00
|
|
|
pagination: PBPagination;
|
|
|
|
|
2020-11-30 11:33:09 +00:00
|
|
|
results: Array<T>;
|
2020-11-27 17:37:56 +00:00
|
|
|
}
|