Skip to content

Commit

Permalink
Merge pull request #3 from kontent-ai/KCL-12377_fix_getting_preview_a…
Browse files Browse the repository at this point in the history
…pi_key

KCL-12377 Fix code that gets preview api key for a project
  • Loading branch information
JiriLojda authored Feb 14, 2024
2 parents 4aaf95c + 9f9997a commit 3bd8fe4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ REACT_APP_AUTH_DOMAIN="login.devkontentmasters.com"
REACT_APP_AUTH_CLIENT_ID="NPIPF1KyuQ7W0pgfE50nms09aDUR4mKi"

REACT_APP_KONTENT_URL="https://app.devkontentmasters.com"
REACT_APP_DELIVER_URL="https://qa-preview-deliver.global.ssl.fastly.net"
REACT_APP_DELIVER_URL="https://preview-deliver.devkontentmasters.com"
12 changes: 5 additions & 7 deletions src/context/AppContextInitialization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AuthContextConsumer, IAuthContext } from "./AuthContext";
import { AppContextConsumer, IAppContext } from "./AppContext";
import { Loading } from "../components/Loading";
import { ErrorPage, ErrorPageType } from "../components/ErrorPage";
import { getPreviewApiKey } from "../repositories/previewApiKeyRepository";
import { LoadingStatus } from "../enums/LoadingStatus";
import { createLoadApplicationData } from "../factories/createLoadApplicationData";
import { createLoadPreviewApiKey } from "../factories/createLoadPreviewApiKey";
Expand All @@ -25,18 +24,18 @@ class AppContextInitialization extends React.PureComponent<IAppContextInitializa
render() {
const { projectIdLoadingStatus, previewApiKeyLoadingStatus, dataLoadingStatus } = this.props.appContext;
if (projectIdLoadingStatus === LoadingStatus.Failed) {
return <ErrorPage type={ErrorPageType.MissingProjectId}/>
return <ErrorPage type={ErrorPageType.MissingProjectId} />
}

if (previewApiKeyLoadingStatus === LoadingStatus.Failed) {
return <ErrorPage type={ErrorPageType.UnableToGetPreviewApiKey}/>
return <ErrorPage type={ErrorPageType.UnableToGetPreviewApiKey} />
}

if (dataLoadingStatus === LoadingStatus.Finished) {
return this.props.children;
}

return <Loading/>
return <Loading />
}
}

Expand All @@ -51,8 +50,7 @@ const AppContextInitializationConnected = (props: RouteComponentProps) => (
loadPreviewApikey: createLoadPreviewApiKey({
authContext,
appContext,
getPreviewApiKey,
})
}),
});

return (
Expand All @@ -61,7 +59,7 @@ const AppContextInitializationConnected = (props: RouteComponentProps) => (
authContext={authContext}
appContext={appContext}
{...props}
/>);
/>);
}}
</AuthContextConsumer>
)}
Expand Down
21 changes: 13 additions & 8 deletions src/factories/createLoadPreviewApiKey.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { IAppContext } from "../context/AppContext";
import { IAuthContext } from "../context/AuthContext";
import { IPreviewApiKey } from "../repositories/previewApiKeyRepository";
import { getPreviewApiTokenSeed, getKeyForTokenSeed } from "../repositories/previewApiKeyRepository";
import { getProjectContainerForEnvironment } from "../repositories/projectContainerRepository";

interface ILoadPreviewApiKeyDeps {
readonly appContext: IAppContext;
readonly authContext: IAuthContext;
readonly getPreviewApiKey: (authToken: string, projectId: string) => Promise<IPreviewApiKey>;
}

export const createLoadPreviewApiKey = (props: ILoadPreviewApiKeyDeps): () => Promise<string | null> => {
const { accessToken } = props.authContext;
const { projectId } = props.appContext;
return () => props.getPreviewApiKey(accessToken, projectId)
.then((response: IPreviewApiKey) => {
return response.api_key;
})
.catch(() => {
return async () => {
const projectContainerId = await getProjectContainerForEnvironment(accessToken, projectId).then(res => res.projectContainerId);
const tokenSeed = await getPreviewApiTokenSeed(accessToken, projectContainerId, projectId).then(res => res[0]?.token_seed_id);

if (!tokenSeed) {
return null;
})
}

return getKeyForTokenSeed(accessToken, projectContainerId, tokenSeed)
.then(response => response.api_key)
.catch(() => null);
}
};
30 changes: 24 additions & 6 deletions src/repositories/previewApiKeyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,33 @@ import {

const restProvider = createRestProvider(createAjaxWithCredentials());

export interface IPreviewApiKey {
readonly api_key: string;
readonly expiresAt: string;
export interface TokenSeedResponse {
token_seed_id: string;
}

export const getPreviewApiKey = (authToken: string, projectId: string): Promise<IPreviewApiKey> => {
export const getPreviewApiTokenSeed = (authToken: string, projectContainerId: string, environmentId: string): Promise<ReadonlyArray<TokenSeedResponse>> => {
const requestContext: IRequestContext = {
authToken: authToken,
};
const url = `${process.env.REACT_APP_KONTENT_URL}/api/project-management/${projectId}/keys/preview-delivery-api-primary`;
return restProvider.post(url, null, requestContext);
const url = `${process.env.REACT_APP_KONTENT_URL}/api/project-container/${projectContainerId}/keys/listing`;
const data = {
query: '',
'api_key_types': ['delivery-api'],
environments: [environmentId],
};

return restProvider.post(url, data, requestContext);
};

export interface KeyFromSeedResponse {
api_key: string;
}

export const getKeyForTokenSeed = (authToken: string, projectContainerId: string, tokenSeed: string): Promise<KeyFromSeedResponse> => {
const requestContext: IRequestContext = {
authToken: authToken,
};
const url = `${process.env.REACT_APP_KONTENT_URL}/api/project-container/${projectContainerId}/keys/${tokenSeed}`;

return restProvider.get(url, requestContext);
};
19 changes: 19 additions & 0 deletions src/repositories/projectContainerRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createAjaxWithCredentials } from '../utils/ajax';
import {
createRestProvider,
IRequestContext,
} from '../utils/restProvider';

const restProvider = createRestProvider(createAjaxWithCredentials());

export interface ProjectContainer {
projectContainerId: string;
}

export const getProjectContainerForEnvironment = (authToken: string, environmentId: string): Promise<ProjectContainer> => {
const requestContext: IRequestContext = {
authToken: authToken,
};
const url = `${process.env.REACT_APP_KONTENT_URL}/api/project-management/${environmentId}`;
return restProvider.get(url, requestContext);
};
11 changes: 6 additions & 5 deletions src/utils/restProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export function createRestProvider(ajax: any) {
}

return {
get(url: string, requestContext?: IRequestContext): Promise<any> {
return makeRequest('GET', url, undefined, requestContext)
.then(verifyStatusCode([200]))
.then(parseResponse);
},
post(url: string, data: any, requestContext?: IRequestContext): Promise<any> {
return makeRequest('POST', url, data, requestContext)
.then(verifyStatusCode([200, 201]))
Expand All @@ -49,11 +54,7 @@ export function createRestProvider(ajax: any) {
}

function prepareDataForSend(data: any): any {
if (data && typeof data === 'object') {
// const dto = toDTO(data);
// return JSON.stringify(dto);
}
else if (typeof data === 'string') {
if ((data && typeof data === 'object') || typeof data === "string") {
return JSON.stringify(data);
}

Expand Down

0 comments on commit 3bd8fe4

Please sign in to comment.