Skip to content

Commit

Permalink
More porting from from requests to urllib3
Browse files Browse the repository at this point in the history
This is related to theupdateframework#2762 (that replaces RequestsFetcher with
Urllib3Fetcher) and takes care of the remaining requests use cases in
the code base.

Signed-off-by: Jussi Kukkonen <[email protected]>
  • Loading branch information
jku committed Feb 14, 2025
1 parent d42426f commit 899c676
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
11 changes: 6 additions & 5 deletions examples/uploader/_localrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import os
from datetime import datetime, timedelta, timezone

import requests
from securesystemslib.signer import CryptoSigner, Signer
from urllib3 import request

from tuf.api.exceptions import RepositoryError
from tuf.api.metadata import Metadata, MetaFile, TargetFile, Targets
Expand Down Expand Up @@ -92,8 +92,9 @@ def close(self, role_name: str, md: Metadata) -> None:

# Upload using "api/role"
uri = f"{self.base_url}/api/role/{role_name}"
r = requests.post(uri, data=md.to_bytes(JSONSerializer()), timeout=5)
r.raise_for_status()
r = request("POST", uri, body=md.to_bytes(JSONSerializer()), timeout=5)
if r.status != 200:
raise RuntimeError(f"HTTP error {r.status}")

def add_target(self, role: str, targetpath: str) -> bool:
"""Add target to roles metadata and submit new metadata version"""
Expand Down Expand Up @@ -124,8 +125,8 @@ def add_delegation(self, role: str) -> bool:

data = {signer.public_key.keyid: signer.public_key.to_dict()}
url = f"{self.base_url}/api/delegation/{role}"
r = requests.post(url, data=json.dumps(data), timeout=5)
if r.status_code != 200:
r = request("POST", url, body=json.dumps(data), timeout=5)
if r.status != 200:
print(f"delegation failed with {r}")
return False

Expand Down
2 changes: 1 addition & 1 deletion tuf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

"""TUF."""

# This value is used in the requests user agent.
# This value is used in the ngclient user agent.
__version__ = "5.1.0"
17 changes: 8 additions & 9 deletions verify_release
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on GitHub and PyPI match the built release artifacts.
"""

import argparse
import json
import os
import subprocess
import sys
Expand All @@ -20,10 +19,10 @@ from typing import Optional

try:
import build as _ # type: ignore[import-not-found] # noqa: F401
import requests
from urllib3 import request
except ImportError:
print("Error: verify_release requires modules 'requests' and 'build':")
print(" pip install requests build")
print("Error: verify_release requires modules 'urllib3' and 'build':")
print(" pip install urllib3 build")
sys.exit(1)

# Project variables
Expand Down Expand Up @@ -75,9 +74,7 @@ def get_git_version() -> str:
def get_github_version() -> str:
"""Return version string of latest GitHub release"""
release_json = f"https://api.github.com/repos/{GITHUB_ORG}/{GITHUB_PROJECT}/releases/latest"
releases = json.loads(
requests.get(release_json, timeout=HTTP_TIMEOUT).content
)
releases = request("GET", release_json, timeout=HTTP_TIMEOUT).json()
return releases["tag_name"][1:]


Expand Down Expand Up @@ -106,9 +103,11 @@ def verify_github_release(version: str, compare_dir: str) -> bool:
with TemporaryDirectory() as github_dir:
for filename in [tar, wheel]:
url = f"{base_url}/v{version}/{filename}"
response = requests.get(url, stream=True, timeout=HTTP_TIMEOUT)
response = request(
"GET", url, preload_content=False, timeout=HTTP_TIMEOUT
)
with open(os.path.join(github_dir, filename), "wb") as f:
for data in response.iter_content():
for data in response.stream():
f.write(data)

return cmp(
Expand Down

0 comments on commit 899c676

Please sign in to comment.