microsoftgraph / microsoftgraph/msgraph-sdk-python-core

BatchRequestBuilder doesn't copy headers from BatchRequestContent on post

Đang mở
#831 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

P1 type:bug
Ngôn ngữ chính
Python
Star
288
Fork
52
Merge trung bình
8 giờ 10 phút
Pull request đã merge (30 ngày)
1

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Đọc src/msgraph_core/requests/batch_request_builder.py, đặc biệt là phương thức to_post_request_information quanh các dòng 126-135, và theo dõi cách BatchRequestContent được truyền vào post. Chạy bản tái hiện batch được cung cấp với header ConsistencyLevel; được xem là hoàn tất khi yêu cầu POST được tạo ra giữ nguyên header đó và không còn trả về lỗi 400 đã được báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
api, backend
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.