microsoftgraph / microsoftgraph/msgraph-sdk-python-core

BatchRequestBuilder doesn't copy headers from BatchRequestContent on post

未关闭
#831 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

P1 type:bug
主要语言
Python
星标
288
派生
52
平均合并
8 小时 10 分钟
30 天内合并 PR
1

描述

Describe the bug

I'm trying to send a batch request from UsersRequestBuilder and GroupsRequestBuilder objects that use the ConsistencyLevel: eventual header. The RequestInformation objects loaded in the BatchRequestContent include the header, but then when calling to the post method of the BatchRequestBuilder it doesn't copy the headers, just creates the Content-Type header, resulting on a 400 error from the server.

The bug itself is in the following lines:

https://github.com/microsoftgraph/msgraph-sdk-python-core/blob/df7bdbfb7e4268df10606f2b35623ecff559fe00/src/msgraph_core/requests/batch_request_builder.py#L126-L135

Expected behavior

The to_post_request_information method from BatchRequestBuilder should copy the headers from the provided BatchRequestContent object

How to reproduce

You can reproduce it using the following snippet

import base64

from typing import Optional

from azure.keyvault.secrets import SecretClient
from azure.identity import CertificateCredential, ManagedIdentityCredential
from kiota_abstractions.base_request_configuration import RequestConfiguration
from msgraph.generated.users.users_request_builder import UsersRequestBuilder
from msgraph.graph_service_client import GraphServiceClient
from msgraph_core.requests.batch_request_builder import BatchRequestBuilder
from msgraph_core.requests.batch_request_content import BatchRequestContent
from msgraph_core.requests.batch_request_item import BatchRequestItem


def get_tenant_credential(tenant_id, client_id, cert_name):
    url = "https://<YOUR VAULT URL>.vault.azure.net/"
    credential = ManagedIdentityCredential(
        client_id="<YOUR CLIENT ID>"
    )
    key_vault_client = SecretClient(vault_url=url, credential=credential)
    secret = key_vault_client.get_secret(cert_name).value
    cert_bytes = base64.b64decode(secret)
    return CertificateCredential(
        tenant_id=tenant_id,
        client_id=client_id,
        certificate_data=cert_bytes,
        send_certificate_chain=True
    )


def get_graph_client():
    tenant = "<YOUR TENANT>"
    client_id = "<YOUR GRAPH CLIENT ID>"
    cert_name = "<YOUR GRAPH CERT NAME>"
    credential = get_tenant_credential(tenant, client_id, cert_name)
    scopes = ["https://graph.microsoft.com/.default"]
    return GraphServiceClient(credential, scopes)


def build_query_filters(
    domains: list[str],
    fields: Optional[list[str]] = None,
    search_term: Optional[str] = None,
    excluded_ids: Optional[set[str]] = None,
) -> str:
    filter_list = [f"endswith(mail, '@{d}')" for d in domains]
    filters = f"({' or '.join(filter_list)})"

    if search_term:
        if fields is None:
            fields = ['displayName', 'mail']

        search_filter_list = [f"startswith({f}, '{search_term}')"
                              for f in fields]
        search_filters = f"({' or '.join(search_filter_list)})"
        filters = f"{filters} and {search_filters}"

    if excluded_ids:
        exclusion_list = [f"id ne '{i}'" for i in excluded_ids]
        excluded_filter = f"{' and '.join(exclusion_list)}"
        filters = f"{filters} and {excluded_filter}"
    return filters


def build_users_request_config(
    search_term: Optional[str] = None,
    n_items: Optional[int] = None,
    excluded_ids: Optional[set[str]] = None,
) -> RequestConfiguration:
    domains = ["microsoft.com", "linkedin.biz", "*.microsoft.com"]
    filters = build_query_filters(
        domains,
        search_term=search_term,
        excluded_ids=excluded_ids,
    )
    query_params = UsersRequestBuilder.UsersRequestBuilderGetQueryParameters(
        select=['id', 'displayName', 'mail'],
        filter=filters,
        top=n_items,
        count=True,
    )
    request_config = RequestConfiguration(query_parameters=query_params)
    request_config.headers.add("ConsistencyLevel", "eventual")
    return request_config


async def search(search_term: str, n_items: int = 5):
    client = get_graph_client()
    request_adapter = client.request_adapter

    # Build user request batch item
    user_request_config = build_users_request_config(search_term, n_items)
    user_request_builder = UsersRequestBuilder(request_adapter, {})
    user_request_info = user_request_builder.to_get_request_information(
        user_request_config
    )
    user_request_item = BatchRequestItem(user_request_info)

    # Build group request batch item
    # group_request_config = build_groups_request_config(
    #     search_term, n_items
    # )
    # group_request_builder = GroupsRequestBuilder(request_adapter, {})
    # group_request_info = group_request_builder.to_get_request_information(
    #     group_request_config
    # )
    # group_request_item = BatchRequestItem(group_request_info)

    # Create batch request content
    batch_request_content = BatchRequestContent({
        user_request_item.id: user_request_item,
        # group_request_item.id: group_request_item
    })

    batch_request_builder = BatchRequestBuilder(request_adapter)
    batch_result = await batch_request_builder.post(batch_request_content)
    return batch_result


if __name__ == "__main__":
    import asyncio

    search_term = "test"
    n_items = 5
    loop = asyncio.get_event_loop()
    results = loop.run_until_complete(search(search_term, n_items))
    print(results)
SDK Version

1.15.0

Latest version known to work for scenario above?

No response

Known Workarounds

There's no workaround possible, given the request information is created in the same method that send the post

Debug output

ERROR:root:API Error:
APIError
Code: 400
message: The server returned an unexpected status code and no error class is registered for this code 400

Configuration

No response

Other information

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 src/msgraph_core/requests/batch_request_builder.py,尤其是第126-135行附近的 to_post_request_information 方法,并跟踪 BatchRequestContent 如何传递给 post。使用 ConsistencyLevel 标头运行提供的批处理复现;当生成的 POST 请求保留该标头且不再返回报告的 400 错误时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
api, backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。