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 摘要。