(
lines: string[],
groupName: string,
method: RpcMethod,
resolveType: (name: string) => string
)
| 3673 | } |
| 3674 | |
| 3675 | function emitClientSessionRegistrationMethod( |
| 3676 | lines: string[], |
| 3677 | groupName: string, |
| 3678 | method: RpcMethod, |
| 3679 | resolveType: (name: string) => string |
| 3680 | ): void { |
| 3681 | const rpcSegments = method.rpcMethod.split("."); |
| 3682 | const handlerVariableName = `handle_${rpcSegments.map(toSnakeCase).join("_")}`; |
| 3683 | const paramsType = resolveType(pythonParamsTypeName(method)); |
| 3684 | const resultSchema = getMethodResultSchema(method); |
| 3685 | const nullableInner = resultSchema ? getNullableInner(resultSchema) : undefined; |
| 3686 | const hasResult = !isVoidSchema(resultSchema) && !nullableInner; |
| 3687 | const handlerField = toSnakeCase(groupName); |
| 3688 | const handlerMethod = clientSessionHandlerMethodName(method.rpcMethod); |
| 3689 | |
| 3690 | lines.push(` async def ${handlerVariableName}(params: dict) -> dict | None:`); |
| 3691 | lines.push(` request = ${paramsType}.from_dict(params)`); |
| 3692 | lines.push(` handler = get_handlers(request.session_id).${handlerField}`); |
| 3693 | lines.push( |
| 3694 | ` if handler is None: raise RuntimeError(f"No ${handlerField} handler registered for session: {request.session_id}")` |
| 3695 | ); |
| 3696 | if (hasResult) { |
| 3697 | lines.push(` result = await handler.${handlerMethod}(request)`); |
| 3698 | if (isObjectSchema(resultSchema)) { |
| 3699 | lines.push(` return result.to_dict()`); |
| 3700 | } else { |
| 3701 | lines.push(` return result.value if hasattr(result, 'value') else result`); |
| 3702 | } |
| 3703 | } else if (nullableInner) { |
| 3704 | lines.push(` result = await handler.${handlerMethod}(request)`); |
| 3705 | const resolvedInner = resolveSchema(nullableInner, rpcDefinitions) ?? nullableInner; |
| 3706 | if (isObjectSchema(resolvedInner) || nullableInner.$ref) { |
| 3707 | lines.push(` return result.to_dict() if result is not None else None`); |
| 3708 | } else { |
| 3709 | lines.push(` return result`); |
| 3710 | } |
| 3711 | } else { |
| 3712 | lines.push(` await handler.${handlerMethod}(request)`); |
| 3713 | lines.push(` return None`); |
| 3714 | } |
| 3715 | lines.push(` client.set_request_handler("${method.rpcMethod}", ${handlerVariableName})`); |
| 3716 | } |
| 3717 | |
| 3718 | function emitClientGlobalApiRegistration( |
| 3719 | lines: string[], |
no test coverage detected
searching dependent graphs…