Azure / Azure/azure-sdk-for-python

Add HttpxTransport support for SDK

Open
#41,382 4 comments 3 reactions 1 assignee Claimed by @xiangyan99 View on GitHub
feature-request
Dominant language
Python
Stars
5.6k
Forks
3.4k
Avg merge
1d 21h
Merged PRs (30d)
193

Description

# Context

Today, we do not pre-installed any async http libs, and if people try they get this:
> ImportError: aiohttp package is not installed

This prompt people to install `aiohttp` to execute async code. While `aiohttp` is installed, we automatically loads it by default:
https://github.com/Azure/azure-sdk-for-python/blob/9e75dc54f8f39ca210f04438aaf0f4302dc82b37/sdk/core/azure-core/azure/core/_pipeline_client_async.py#L258-L262

I'd like to add `httpx` in the mix, and make it the recommendation for async.

# Proposal

- Add `HttpxTransport` to `azure-core`
- Make the default behavior as follow:
- If no async transport found, raise an ImportError about `httpx`
- If *only* httpx is found, load it as async transport
- If `aiohttp` is found (with or without `httpx` installed), use it as async transport
- Add a setting in azure.core settings called `default_sync_transport` and `default_async_transport`
- We can set it for `httpx` to make it the default independently of `requests` or `aiohttp` being installed
- We accept an instance, in which case this instance becomes the default transport of all SDK. This helps customers pass their own transport if they want to, without having to use `transport=` in all SDK.
- (Optional) Let's add transport information in UserAgent to monitor what is happening and get numbers

# Notes

- The only behavioral change, is that if `httpx` is installed, instead of raising an exception that async is not available, not it works with `httpx`. That's not a breaking change in my book, I don't see how this could break anyone that except their code to raise.
- We can discuss longer term to shift the default
- This gives an easy escape hatch in case of troubles
- Our transport layer is supposed to hide implementation details, therefore I don't consider the fact that `httpx` is not GA as a problem. As far as I am aware, OpenAI is doing just fine.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.