Skip to content

Commit 5fc0411

Browse files
iscai-msftiscai-msft
andauthored
[python] let client options on child clients override parent client (#9776)
fixes #9757 --------- Co-authored-by: iscai-msft <isabellavcai@gmail.com>
1 parent e1510df commit 5fc0411

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-client-python"
5+
---
6+
7+
Allow client options on child clients to override parent clients

packages/http-client-python/emitter/src/code-model.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function emitBasicMethod<TServiceOperation extends SdkServiceOperation>(
3939
context: PythonSdkContext,
4040
rootClient: SdkClientType<TServiceOperation>,
4141
method: SdkBasicServiceMethod<TServiceOperation>,
42-
operationGroupName: string,
42+
operationGroup: SdkClientType<TServiceOperation>,
4343
serviceApiVersions: string[],
4444
): Record<string, any>[] {
4545
if (method.operation.kind !== "http")
@@ -50,7 +50,7 @@ function emitBasicMethod<TServiceOperation extends SdkServiceOperation>(
5050
context,
5151
rootClient,
5252
method,
53-
operationGroupName,
53+
operationGroup.name,
5454
serviceApiVersions,
5555
);
5656
default:
@@ -62,14 +62,20 @@ function emitLroMethod<TServiceOperation extends SdkServiceOperation>(
6262
context: PythonSdkContext,
6363
rootClient: SdkClientType<TServiceOperation>,
6464
method: SdkLroServiceMethod<TServiceOperation>,
65-
operationGroupName: string,
65+
operationGroup: SdkClientType<TServiceOperation>,
6666
serviceApiVersions: string[],
6767
): Record<string, any>[] {
6868
if (method.operation.kind !== "http")
6969
throw new Error("We only support HTTP operations right now");
7070
switch (method.operation.kind) {
7171
case "http":
72-
return emitLroHttpMethod(context, rootClient, method, operationGroupName, serviceApiVersions);
72+
return emitLroHttpMethod(
73+
context,
74+
rootClient,
75+
method,
76+
operationGroup.name,
77+
serviceApiVersions,
78+
);
7379
default:
7480
throw new Error("We only support HTTP operations right now");
7581
}
@@ -79,7 +85,7 @@ function emitPagingMethod<TServiceOperation extends SdkServiceOperation>(
7985
context: PythonSdkContext,
8086
rootClient: SdkClientType<TServiceOperation>,
8187
method: SdkPagingServiceMethod<TServiceOperation>,
82-
operationGroupName: string,
88+
operationGroup: SdkClientType<TServiceOperation>,
8389
serviceApiVersions: string[],
8490
): Record<string, any>[] {
8591
if (method.operation.kind !== "http")
@@ -90,7 +96,7 @@ function emitPagingMethod<TServiceOperation extends SdkServiceOperation>(
9096
context,
9197
rootClient,
9298
method,
93-
operationGroupName,
99+
operationGroup.name,
94100
serviceApiVersions,
95101
);
96102
default:
@@ -102,7 +108,7 @@ function emitLroPagingMethod<TServiceOperation extends SdkServiceOperation>(
102108
context: PythonSdkContext,
103109
rootClient: SdkClientType<TServiceOperation>,
104110
method: SdkLroPagingServiceMethod<TServiceOperation>,
105-
operationGroupName: string,
111+
operationGroup: SdkClientType<TServiceOperation>,
106112
serviceApiVersions: string[],
107113
): Record<string, any>[] {
108114
if (method.operation.kind !== "http")
@@ -113,7 +119,7 @@ function emitLroPagingMethod<TServiceOperation extends SdkServiceOperation>(
113119
context,
114120
rootClient,
115121
method,
116-
operationGroupName,
122+
operationGroup.name,
117123
serviceApiVersions,
118124
);
119125
default:
@@ -183,25 +189,19 @@ function emitMethodParameter(
183189
function emitMethod<TServiceOperation extends SdkServiceOperation>(
184190
context: PythonSdkContext,
185191
rootClient: SdkClientType<TServiceOperation>,
192+
operationGroup: SdkClientType<TServiceOperation>,
186193
method: SdkServiceMethod<TServiceOperation>,
187-
operationGroupName: string,
188194
serviceApiVersions: string[],
189195
): Record<string, any>[] {
190196
switch (method.kind) {
191197
case "basic":
192-
return emitBasicMethod(context, rootClient, method, operationGroupName, serviceApiVersions);
198+
return emitBasicMethod(context, rootClient, method, operationGroup, serviceApiVersions);
193199
case "lro":
194-
return emitLroMethod(context, rootClient, method, operationGroupName, serviceApiVersions);
200+
return emitLroMethod(context, rootClient, method, operationGroup, serviceApiVersions);
195201
case "paging":
196-
return emitPagingMethod(context, rootClient, method, operationGroupName, serviceApiVersions);
202+
return emitPagingMethod(context, rootClient, method, operationGroup, serviceApiVersions);
197203
default:
198-
return emitLroPagingMethod(
199-
context,
200-
rootClient,
201-
method,
202-
operationGroupName,
203-
serviceApiVersions,
204-
);
204+
return emitLroPagingMethod(context, rootClient, method, operationGroup, serviceApiVersions);
205205
}
206206
}
207207

@@ -218,11 +218,17 @@ function emitOperationGroups<TServiceOperation extends SdkServiceOperation>(
218218

219219
for (const operationGroup of client.children ?? []) {
220220
const name = `${prefix}${operationGroup.name}`;
221+
const operationGroupWithPrefixedName = {
222+
...operationGroup,
223+
name,
224+
} as SdkClientType<TServiceOperation>;
221225
let operations: Record<string, any>[] = [];
222226
const apiVersions =
223227
serviceApiVersions.length > 0 ? serviceApiVersions : operationGroup.apiVersions;
224228
for (const method of operationGroup.methods) {
225-
operations = operations.concat(emitMethod(context, rootClient, method, name, apiVersions));
229+
operations = operations.concat(
230+
emitMethod(context, rootClient, operationGroupWithPrefixedName, method, apiVersions),
231+
);
226232
}
227233
operationGroups.push({
228234
name: name,
@@ -236,10 +242,11 @@ function emitOperationGroups<TServiceOperation extends SdkServiceOperation>(
236242

237243
// root client should deal with mixin operation group
238244
if (prefix === "") {
245+
const mixinGroup = { ...client, name: "" } as SdkClientType<TServiceOperation>;
239246
let operations: Record<string, any>[] = [];
240247
for (const method of client.methods) {
241248
operations = operations.concat(
242-
emitMethod(context, rootClient, method, "", serviceApiVersions),
249+
emitMethod(context, rootClient, mixinGroup, method, serviceApiVersions),
243250
);
244251
}
245252
if (operations.length > 0) {

packages/http-client-python/emitter/src/http.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,16 @@ function emitHttpOperation(
379379
for (const exception of operation.exceptions) {
380380
exceptions.push(emitHttpResponse(context, exception.statusCodes, exception, undefined, true)!);
381381
}
382-
const includeRootSlash = getClientOptions(rootClient, "includeRootSlash") !== false;
382+
// Walk up the client hierarchy to find the option, allowing sub-clients to
383+
// override values set on the root client.
384+
let includeRootSlashOption: unknown;
385+
let current: SdkClientType<SdkHttpOperation> | undefined = rootClient;
386+
while (current) {
387+
includeRootSlashOption = getClientOptions(current, "includeRootSlash");
388+
if (includeRootSlashOption !== undefined) break;
389+
current = current.parent;
390+
}
391+
const includeRootSlash = includeRootSlashOption !== false;
383392

384393
const result = {
385394
url: includeRootSlash ? operation.path : operation.path.replace(/^\//, ""),

0 commit comments

Comments
 (0)