Skip to content

Commit

Permalink
[v6] Complete support for LRO (#611)
Browse files Browse the repository at this point in the history
* Transform OperationOptions to BaseRequestOptions

* Keep original request state

* Mode strategy discovery to LRO poller

* Override delay in tests

* Update LRO tests

* Fix unit Tests

* generate all test swaggers

* Add AzureASyncOperation

* Fix tests

* re-generate everything

* Fix injection issue

* Fixes in LRO Policy

* re-gen

* Refactor strategy methods

* Add tests

* Re generate

* enable test postDoubleHeadersFinalAzureHeaderGetDefault

* safely build path for copying LRO files

* Move response injection to generation

* Regen everything with latest testserver

* Use core-http dev and update test-server

* Update generated files

* renegerate
  • Loading branch information
joheredi authored Apr 10, 2020
1 parent ac5dbbd commit c134393
Show file tree
Hide file tree
Showing 88 changed files with 6,189 additions and 952 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "none"
}
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector"
},
{
"type": "node",
"request": "launch",
"name": "IntegrationTests - Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-r",
"ts-node/register",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/${relativeFile}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector"
}
]
}
69 changes: 57 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@azure-tools/codemodel": "4.13.317",
"@azure-tools/linq": "3.1.206",
"@azure-tools/openapi": "3.0.209",
"@azure/core-http": "^1.0.4",
"@azure/core-http": "^1.1.1-dev.20200410.1",
"@azure/core-lro": "^1.0.1",
"@azure/core-paging": "^1.0.0",
"@azure/logger": "^1.0.0",
Expand Down
16 changes: 7 additions & 9 deletions src/generators/LROGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ClientDetails } from "../models/clientDetails";
import { Project } from "ts-morph";
import { OperationGroupDetails } from "../models/operationDetails";
import { promises } from "fs";
import { join as joinPath } from "path";

export async function generateLROFiles(
clientDetails: ClientDetails,
Expand All @@ -11,19 +12,16 @@ export async function generateLROFiles(
return;
}

const lroFiles = [
{ name: "bodyPollingStrategy", file: "./src/lro/bodyPollingStrategy.ts" },
{ name: "lroPoller", file: "./src/lro/lroPoller.ts" },
{ name: "models", file: "./src/lro/models.ts" },
{ name: "operation", file: "./src/lro/operation.ts" }
];
const lroDir = joinPath(__dirname, "..", "..", "..", "src", "lro");
const lroFiles = await promises.readdir(lroDir);

for (let i = 0; i < lroFiles.length; i++) {
const { name, file } = lroFiles[i];
const fileContent = await promises.readFile(file, "utf-8");
const file = lroFiles[i];
const filePath = joinPath(lroDir, file);
const fileContent = await promises.readFile(filePath, "utf-8");

project.createSourceFile(
`${clientDetails.srcPath}/lro/${name}.ts`,
joinPath(clientDetails.srcPath || "", "lro", file),
fileContent,
{ overwrite: true }
);
Expand Down
49 changes: 40 additions & 9 deletions src/generators/clientContextFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export function generateClientContext(
const clientParams = clientDetails.parameters.filter(
param => param.implementationLocation === ImplementationLocation.Client
);

const hasLRO = clientDetails.operationGroups.some(og =>
og.operations.some(o => o.isLRO)
);

const clientContextClassName = `${clientDetails.className}Context`;
const clientContextFileName = normalizeName(
clientContextClassName,
Expand All @@ -39,7 +44,7 @@ export function generateClientContext(
}
);

writeImports(sourceFile);
writeImports(sourceFile, hasLRO);
writePackageInfo(sourceFile, packageDetails);

const contextClass = buildClass(sourceFile, clientContextClassName);
Expand All @@ -51,18 +56,22 @@ export function generateClientContext(
clientParams
});

writeConstructorBody(classConstructor, {
clientParams,
clientDetails
});
writeConstructorBody(
classConstructor,
{
clientParams,
clientDetails
},
hasLRO
);
}

interface WriteConstructorBodyParameters {
clientParams: ParameterDetails[];
clientDetails: ClientDetails;
}

function writeImports(sourceFile: SourceFile) {
function writeImports(sourceFile: SourceFile, hasLRO: boolean) {
sourceFile.addImportDeclaration({
namespaceImport: "coreHttp",
moduleSpecifier: "@azure/core-http"
Expand All @@ -72,6 +81,13 @@ function writeImports(sourceFile: SourceFile) {
namespaceImport: "Models",
moduleSpecifier: "./models"
});

if (hasLRO) {
sourceFile.addImportDeclaration({
namedImports: ["lroPolicy"],
moduleSpecifier: "./lro"
});
}
}

function writePackageInfo(
Expand Down Expand Up @@ -104,7 +120,8 @@ function writeClassProperties(

function writeConstructorBody(
classConstructor: ConstructorDeclaration,
{ clientParams, clientDetails }: WriteConstructorBodyParameters
{ clientParams, clientDetails }: WriteConstructorBodyParameters,
hasLRO: boolean
) {
const requiredParams = getRequiredParameters(clientParams);
const addBlankLine = true;
Expand All @@ -113,7 +130,10 @@ function writeConstructorBody(
classConstructor.addStatements([
writeStatements(getRequiredParamChecks(requiredParams), addBlankLine),
writeStatement(
writeDefaultOptions(clientParams.some(p => p.name === "credentials"))
writeDefaultOptions(
clientParams.some(p => p.name === "credentials"),
hasLRO
)
),
writeStatement(getEndpointStatement(clientDetails.endpoint), addBlankLine),
requiredParameters.length ? "// Parameter assignments" : "",
Expand Down Expand Up @@ -141,7 +161,16 @@ const writeStatements = (lines: string[], shouldAddBlankLine = false) => (
shouldAddBlankLine && writer.blankLine();
};

function writeDefaultOptions(hasCredentials: boolean) {
function writeDefaultOptions(hasCredentials: boolean, hasLRO: boolean) {
const policies = `
const defaultPipelines = coreHttp.createPipelineFromOptions(options)
.requestPolicyFactories as coreHttp.RequestPolicyFactory[];
options = {
...options,
requestPolicyFactories: [lroPolicy(), ...defaultPipelines]
};`;

return `// Initializing default values for options
if (!options) {
options = {};
Expand All @@ -152,6 +181,8 @@ function writeDefaultOptions(hasCredentials: boolean) {
options.userAgent = \`\${packageName}/\${packageVersion} \${defaultUserAgent}\`;
}
${hasLRO ? policies : ""}
super(${hasCredentials ? "credentials" : `undefined`}, options);
this.requestContentType = "application/json; charset=utf-8";
Expand Down
Loading

0 comments on commit c134393

Please sign in to comment.