Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some issues with API version handling in new getClient #2738

Merged
merged 7 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function createNetworkAnalytics(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
timovv marked this conversation as resolved.
Show resolved Hide resolved
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function createAnomalyDetector(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
timovv marked this conversation as resolved.
Show resolved Hide resolved
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function createBatch(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function createChatProtocol(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createContentSafety(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createEventGrid(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createFoo(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createAdministrationOperations(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createTestProfileAdministrationOperations(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createTestProfileRunOperations(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createTestRunOperations(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createOpenAI(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createOpenAI(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export function createOpenAI(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = { ...options, userAgentOptions: { userAgentPrefix } };
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
};
const clientContext = getClient(endpointUrl, undefined, updatedOptions);

if (isKeyCredential(credential)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createWidgetManager(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function createParametrizedHost(
const host = options.host ?? "one";
const subdomain = options.subdomain ?? "two";
const sufix = options.sufix ?? "three";
const apiVersion = options.apiVersion;
const apiVersion = options.apiVersion ?? "v1";
const endpointUrl =
options.endpoint ??
options.baseUrl ??
Expand All @@ -32,7 +32,7 @@ export function createParametrizedHost(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createSchemaRegistry(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createDemoService(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createWidgetService(
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const updatedOptions = {
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
Expand Down
5 changes: 4 additions & 1 deletion packages/typespec-ts/src/modular/buildCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,10 @@ function emitParameter(
if (clientDefaultValue !== undefined) {
(paramMap as any).optional = true;
}
} else if (
}

if (
clientDefaultValue === undefined &&
paramMap.location === "endpointPath" &&
parameter.param.defaultValue?.valueKind === "StringValue"
) {
Expand Down
4 changes: 3 additions & 1 deletion packages/typespec-ts/src/modular/helpers/clientHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ export function buildGetClientOptionsParam(
const loggingOptions = buildLoggingOptions(codeModel.options.flavor);
const credentials = buildCredentials(codeModel, endpointParam);

let expr = `const updatedOptions = { ...options,`;
let expr = "const { apiVersion: _, ...updatedOptions } = {";

expr += "...options,";

if (userAgentOptions) {
expr += `userAgentOptions: ${userAgentOptions},`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { KeyCredential } from "@azure/core-auth";
import { ClientOptions, Client, getClient } from "@azure-rest/core-client";
import { logger } from "../logger.js";

export interface ApiKeyContext extends Client {}

/** Optional parameters for the client. */
export interface ApiKeyClientOptionalParams extends ClientOptions {}

/** Illustrates clients generated with ApiKey authentication. */
export function createApiKey(
credential: KeyCredential,
options: ApiKeyClientOptionalParams = {},
): ApiKeyContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
credentials: {
apiKeyHeaderName: options.credentials?.apiKeyHeaderName ?? "x-ms-api-key",
},
};
const clientContext = getClient(endpointUrl, credential, updatedOptions);
clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });
if (options.apiVersion) {
logger.warning(
"This client does not support client api-version, please change it at the operation level",
);
}
return clientContext;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { KeyCredential } from "@azure/core-auth";
import { ClientOptions, Client, getClient } from "@azure-rest/core-client";
import { logger } from "../logger.js";
import { isKeyCredential } from "@azure/core-auth";

export interface CustomContext extends Client {}

/** Optional parameters for the client. */
export interface CustomClientOptionalParams extends ClientOptions {}

/** Illustrates clients generated with generic HTTP auth. */
export function createCustom(
credential: KeyCredential,
options: CustomClientOptionalParams = {},
): CustomContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
};
const clientContext = getClient(endpointUrl, undefined, updatedOptions);

if (isKeyCredential(credential)) {
clientContext.pipeline.addPolicy({
name: "customKeyCredentialPolicy",
sendRequest(request, next) {
request.headers.set(
"Authorization",
"SharedAccessKey " + credential.key,
);
return next(request);
},
});
}
clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });
if (options.apiVersion) {
logger.warning(
"This client does not support client api-version, please change it at the operation level",
);
}
return clientContext;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { TokenCredential } from "@azure/core-auth";
import { ClientOptions, Client, getClient } from "@azure-rest/core-client";
import { logger } from "../logger.js";

export interface OAuth2Context extends Client {}

/** Optional parameters for the client. */
export interface OAuth2ClientOptionalParams extends ClientOptions {}

/** Illustrates clients generated with OAuth2 authentication. */
export function createOAuth2(
credential: TokenCredential,
options: OAuth2ClientOptionalParams = {},
): OAuth2Context {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
credentials: {
scopes: options.credentials?.scopes ?? [
"https://security.microsoft.com/.default",
],
},
};
const clientContext = getClient(endpointUrl, credential, updatedOptions);
clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });
if (options.apiVersion) {
logger.warning(
"This client does not support client api-version, please change it at the operation level",
);
}
return clientContext;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { TokenCredential, KeyCredential } from "@azure/core-auth";
import { ClientOptions, Client, getClient } from "@azure-rest/core-client";
import { logger } from "../logger.js";

export interface UnionContext extends Client {}

/** Optional parameters for the client. */
export interface UnionClientOptionalParams extends ClientOptions {}

/** Illustrates clients generated with ApiKey and OAuth2 authentication. */
export function createUnion(
credential: KeyCredential | TokenCredential,
options: UnionClientOptionalParams = {},
): UnionContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
credentials: {
scopes: options.credentials?.scopes ?? [
"https://security.microsoft.com/.default",
],
apiKeyHeaderName: options.credentials?.apiKeyHeaderName ?? "x-ms-api-key",
},
};
const clientContext = getClient(endpointUrl, credential, updatedOptions);
clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });
if (options.apiVersion) {
logger.warning(
"This client does not support client api-version, please change it at the operation level",
);
}
return clientContext;
}
Loading
Loading