Skip to content

Commit

Permalink
Add api version in ClientOptions and policy inside generated client (#…
Browse files Browse the repository at this point in the history
…2561)

* remove-api-version-from-options-and-add-policy-inside-client

* generate api version in client side

* to not use delete

* fix ci error

* fix autorest ci

* fix ci

* api version support

* fix ci

* fix ci

* fix autorest codegen ci

* add use options for documentation

* resolve comments

* fix ci

* merge main

* fix docs

* fix ci

* fix ci error

* fix ci

* fix unit test

---------

Co-authored-by: Jiao Di (MSFT) <80496810+v-jiaodi@users.noreply.github.com>
  • Loading branch information
qiaozha and v-jiaodi committed Jun 26, 2024
1 parent 773418d commit b0853b2
Show file tree
Hide file tree
Showing 253 changed files with 1,847 additions and 962 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ function transformApiVersion(
const queryVersionDetail = getOperationQueryApiVersion(model);
const pathVersionDetail = extractPathApiVersion(urlInfo);
const isCrossedVersion =
pathVersionDetail?.isCrossedVersion || queryVersionDetail?.isCrossedVersion;
queryVersionDetail || pathVersionDetail
? Boolean(
pathVersionDetail?.isCrossedVersion ||
queryVersionDetail?.isCrossedVersion
)
: undefined;
let defaultValue =
pathVersionDetail?.defaultValue ?? queryVersionDetail?.defaultValue;

Expand All @@ -88,7 +93,8 @@ function transformApiVersion(
pathVersionDetail
),
isCrossedVersion,
defaultValue
defaultValue,
required: pathVersionDetail?.required ?? queryVersionDetail?.required
};
}

Expand All @@ -112,9 +118,10 @@ function getOperationQueryApiVersion(

if (apiVersionParam && isConstantSchema(apiVersionParam.schema)) {
return {
definedPosition: "path",
definedPosition: "query",
isCrossedVersion: false,
defaultValue: apiVersionParam.schema.value.value.toString()
defaultValue: apiVersionParam.schema.value.value.toString(),
required: apiVersionParam.required
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { BodyComplexRestClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface BodyComplexRestClientOptions extends ClientOptions {
/** The api version option of the client */
apiVersion?: string;
}

/**
* Initialize a new instance of `BodyComplexRestClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
): BodyComplexRestClient {
export default function createClient({
apiVersion = "2016-02-29",
...options
}: BodyComplexRestClientOptions = {}): BodyComplexRestClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
const userAgentInfo = `azsdk-js-body-complex-rest/1.0.0-preview1`;
Expand All @@ -28,15 +35,23 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as BodyComplexRestClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
if (options.apiVersion) {
logger.warning(
"This client does not support client api-version, please change it at the operation level",
);
}
client.pipeline.addPolicy({
name: "ClientApiVersionPolicy",
sendRequest: (req, next) => {
// Use the apiVersion defined in request url directly
// Append one if there is no apiVersion and we have one at client options
const url = new URL(req.url);
if (!url.searchParams.get("api-version") && apiVersion) {
req.url = `${req.url}${
Array.from(url.searchParams.keys()).length > 0 ? "&" : "?"
}api-version=${apiVersion}`;
}

return next(req);
},
});
return client;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { BodyFileClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface BodyFileClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `BodyFileClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: BodyFileClientOptions = {},
): BodyFileClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as BodyFileClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { BodyFormDataClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface BodyFormDataClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `BodyFormDataClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: BodyFormDataClientOptions = {},
): BodyFormDataClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as BodyFormDataClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { BodyStringRestClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface BodyStringRestClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `BodyStringRestClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: BodyStringRestClientOptions = {},
): BodyStringRestClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as BodyStringRestClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { CustomUrlRestClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface CustomUrlRestClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `CustomUrlRestClient`
* @param host - A string value that is used as a global part of the parameterized host
* @param options - the parameter for all optional parameters
*/
export default function createClient(
host: string,
options: ClientOptions = {},
options: CustomUrlRestClientOptions = {},
): CustomUrlRestClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://{accountName}${host}`;
Expand All @@ -30,7 +33,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as CustomUrlRestClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { DPGCustomizationClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface DPGCustomizationClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `DPGCustomizationClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: DPGCustomizationClientOptions = {},
): DPGCustomizationClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as DPGCustomizationClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { HeaderRestClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface HeaderRestClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `HeaderRestClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: HeaderRestClientOptions = {},
): HeaderRestClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as HeaderRestClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { HttpInfrastructureRestClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface HttpInfrastructureRestClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `HttpInfrastructureRestClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: HttpInfrastructureRestClientOptions = {},
): HttpInfrastructureRestClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(
endpointUrl,
options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { LRORestClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface LRORestClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `LRORestClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: LRORestClientOptions = {},
): LRORestClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as LRORestClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { MediaTypesClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface MediaTypesClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `MediaTypesClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: MediaTypesClientOptions = {},
): MediaTypesClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as MediaTypesClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { MultipleInheritanceRestClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface MultipleInheritanceRestClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `MultipleInheritanceRestClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
options: MultipleInheritanceRestClientOptions = {},
): MultipleInheritanceRestClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
Expand All @@ -28,7 +31,6 @@ export default function createClient(
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(
endpointUrl,
options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { logger } from "./logger";
import { TokenCredential } from "@azure/core-auth";
import { MultipleUrlParameterRestClient } from "./clientDefinitions";

/** The optional parameters for the client */
export interface MultipleUrlParameterRestClientOptions extends ClientOptions {}

/**
* Initialize a new instance of `MultipleUrlParameterRestClient`
* @param endpoint - The catalog endpoint of your Purview account. Example: https://{accountName}.purview.azure.com
Expand All @@ -17,13 +20,12 @@ export default function createClient(
endpoint: string,
serviceVersion: "v2" | "v1",
credentials: TokenCredential,
options: ClientOptions = {},
options: MultipleUrlParameterRestClientOptions = {},
): MultipleUrlParameterRestClient {
const endpointUrl =
options.endpoint ??
options.baseUrl ??
`${endpoint}/catalog/api/atlas/${serviceVersion}/{accountName}`;

const userAgentInfo = `azsdk-js-multiple-url-parameter-rest/1.0.0-preview1`;
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
Expand All @@ -41,7 +43,6 @@ export default function createClient(
scopes: options.credentials?.scopes ?? ["user_impersonation"],
},
};

const client = getClient(
endpointUrl,
credentials,
Expand Down
Loading

0 comments on commit b0853b2

Please sign in to comment.