Repository navigation
Feature: add httpx2 support with httpx fallback #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CoderJoshDK
wants to merge
4
commits into
yanyongyu:master
Choose a base branch
from
CoderJoshDK:httpx2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,12 @@ | ||
| # Configuration | ||
|
|
||
| githubkit is highly configurable. You can customize its behavior by passing keyword arguments directly to the `GitHub` constructor: | ||
| githubkit is highly configurable. | ||
|
|
||
| GitHubKit installs HTTPX by default to preserve compatibility with existing transports. It prefers [HTTPX2](https://pydantic.dev/docs/httpx2/) when you install it separately. The default HTTPX path remains supported, but emits a `DeprecationWarning`. To opt in, run `pip install httpx2`, then use `import httpx2` and `httpx2.*` as in the examples below. If you remain on HTTPX, use `import httpx` and `httpx.*` instead. | ||
|
|
||
| Custom timeouts, URLs, proxies, transports, and responses must come from the selected package: HTTPX2 when installed, or HTTPX on the deprecated fallback path. Objects from the two packages are not interchangeable. HTTPX2 uses the operating system's trust store by default instead of HTTPX's bundled CA certificates; its logger names are `httpx2` and `httpcore2.*`. | ||
|
Comment on lines
+5
to
+7
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section can be explained to the user by adding admonition block below the original paragraph. |
||
|
|
||
| You can customize GitHubKit's behavior by passing keyword arguments directly to the `GitHub` constructor: | ||
|
|
||
| ```python | ||
| from githubkit import GitHub | ||
|
|
@@ -30,7 +36,7 @@ github = GitHub( | |
| Alternatively, you can build a `Config` object and pass it via the `config` parameter. This is useful when you want to share the same configuration across multiple `GitHub` instances: | ||
|
|
||
| ```python | ||
| import httpx | ||
| import httpx2 | ||
| from githubkit import GitHub, Config | ||
| from githubkit.retry import RETRY_DEFAULT | ||
| from githubkit.cache import DEFAULT_CACHE_STRATEGY | ||
|
|
@@ -40,7 +46,7 @@ config = Config( | |
| accept="application/vnd.github+json", | ||
| user_agent="GitHubKit/Python", | ||
| follow_redirects=True, | ||
| timeout=httpx.Timeout(None), | ||
| timeout=httpx2.Timeout(None), | ||
| ssl_verify=True, | ||
| trust_env=True, | ||
| proxy=None, | ||
|
|
@@ -103,16 +109,16 @@ Whether to automatically follow HTTP redirects (3xx responses). Enabled by defau | |
|
|
||
| ### `timeout` | ||
|
|
||
| The request timeout. Accepts a `float` (seconds), an `httpx.Timeout` object for fine-grained control, or `None` for no timeout (default). See [HTTPX Timeouts](https://www.python-httpx.org/advanced/timeouts/) for details. | ||
| The request timeout. Accepts a `float` (seconds), an `httpx2.Timeout` object for fine-grained control, or `None` for no timeout (default). See [HTTPX2 Timeouts](https://pydantic.dev/docs/httpx2/advanced/timeouts/) for details. | ||
|
|
||
| ```python | ||
| import httpx | ||
| import httpx2 | ||
|
|
||
| # Simple: 10 second timeout for all operations | ||
| github = GitHub(timeout=10.0) | ||
|
|
||
| # Fine-grained: different timeouts for connect vs. read | ||
| github = GitHub(timeout=httpx.Timeout(5.0, read=30.0)) | ||
| github = GitHub(timeout=httpx2.Timeout(5.0, read=30.0)) | ||
| ``` | ||
|
|
||
| ### `ssl_verify` | ||
|
|
@@ -123,76 +129,76 @@ Controls SSL certificate verification. Defaults to `True`. | |
| - `False` — **disable** SSL verification (not recommended for production). | ||
| - `ssl.SSLContext` — provide a custom SSL context for advanced use cases. | ||
|
|
||
| See [HTTPX SSL](https://www.python-httpx.org/advanced/ssl/) for details. | ||
| See [HTTPX2 SSL](https://pydantic.dev/docs/httpx2/advanced/ssl/) for details. | ||
|
|
||
| ### `trust_env` | ||
|
|
||
| When `True` (default), githubkit (via HTTPX) reads environment variables such as `HTTP_PROXY`, `HTTPS_PROXY`, and `SSL_CERT_FILE` to configure proxies and SSL. Set to `False` to ignore these variables. | ||
| When `True` (default), githubkit (via HTTPX2) reads environment variables such as `HTTP_PROXY`, `HTTPS_PROXY`, and `SSL_CERT_FILE` to configure proxies and SSL. Set to `False` to ignore these variables. | ||
|
|
||
| ### `proxy` | ||
|
|
||
| Sets a proxy URL for all requests. Accepts a string, `httpx.URL`, or `httpx.Proxy` object. | ||
| Sets a proxy URL for all requests. Accepts a string, `httpx2.URL`, or `httpx2.Proxy` object. | ||
|
|
||
| ```python | ||
| github = GitHub(proxy="http://proxy.example.com:8080") | ||
| ``` | ||
|
|
||
| See [HTTPX Proxies](https://www.python-httpx.org/advanced/proxies/) for more details. | ||
| See [HTTPX2 Proxies](https://pydantic.dev/docs/httpx2/advanced/proxies/) for more details. | ||
|
|
||
| !!! note | ||
|
|
||
| If `trust_env` is `True` and no `proxy` is set, githubkit respects the `HTTP_PROXY` / `HTTPS_PROXY` / `ALL_PROXY` environment variables. | ||
|
|
||
| ### `transport`, `async_transport` | ||
|
|
||
| Provide custom [HTTPX transports](https://www.python-httpx.org/advanced/transports/) to replace the default networking layer. This is useful for: | ||
| Provide custom [HTTPX2 transports](https://pydantic.dev/docs/httpx2/advanced/transports/) to replace the default networking layer. This is useful for: | ||
|
|
||
| - **Unit testing** — inject `httpx.MockTransport` to stub API responses without making real HTTP calls. | ||
| - **Unit testing** — inject `httpx2.MockTransport` to stub API responses without making real HTTP calls. | ||
| - **Custom networking** — use alternative transport implementations (e.g., HTTP/3, Unix sockets). | ||
|
|
||
| | Option | Type | Used for | | ||
| | ----------------- | -------------------------- | -------------- | | ||
| | `transport` | `httpx.BaseTransport` | Sync requests | | ||
| | `async_transport` | `httpx.AsyncBaseTransport` | Async requests | | ||
| | `transport` | `httpx2.BaseTransport` | Sync requests | | ||
| | `async_transport` | `httpx2.AsyncBaseTransport` | Async requests | | ||
|
|
||
| ```python | ||
| import httpx | ||
| import httpx2 | ||
|
|
||
|
|
||
| def mock_handler(request: httpx.Request) -> httpx.Response: | ||
| return httpx.Response(200, json={"login": "octocat"}) | ||
| def mock_handler(request: httpx2.Request) -> httpx2.Response: | ||
| return httpx2.Response(200, json={"login": "octocat"}) | ||
|
|
||
|
|
||
| github = GitHub(transport=httpx.MockTransport(mock_handler)) | ||
| github = GitHub(transport=httpx2.MockTransport(mock_handler)) | ||
| ``` | ||
|
|
||
| !!! warning | ||
|
|
||
| When a custom transport is provided, proxy-related environment variables (`HTTP_PROXY`, etc.) have no effect. Set `transport` / `async_transport` to `None` (default) to use HTTPX's built-in transport. | ||
| When a custom transport is provided, proxy-related environment variables (`HTTP_PROXY`, etc.) have no effect. Set `transport` / `async_transport` to `None` (default) to use HTTPX2's built-in transport. | ||
|
|
||
| ### `event_hooks`, `async_event_hooks` | ||
|
|
||
| Register [HTTPX event hooks](https://www.python-httpx.org/advanced/event-hooks/) that run on every request and/or response. This is useful for logging, injecting headers, collecting metrics, or raising on error status codes — without modifying your business logic. | ||
| Register [HTTPX2 event hooks](https://pydantic.dev/docs/httpx2/advanced/event-hooks/) that run on every request and/or response. This is useful for logging, injecting headers, collecting metrics, or raising on error status codes — without modifying your business logic. | ||
|
|
||
| | Option | Hook signatures | Used for | | ||
| | ------------------- | ------------------------------------------------------ | -------------- | | ||
| | `event_hooks` | `def hook(request)` / `def hook(response)` | Sync requests | | ||
| | `async_event_hooks` | `async def hook(request)` / `async def hook(response)` | Async requests | | ||
|
|
||
| Both options accept a dictionary mapping event names (`"request"`, `"response"`) to a list of callables. Each callable receives an `httpx.Request` or `httpx.Response` object respectively. | ||
| Both options accept a dictionary mapping event names (`"request"`, `"response"`) to a list of callables. Each callable receives an `httpx2.Request` or `httpx2.Response` object respectively. | ||
|
|
||
| === "Sync" | ||
|
|
||
| ```python | ||
| import httpx | ||
| import httpx2 | ||
| from githubkit import GitHub | ||
|
|
||
|
|
||
| def log_request(request: httpx.Request) -> None: | ||
| def log_request(request: httpx2.Request) -> None: | ||
| print(f"-> {request.method} {request.url}") | ||
|
|
||
|
|
||
| def log_response(response: httpx.Response) -> None: | ||
| def log_response(response: httpx2.Response) -> None: | ||
| print(f"<- {response.status_code}") | ||
|
|
||
|
|
||
|
|
@@ -207,15 +213,15 @@ Both options accept a dictionary mapping event names (`"request"`, `"response"`) | |
| === "Async" | ||
|
|
||
| ```python | ||
| import httpx | ||
| import httpx2 | ||
| from githubkit import GitHub | ||
|
|
||
|
|
||
| async def log_request(request: httpx.Request) -> None: | ||
| async def log_request(request: httpx2.Request) -> None: | ||
| print(f"-> {request.method} {request.url}") | ||
|
|
||
|
|
||
| async def log_response(response: httpx.Response) -> None: | ||
| async def log_response(response: httpx2.Response) -> None: | ||
| print(f"<- {response.status_code}") | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from typing import TYPE_CHECKING | ||
| import warnings | ||
|
|
||
| if TYPE_CHECKING: | ||
| from hishel.httpx2 import AsyncCacheTransport as AsyncCacheTransport | ||
| from hishel.httpx2 import SyncCacheTransport as SyncCacheTransport | ||
| import httpx2 as httpx | ||
| else: | ||
| try: | ||
| from hishel.httpx2 import AsyncCacheTransport, SyncCacheTransport | ||
| import httpx2 as httpx | ||
| except ImportError: | ||
| # Hishel wraps a missing HTTP client in ImportError. | ||
| try: | ||
| from hishel.httpx import AsyncCacheTransport as AsyncCacheTransport | ||
| from hishel.httpx import SyncCacheTransport as SyncCacheTransport | ||
| import httpx as httpx | ||
| except ImportError: | ||
| raise RuntimeError( | ||
| "GitHubKit requires httpx2 and its Hishel integration " | ||
| "to be installed.\n" | ||
| "You can install them with:\n" | ||
| ' $ pip install httpx2 "hishel[async,httpx2]>=1.4.0"\n' | ||
| ) from None | ||
| else: | ||
| warnings.warn( | ||
| "Using `httpx` with GitHubKit is deprecated; install `httpx2` " | ||
| "and `hishel[async,httpx2]>=1.4.0` instead.", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
|
CoderJoshDK marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import re | ||
|
|
||
| import httpx | ||
| from githubkit._httpx import httpx | ||
|
|
||
| APP_ROUTES = { | ||
| r"/app", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be achieved by adding an environment variable within githubkit to force the use of
httpx. FYI, like this.