use admin client instead of fetch (#20774)

This commit is contained in:
Erik Jan de Wit 2023-06-12 12:50:53 +02:00 committed by GitHub
parent db315b59f6
commit 911663b65c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 36 deletions

View file

@ -5,9 +5,6 @@ import { HelpItem } from "ui-shared";
import { adminClient } from "../../admin-client"; import { adminClient } from "../../admin-client";
import { JsonFileUpload } from "../../components/json-file-upload/JsonFileUpload"; import { JsonFileUpload } from "../../components/json-file-upload/JsonFileUpload";
import { useRealm } from "../../context/realm-context/RealmContext";
import { addTrailingSlash } from "../../util";
import { getAuthorizationHeaders } from "../../utils/getAuthorizationHeaders";
import { DiscoveryEndpointField } from "../component/DiscoveryEndpointField"; import { DiscoveryEndpointField } from "../component/DiscoveryEndpointField";
import { DiscoverySettings } from "./DiscoverySettings"; import { DiscoverySettings } from "./DiscoverySettings";
@ -15,7 +12,6 @@ export const OpenIdConnectSettings = () => {
const { t } = useTranslation("identity-providers"); const { t } = useTranslation("identity-providers");
const id = "oidc"; const id = "oidc";
const { realm } = useRealm();
const { const {
setValue, setValue,
setError, setError,
@ -38,25 +34,10 @@ export const OpenIdConnectSettings = () => {
formData.append("file", new Blob([JSON.stringify(obj)])); formData.append("file", new Blob([JSON.stringify(obj)]));
try { try {
const response = await fetch( const result = await adminClient.identityProviders.importFromUrl(
`${addTrailingSlash( formData
adminClient.baseUrl
)}admin/realms/${realm}/identity-provider/import-config`,
{
method: "POST",
body: formData,
headers: getAuthorizationHeaders(await adminClient.getAccessToken()),
}
); );
if (response.ok) { setupForm(result);
const result = await response.json();
setupForm(result);
} else {
setError("discoveryError", {
type: "manual",
message: response.statusText,
});
}
} catch (error) { } catch (error) {
setError("discoveryError", { setError("discoveryError", {
type: "manual", type: "manual",

View file

@ -82,22 +82,23 @@ export class Agent {
const baseParams = this.getBaseParams?.() ?? {}; const baseParams = this.getBaseParams?.() ?? {};
// Filter query parameters by queryParamKeys // Filter query parameters by queryParamKeys
const queryParams = queryParamKeys const queryParams =
? pick(payload, queryParamKeys) queryParamKeys.length > 0 ? pick(payload, queryParamKeys) : undefined;
: undefined;
// Add filtered payload parameters to base parameters // Add filtered payload parameters to base parameters
const allUrlParamKeys = [...Object.keys(baseParams), ...urlParamKeys]; const allUrlParamKeys = [...Object.keys(baseParams), ...urlParamKeys];
const urlParams = { ...baseParams, ...pick(payload, allUrlParamKeys) }; const urlParams = { ...baseParams, ...pick(payload, allUrlParamKeys) };
// Omit url parameters and query parameters from payload if (!(payload instanceof FormData)) {
const omittedKeys = ignoredKeys // Omit url parameters and query parameters from payload
? [...allUrlParamKeys, ...queryParamKeys].filter( const omittedKeys = ignoredKeys
(key) => !ignoredKeys.includes(key) ? [...allUrlParamKeys, ...queryParamKeys].filter(
) (key) => !ignoredKeys.includes(key)
: [...allUrlParamKeys, ...queryParamKeys]; )
: [...allUrlParamKeys, ...queryParamKeys];
payload = omit(payload, omittedKeys); payload = omit(payload, omittedKeys);
}
// Transform keys of both payload and queryParams // Transform keys of both payload and queryParams
if (keyTransform) { if (keyTransform) {

View file

@ -116,10 +116,11 @@ export class IdentityProviders extends Resource<{ realm?: string }> {
}); });
public importFromUrl = this.makeRequest< public importFromUrl = this.makeRequest<
{ | {
fromUrl: string; fromUrl: string;
providerId: string; providerId: string;
}, }
| FormData,
Record<string, string> Record<string, string>
>({ >({
method: "POST", method: "POST",