-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: export open api * feat: restore support export * feat: support export api * feat: export open api * feat: restore support export * feat: update * feat: update * feat: update * feat: update method name * feat: update * feat: support command palette to export api * test: add test cases * test: add extension test cases * test: revert extension tests * test: update * feat: update * feat: add * feat: update
- Loading branch information
1 parent
1377ba5
commit b912daf
Showing
7 changed files
with
142 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { getResourceGroupFromId } from "@microsoft/vscode-azext-azureutils"; | ||
import { IActionContext } from "@microsoft/vscode-azext-utils"; | ||
import * as fs from "fs-extra"; | ||
import * as path from "path"; | ||
import * as vscode from "vscode"; | ||
import { ApiCenterService } from "../azure/ApiCenter/ApiCenterService"; | ||
import { ApiSpecExportResultFormat } from "../azure/ApiCenter/contracts"; | ||
import { TelemetryClient } from '../common/telemetryClient'; | ||
import { ext } from "../extensionVariables"; | ||
import { ApiVersionDefinitionTreeItem } from "../tree/ApiVersionDefinitionTreeItem"; | ||
import { createTemporaryFolder } from "../utils/fsUtil"; | ||
export namespace ExportAPI { | ||
export async function exportApi( | ||
context: IActionContext, | ||
node?: ApiVersionDefinitionTreeItem): Promise<void> { | ||
if (!node) { | ||
node = await ext.treeDataProvider.showTreeItemPicker<ApiVersionDefinitionTreeItem>(new RegExp(`${ApiVersionDefinitionTreeItem.contextValue}*`), context); | ||
} | ||
|
||
const apiCenterService = new ApiCenterService( | ||
node?.subscription!, | ||
getResourceGroupFromId(node?.id!), | ||
node?.apiCenterName!); | ||
const exportedSpec = await apiCenterService.exportSpecification( | ||
node?.apiCenterApiName!, | ||
node?.apiCenterApiVersionName!, | ||
node?.apiCenterApiVersionDefinition.name!); | ||
await writeToTempFile(node!, exportedSpec.format, exportedSpec.value); | ||
} | ||
|
||
function getFolderName(treeItem: ApiVersionDefinitionTreeItem): string { | ||
return `${treeItem.apiCenterName}-${treeItem.apiCenterApiName}`; | ||
} | ||
|
||
function getFilename(treeItem: ApiVersionDefinitionTreeItem): string { | ||
return `${treeItem.apiCenterApiVersionDefinition.name}`; | ||
} | ||
|
||
async function writeToTempFile(node: ApiVersionDefinitionTreeItem, specFormat: string, specValue: string) { | ||
if (specFormat === ApiSpecExportResultFormat.inline) { | ||
await ExportAPI.showTempFile(node, specValue); | ||
} else { | ||
// Currently at server side did not exist link, so just monitor this event. | ||
TelemetryClient.sendEvent("azure-api-center.exportApi", { format: specFormat }); | ||
} | ||
} | ||
|
||
export async function showTempFile(node: ApiVersionDefinitionTreeItem, fileContent: string) { | ||
const folderName = getFolderName(node); | ||
const folderPath = await createTemporaryFolder(folderName); | ||
const fileName = getFilename(node); | ||
const localFilePath: string = path.join(folderPath, fileName); | ||
await fs.ensureFile(localFilePath); | ||
const document: vscode.TextDocument = await vscode.workspace.openTextDocument(localFilePath); | ||
await vscode.workspace.fs.writeFile(vscode.Uri.file(localFilePath), Buffer.from(fileContent)); | ||
await vscode.window.showTextDocument(document); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { AzExtParentTreeItem, AzExtTreeItem, IActionContext } from "@microsoft/vscode-azext-utils"; | ||
import * as sinon from "sinon"; | ||
import { ApiCenterService } from "../../../azure/ApiCenter/ApiCenterService"; | ||
import { ApiCenterApiVersionDefinition } from "../../../azure/ApiCenter/contracts"; | ||
import { ExportAPI } from "../../../commands/exportApi"; | ||
import { TelemetryClient } from "../../../common/telemetryClient"; | ||
import { ApiVersionDefinitionTreeItem } from "../../../tree/ApiVersionDefinitionTreeItem"; | ||
abstract class ParentTreeItemBase extends AzExtParentTreeItem { | ||
private _childIndex: number = 0; | ||
public async loadMoreChildrenImpl(clearCache: boolean, context: IActionContext): Promise<AzExtTreeItem[]> { | ||
const children: AzExtTreeItem[] = []; | ||
return children; | ||
} | ||
public hasMoreChildrenImpl(): boolean { | ||
return this._childIndex < 10; | ||
} | ||
protected abstract createChildTreeItem(index: number): AzExtTreeItem; | ||
} | ||
|
||
class RootTreeItem extends ParentTreeItemBase { | ||
public label: string = 'root'; | ||
public contextValue: string = 'root'; | ||
|
||
protected createChildTreeItem(index: number): AzExtTreeItem { | ||
return new ApiVersionDefinitionTreeItem(this, "fakeApiCenterName", "fakeApiCenterApiName", "fakeApiCenterApiVersionName", {} as ApiCenterApiVersionDefinition); | ||
} | ||
} | ||
|
||
suite("export API test cases", () => { | ||
let sandbox = null as any; | ||
let root: RootTreeItem; | ||
let node: ApiVersionDefinitionTreeItem; | ||
suiteSetup(() => { | ||
sandbox = sinon.createSandbox(); | ||
sinon.stub(TelemetryClient, "sendEvent").returns(); | ||
}); | ||
setup(() => { | ||
root = new RootTreeItem(undefined); | ||
node = new ApiVersionDefinitionTreeItem(root, | ||
"fakeApiCenterName", | ||
"fakeApiCenterApiName", | ||
"fakeApiCenterApiVersionName", | ||
{ | ||
properties: { | ||
specification: { | ||
name: "fakeName" | ||
} | ||
} | ||
} as ApiCenterApiVersionDefinition | ||
); | ||
sandbox.stub(node, "subscription").value("fakeSub"); | ||
sandbox.stub(node, "id").value("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test/providers/Microsoft.ApiCenter/services/test/workspaces/default/apis/test/versions/v1/definitions/openapi"); | ||
}); | ||
teardown(() => { | ||
sandbox.restore(); | ||
}); | ||
test('export API happy path with link type', async () => { | ||
const spyShowTempFile = sandbox.spy(ExportAPI, "showTempFile"); | ||
sandbox.stub(ApiCenterService.prototype, "exportSpecification").resolves({ format: "link", value: "fakeValue" }); | ||
await ExportAPI.exportApi({} as IActionContext, node); | ||
sandbox.assert.notCalled(spyShowTempFile); | ||
}); | ||
test('export API happy path with inline type', async () => { | ||
let stubShowTempFile = sandbox.stub(ExportAPI, "showTempFile").resolves(); | ||
sandbox.stub(ApiCenterService.prototype, "exportSpecification").resolves({ format: "inline", value: "fakeValue" }); | ||
await ExportAPI.exportApi({} as IActionContext, node); | ||
sandbox.assert.calledOnce(stubShowTempFile); | ||
}); | ||
}); |