Skip to content

Commit

Permalink
[3/] Improvement: Use loadedToken for fetching data (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyadkhaled committed Feb 8, 2024
1 parent c8854e4 commit 9e68f8f
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 58 deletions.
5 changes: 5 additions & 0 deletions packages/cli/changelog/@unreleased/pr-17.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: '[3/] Improvement: Use loadedToken for fetching data'
links:
- https://github.com/palantir/osdk-ts/pull/17
26 changes: 19 additions & 7 deletions packages/cli/src/commands/site/deploy/siteDeployCommand.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import {
artifacts,
ArtifactsSitesAdminV2Service,
createConjureContext,
createInternalClientContext,
thirdPartyApplicationService,
} from "#net";
import archiver from "archiver";
import * as fs from "node:fs";
import { Readable } from "node:stream";
import { ExitProcessError } from "../../../ExitProcessError.js";
import { autoVersion as findAutoVersion } from "../../../util/autoVersion.js";
import { loadToken } from "../../../util/token.js";
import type { SiteDeployArgs } from "./SiteDeployArgs.js";

export default async function siteDeployCommand(
Expand All @@ -38,6 +40,8 @@ export default async function siteDeployCommand(
gitTagPrefix,
uploadOnly,
directory,
token,
tokenFile,
}: SiteDeployArgs,
) {
if (!version && !autoVersion) {
Expand All @@ -46,6 +50,7 @@ export default async function siteDeployCommand(
"Either version or autoVersion must be specified",
);
}
const loadedToken = await loadToken(token, tokenFile);

const siteVersion = !version ? await findAutoVersion(gitTagPrefix) : version;
if (autoVersion) {
Expand All @@ -63,13 +68,16 @@ export default async function siteDeployCommand(
consola.start("Zippping site files");

const archive = archiver("zip").directory(directory, false);

const tokenProvider = () => loadedToken;
const clientCtx = createInternalClientContext(foundryUrl, tokenProvider);
await Promise.all([
artifacts.SiteAssetArtifactsService.uploadZippedSiteAsset(
foundryUrl,
application,
siteVersion,
Readable.toWeb(archive) as ReadableStream<any>, // This cast is because the dom fetch doesnt align type wise with streams
clientCtx,
{
application,
version: siteVersion,
zipFile: Readable.toWeb(archive) as ReadableStream<any>, // This cast is because the dom fetch doesnt align type wise with streams
},
),
archive.finalize(),
]);
Expand All @@ -78,9 +86,13 @@ export default async function siteDeployCommand(

if (!uploadOnly) {
const repositoryRid = await thirdPartyApplicationService
.fetchWebsiteRepositoryRid(foundryUrl, application);
.fetchWebsiteRepositoryRid(clientCtx, application);

const ctx = createConjureContext(foundryUrl, "/artifacts/api");
const ctx = createConjureContext(
foundryUrl,
"/artifacts/api",
tokenProvider,
);
await ArtifactsSitesAdminV2Service.updateDeployedVersion(
ctx,
repositoryRid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
* limitations under the License.
*/

import { artifacts, createInternalClientContext } from "#net";
import { consola } from "consola";
import { artifacts } from "../../../../net/index.mjs";
import { loadToken } from "../../../../util/token.js";
import type { SiteVersionArgs } from "../SiteVersionArgs.js";

export default async function versionDeleteCommand(
{ version, application, foundryUrl }: SiteVersionArgs,
{ version, application, foundryUrl, token, tokenFile }: SiteVersionArgs,
) {
consola.start(`Deleting version ${version}`);
const loadedToken = await loadToken(token, tokenFile);
const tokenProvider = () => loadedToken;
const clientCtx = createInternalClientContext(foundryUrl, tokenProvider);
await artifacts.SiteAssetArtifactsService.deleteSiteVersion(
foundryUrl,
application,
version,
clientCtx,
{ application, version },
);
consola.success(
`Deleted version ${version}`,
Expand Down
11 changes: 8 additions & 3 deletions packages/cli/src/commands/site/version/get/versionGetCommand.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@
import {
ArtifactsSitesAdminV2Service,
createConjureContext,
createInternalClientContext,
thirdPartyApplicationService,
} from "#net";
import { consola } from "consola";
import { loadToken } from "../../../../util/token.js";
import type { CommonSiteArgs } from "../../CommonSiteArgs.js";

export default async function versionGetCommand(
{ foundryUrl, application }: CommonSiteArgs,
{ foundryUrl, application, token, tokenFile }: CommonSiteArgs,
) {
const loadedToken = await loadToken(token, tokenFile);
const tokenProvider = () => loadedToken;
const clientCtx = createInternalClientContext(foundryUrl, tokenProvider);
consola.start("Getting live version");

const repositoryRid = await thirdPartyApplicationService
.fetchWebsiteRepositoryRid(foundryUrl, application);
.fetchWebsiteRepositoryRid(clientCtx, application);

const ctx = createConjureContext(foundryUrl, "/artifacts/api");
const ctx = createConjureContext(foundryUrl, "/artifacts/api", tokenProvider);

const deployedVersion = await ArtifactsSitesAdminV2Service.getDeployedVersion(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,30 @@ import {
artifacts,
ArtifactsSitesAdminV2Service,
createConjureContext,
createInternalClientContext,
thirdPartyApplicationService,
} from "#net";
import { consola } from "consola";
import { colorize } from "consola/utils";
import { loadToken } from "../../../../util/token.js";
import type { CommonSiteArgs } from "../../CommonSiteArgs.js";

export default async function versionListCommand(
{ foundryUrl, application }: CommonSiteArgs,
{ foundryUrl, application, token, tokenFile }: CommonSiteArgs,
) {
const loadedToken = await loadToken(token, tokenFile);
const tokenProvider = () => loadedToken;
const clientCtx = createInternalClientContext(foundryUrl, tokenProvider);
consola.start("Fetching versions & deployed version");

const repositoryRid = await thirdPartyApplicationService
.fetchWebsiteRepositoryRid(foundryUrl, application);
.fetchWebsiteRepositoryRid(clientCtx, application);

const ctx = createConjureContext(foundryUrl, "/artifacts/api");
const ctx = createConjureContext(foundryUrl, "/artifacts/api", tokenProvider);

const [versions, deployedVersion] = await Promise.all([
artifacts.SiteAssetArtifactsService.fetchSiteVersions(
foundryUrl,
clientCtx,
application,
),
ArtifactsSitesAdminV2Service.getDeployedVersion(ctx, repositoryRid),
Expand Down
11 changes: 8 additions & 3 deletions packages/cli/src/commands/site/version/set/versionSetCommand.mts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ import { consola } from "consola";
import {
ArtifactsSitesAdminV2Service,
createConjureContext,
createInternalClientContext,
thirdPartyApplicationService,
} from "#net";
import { loadToken } from "../../../../util/token.js";
import type { SiteVersionArgs } from "../SiteVersionArgs.js";

export default async function versionSetCommand(
{ version, application, foundryUrl }: SiteVersionArgs,
{ version, application, foundryUrl, token, tokenFile }: SiteVersionArgs,
) {
consola.start(`Setting live version`);
const loadedToken = await loadToken(token, tokenFile);
const tokenProvider = () => loadedToken;
const clientCtx = createInternalClientContext(foundryUrl, tokenProvider);
const repositoryRid = await thirdPartyApplicationService
.fetchWebsiteRepositoryRid(foundryUrl, application);
.fetchWebsiteRepositoryRid(clientCtx, application);

const ctx = createConjureContext(foundryUrl, "/artifacts/api");
const ctx = createConjureContext(foundryUrl, "/artifacts/api", tokenProvider);
if (version) {
await ArtifactsSitesAdminV2Service.updateDeployedVersion(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ import { consola } from "consola";
import {
ArtifactsSitesAdminV2Service,
createConjureContext,
createInternalClientContext,
thirdPartyApplicationService,
} from "#net";
import { loadToken } from "../../../../util/token.js";
import type { CommonSiteArgs } from "../../CommonSiteArgs.js";

export default async function versionUnsetCommand(
{ application, foundryUrl }: CommonSiteArgs,
{ application, foundryUrl, token, tokenFile }: CommonSiteArgs,
) {
const loadedToken = await loadToken(token, tokenFile);
const tokenProvider = () => loadedToken;
const clientCtx = createInternalClientContext(foundryUrl, tokenProvider);
consola.start("Clearing live site version");
const repositoryRid = await thirdPartyApplicationService
.fetchWebsiteRepositoryRid(foundryUrl, application);
const ctx = createConjureContext(foundryUrl, "/artifacts/api");
.fetchWebsiteRepositoryRid(clientCtx, application);
const ctx = createConjureContext(foundryUrl, "/artifacts/api", tokenProvider);
await ArtifactsSitesAdminV2Service.clearDeployedVersion(ctx, repositoryRid);
consola.success("Cleared live site version");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { ThirdPartyAppRid } from "../../ThirdPartyAppRid.js";

export interface DeleteSiteVersionRequest {
application: ThirdPartyAppRid;
version: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { ThirdPartyAppRid } from "../../ThirdPartyAppRid.js";

export interface UploadZippedSiteAssetRequest {
application: ThirdPartyAppRid;
version: string;
zipFile: ReadableStream | Blob | BufferSource;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@
import { consola } from "consola";
import { ExitProcessError } from "../../../ExitProcessError.js";
import { createFetch } from "../../createFetch.mjs";
import type { InternalClientContext } from "../../internalClientContext.mjs";
import { fetchWebsiteRepositoryRid } from "../../third-party-application-service/fetchWebsiteRepositoryRid.mjs";
import type { ThirdPartyAppRid } from "../../ThirdPartyAppRid.js";
import type { DeleteSiteVersionRequest } from "./DeleteSiteVersionRequest.mjs";
import { getSiteAssetBaseUrl } from "./getSiteAssetBaseUrl.mjs";

export async function deleteSiteVersion(
baseUrl: string,
thirdPartyAppRid: ThirdPartyAppRid,
version: string,
ctx: InternalClientContext,
request: DeleteSiteVersionRequest,
) {
const repositoryRid = await fetchWebsiteRepositoryRid(
baseUrl,
thirdPartyAppRid,
ctx,
request.application,
);

const url = `${
getSiteAssetBaseUrl(baseUrl, repositoryRid)
}/versions/${version}`;
getSiteAssetBaseUrl(ctx.foundryUrl, repositoryRid)
}/versions/${request.version}`;

const fetch = createFetch(() => process.env.FOUNDRY_SDK_AUTH_TOKEN as string);
const fetch = createFetch(ctx.tokenProvider);

const result = await fetch(
url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@
*/

import { createFetch } from "../../createFetch.mjs";
import type { InternalClientContext } from "../../internalClientContext.mjs";
import { fetchWebsiteRepositoryRid } from "../../third-party-application-service/fetchWebsiteRepositoryRid.mjs";
import type { ThirdPartyAppRid } from "../../ThirdPartyAppRid.js";
import { getSiteAssetBaseUrl } from "./getSiteAssetBaseUrl.mjs";
import type { SiteAssetVersions } from "./SiteAssetVersions.mjs";

export async function fetchSiteVersions(
baseUrl: string,
ctx: InternalClientContext,
thirdPartyAppRid: ThirdPartyAppRid,
) {
const repositoryRid = await fetchWebsiteRepositoryRid(
baseUrl,
ctx,
thirdPartyAppRid,
);

const url = `${getSiteAssetBaseUrl(baseUrl, repositoryRid)}/versions`;
const fetch = createFetch(() => process.env.FOUNDRY_SDK_AUTH_TOKEN as string);
const url = `${getSiteAssetBaseUrl(ctx.foundryUrl, repositoryRid)}/versions`;
const fetch = createFetch(ctx.tokenProvider);

const result = await fetch(url);
if (result.status === 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,29 @@
import { consola } from "consola";
import { ExitProcessError } from "../../../ExitProcessError.js";
import { createFetch } from "../../createFetch.mjs";
import type { InternalClientContext } from "../../internalClientContext.mjs";
import { fetchWebsiteRepositoryRid } from "../../third-party-application-service/fetchWebsiteRepositoryRid.mjs";
import type { ThirdPartyAppRid } from "../../ThirdPartyAppRid.js";
import { getSiteAssetBaseUrl } from "./getSiteAssetBaseUrl.mjs";
import type { UploadZippedSiteAssetRequest } from "./UploadZippedSiteAssetRequest.mjs";

export async function uploadZippedSiteAsset(
baseUrl: string,
thirdPartyAppRid: ThirdPartyAppRid,
version: string,
zipFile: ReadableStream | Blob | BufferSource,
ctx: InternalClientContext,
request: UploadZippedSiteAssetRequest,
) {
const repositoryRid = await fetchWebsiteRepositoryRid(
baseUrl,
thirdPartyAppRid,
ctx,
request.application,
);

const url = `${
getSiteAssetBaseUrl(baseUrl, repositoryRid)
}/versions/zip/${version}`;

const fetch = createFetch(() => process.env.FOUNDRY_SDK_AUTH_TOKEN as string);

getSiteAssetBaseUrl(ctx.foundryUrl, repositoryRid)
}/versions/zip/${request.version}`;
const fetch = createFetch(ctx.tokenProvider);
const result = await fetch(
url,
{
method: "PUT",
body: zipFile,
body: request.zipFile,
headers: {
"Content-Type": "application/octet-stream",
},
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/net/createConjureContext.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import type { ConjureContext } from "conjure-lite";
import { createFetch } from "./createFetch.mjs";

export function createConjureContext(
baseUrl: string,
foundryUrl: string,
servicePath: "/artifacts/api",
tokenProvider: () => string,
): ConjureContext {
return {
fetchFn: createFetch(() => process.env.FOUNDRY_SDK_AUTH_TOKEN as string),
baseUrl,
fetchFn: createFetch(tokenProvider),
baseUrl: foundryUrl,
servicePath,
};
}
Loading

0 comments on commit 9e68f8f

Please sign in to comment.