[python] add supportHttpxSync option for sync httpx methods (#23032)

Closed
AN
5mo ago
+53117-0

This Pull Request aims to help @soapun in implementation of httpx Sync in open api codegen.

Add a new supportHttpxSync option to the Python generator (httpx library only) that generates synchronous _sync variants of each API method inside the same API class, instead of a separate httpx-sync library.

Following the maintainer's review feedback on #23044, each generated _sync method simply calls its asynchronous counterpart and waits for completion, so both synchronous and asynchronous methods are available from the same SDK (matching the sync/async layout already used by other generators).

  • PythonClientCodegen: new supportHttpxSync CLI option, wired for the httpx library only (ignored with a warning otherwise)
  • httpx/sync_helper.mustache: run_sync() helper running coroutines to completion on a dedicated, reused background event loop so the httpx AsyncClient stays bound to a single loop across calls
  • api.mustache: generate _sync, _sync_with_http_info and _sync_without_preload_content variants under {{#supportHttpxSync}}
  • api_doc / api_test: document and stub the sync variants
  • new sample petstore python-httpx-sync (bin/configs/python-httpx-sync.yaml)
  • docs/generators/python.md regenerated

PR checklist

  • [ X ] Read the contribution guidelines.
  • [ X ] Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • [ X ] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) @multani (2023/10)


Summary by cubic

Add supportHttpxSync to the Python httpx generator to produce synchronous _sync method variants alongside existing async methods. The sync methods call their async counterparts via a reusable background event loop so the httpx.AsyncClient stays bound to one loop across calls.

  • New Features

    • New CLI option supportHttpxSync (for httpx only; ignored with a warning otherwise).
    • Generates _sync, _sync_with_http_info, and _sync_without_preload_content methods using sync_helper.run_sync.
    • Added httpx/sync_helper.mustache; updated api.mustache, api_doc.mustache, api_test.mustache; updated docs (docs/generators/python.md).
    • Added sample python-httpx-sync and config bin/configs/python-httpx-sync.yaml.
  • Bug Fixes

    • Corrected the generated .openapi-generator/FILES listing in the python-httpx-sync sample.