Skip to content

Commit 2eaad90

Browse files
committed
More porting from from requests to urllib3
This is related to theupdateframework#2762 (that aims to replace RequestsFetcher with Urllib3Fetcher) and takes care of the remaining requests use cases in the code base. Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent 554f508 commit 2eaad90

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

examples/uploader/_localrepo.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import os
1313
from datetime import datetime, timedelta, timezone
1414

15-
import requests
1615
from securesystemslib.signer import CryptoSigner, Signer
16+
from urllib3 import request
1717

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

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

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

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

verify_release

+8-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ on GitHub and PyPI match the built release artifacts.
1010
"""
1111

1212
import argparse
13-
import json
1413
import os
1514
import subprocess
1615
import sys
@@ -20,10 +19,10 @@ from typing import Optional
2019

2120
try:
2221
import build as _ # type: ignore[import-not-found] # noqa: F401
23-
import requests
22+
from urllib3 import request
2423
except ImportError:
25-
print("Error: verify_release requires modules 'requests' and 'build':")
26-
print(" pip install requests build")
24+
print("Error: verify_release requires modules 'urllib3' and 'build':")
25+
print(" pip install urllib3 build")
2726
sys.exit(1)
2827

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

8380

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

114113
return cmp(

0 commit comments

Comments
 (0)