-
Notifications
You must be signed in to change notification settings - Fork 70
Remove requests dependency in favor of urllib3 #1653
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
Draft
woodruffw
wants to merge
4
commits into
main
Choose a base branch
from
ww/urllib3
base: main
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.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| # Copyright 2022 The Sigstore Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| HTTP client utilities for sigstore-python using urllib3. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import Any | ||
|
|
||
| import urllib3 | ||
|
|
||
| from sigstore import __version__ as sigstore_version | ||
|
|
||
| # Global PoolManager for all HTTP requests | ||
| _pool_manager: urllib3.PoolManager | None = None | ||
|
|
||
| # User-Agent header for all requests | ||
| USER_AGENT = f"sigstore-python/{sigstore_version} (urllib3/{urllib3.__version__})" # type: ignore[attr-defined] | ||
|
|
||
|
|
||
| def _get_pool_manager() -> urllib3.PoolManager: | ||
| """ | ||
| Get or create the global PoolManager instance. | ||
|
|
||
| Returns: | ||
| The global urllib3.PoolManager instance. | ||
| """ | ||
| global _pool_manager | ||
| if _pool_manager is None: | ||
| _pool_manager = urllib3.PoolManager( | ||
| headers={"User-Agent": USER_AGENT}, | ||
| timeout=urllib3.Timeout(connect=30.0, read=30.0), | ||
| ) | ||
| return _pool_manager | ||
|
|
||
|
|
||
| class HTTPError(Exception): | ||
| """ | ||
| Represents an HTTP error response. | ||
| """ | ||
|
|
||
| def __init__(self, status: int, reason: str, body: str | None = None): | ||
| """ | ||
| Create a new HTTPError. | ||
|
|
||
| Args: | ||
| status: HTTP status code | ||
| reason: HTTP status reason phrase | ||
| body: Optional response body | ||
| """ | ||
| self.status = status | ||
| self.reason = reason | ||
| self.body = body | ||
| super().__init__(f"HTTP {status}: {reason}") | ||
|
|
||
|
|
||
| class HTTPResponse: | ||
| """ | ||
| Wrapper around urllib3 HTTPResponse for easier usage. | ||
| """ | ||
|
|
||
| def __init__(self, response: urllib3.BaseHTTPResponse): | ||
| """ | ||
| Create a new HTTPResponse. | ||
|
|
||
| Args: | ||
| response: The underlying urllib3.HTTPResponse | ||
| """ | ||
| self._response = response | ||
| self.status_code = response.status | ||
| self.reason = response.reason | ||
| self._data = response.data | ||
|
|
||
| def raise_for_status(self) -> None: | ||
| """ | ||
| Raise an HTTPError if the response status indicates an error. | ||
|
|
||
| Raises: | ||
| HTTPError: If status code is 4xx or 5xx | ||
| """ | ||
| if 400 <= self.status_code < 600: | ||
| raise HTTPError(self.status_code, self.reason or "", self.text) | ||
|
|
||
| @property | ||
| def text(self) -> str: | ||
| """ | ||
| Get the response body as text. | ||
|
|
||
| Returns: | ||
| The response body decoded as UTF-8 | ||
| """ | ||
| return self._response.data.decode("utf-8") | ||
|
|
||
| def json(self) -> Any: | ||
| """ | ||
| Parse the response body as JSON. | ||
|
|
||
| Returns: | ||
| The parsed JSON data | ||
| """ | ||
| return self._response.json() | ||
|
|
||
|
|
||
| def request( | ||
| method: str, | ||
| url: str, | ||
| *, | ||
| headers: dict[str, str] | None = None, | ||
| json_data: Any | None = None, | ||
| data: bytes | None = None, | ||
| params: dict[str, Any] | None = None, | ||
| timeout: float | None = None, | ||
| ) -> HTTPResponse: | ||
| """ | ||
| Make an HTTP request using the global PoolManager. | ||
|
|
||
| Args: | ||
| method: HTTP method (GET, POST, etc.) | ||
| url: URL to request | ||
| headers: Optional additional headers | ||
| json_data: Optional JSON data to send (will be serialized) | ||
| data: Optional raw bytes to send | ||
| params: Optional query parameters | ||
| timeout: Optional timeout in seconds | ||
|
|
||
| Returns: | ||
| HTTPResponse object | ||
|
|
||
| Raises: | ||
| urllib3.exceptions.HTTPError: On connection errors | ||
| HTTPError: On HTTP error status codes (if raise_for_status is called) | ||
| """ | ||
| pool = _get_pool_manager() | ||
|
|
||
| # Build request headers | ||
| request_headers = {} | ||
| if json_data is not None: | ||
| request_headers["Content-Type"] = "application/json" | ||
| data = json.dumps(json_data).encode("utf-8") | ||
| if headers: | ||
| request_headers.update(headers) | ||
|
|
||
| # Build fields for query parameters | ||
| fields = None | ||
| if params: | ||
| fields = params | ||
|
|
||
| # Create timeout object if specified | ||
| timeout_obj = None | ||
| if timeout is not None: | ||
| timeout_obj = urllib3.Timeout(connect=timeout, read=timeout) | ||
|
Member
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. I don't know if this changes anything in practice but in urllib3 the timeout argument default value is not That said, maybe we should set an actual timeout default here to make it clear? |
||
|
|
||
| response = pool.request( | ||
| method, | ||
| url, | ||
| headers=request_headers, | ||
| body=data, | ||
| fields=fields if method.upper() == "GET" else None, | ||
| timeout=timeout_obj, | ||
| ) | ||
|
|
||
| return HTTPResponse(response) | ||
|
|
||
|
|
||
| def get(url: str, **kwargs: Any) -> HTTPResponse: | ||
| """ | ||
| Make a GET request. | ||
|
|
||
| Args: | ||
| url: URL to request | ||
| **kwargs: Additional arguments to pass to request() | ||
|
|
||
| Returns: | ||
| HTTPResponse object | ||
| """ | ||
| return request("GET", url, **kwargs) | ||
|
|
||
|
|
||
| def post(url: str, **kwargs: Any) -> HTTPResponse: | ||
| """ | ||
| Make a POST request. | ||
|
|
||
| Args: | ||
| url: URL to request | ||
| **kwargs: Additional arguments to pass to request() | ||
|
|
||
| Returns: | ||
| HTTPResponse object | ||
| """ | ||
| return request("POST", url, **kwargs) | ||
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.
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.
fields seems like a useless variable here?