Skip to content

Commit 0187189

Browse files
committed
Eliminate uses of requests library in src/azul/service/drs_controller.py (#7633)
1 parent befcb9d commit 0187189

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

src/azul/service/drs_controller.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from datetime import (
1313
datetime,
1414
)
15+
import logging
1516
from typing import (
1617
Any,
1718
)
@@ -27,7 +28,7 @@
2728
from more_itertools import (
2829
one,
2930
)
30-
import requests
31+
import urllib3
3132

3233
from azul import (
3334
config,
@@ -38,6 +39,9 @@
3839
drs_object_uri,
3940
drs_object_url_path,
4041
)
42+
from azul.http import (
43+
HasCachedHttpClient,
44+
)
4145
from azul.lib import (
4246
cached_property,
4347
mutable_furl,
@@ -60,8 +64,10 @@
6064
IndexService,
6165
)
6266

67+
log = logging.getLogger(__name__)
68+
6369

64-
class DRSController(ServiceController):
70+
class DRSController(ServiceController, HasCachedHttpClient):
6571

6672
@cached_property
6773
def _service(self) -> IndexService:
@@ -205,22 +211,22 @@ def get_object(self, file_uuid, query_params):
205211
# We only want direct URLs for Google
206212
extra_params = dict(query_params, directurl=access_method.replica == 'gcp')
207213
response = self._dss_get_file(file_uuid, access_method.replica, **extra_params)
208-
if response.status_code == 301:
214+
if response.status == 301:
209215
retry_url = response.headers['location']
210216
query = urllib.parse.urlparse(retry_url).query
211217
query = urllib.parse.parse_qs(query, strict_parsing=True)
212218
token = one(query['token'])
213219
# We use the encoded token string as the key for our access ID.
214220
access_id = encode_access_id(token, access_method.replica)
215221
drs_object.add_access_method(access_method, access_id=access_id)
216-
elif response.status_code == 302:
222+
elif response.status == 302:
217223
retry_url = response.headers['location']
218224
if access_method.replica == 'gcp':
219225
assert retry_url.startswith('gs:')
220226
drs_object.add_access_method(access_method, url=retry_url)
221227
else:
222228
# For errors, just proxy DSS response
223-
return Response(response.text, status_code=response.status_code)
229+
return Response(response.data, status_code=response.status)
224230
return Response(drs_object.to_json())
225231

226232
def get_object_access(self, access_id, file_uuid, query_params):
@@ -238,24 +244,29 @@ def get_object_access(self, access_id, file_uuid, query_params):
238244
'directurl': replica == 'gcp',
239245
'token': token
240246
})
241-
if response.status_code == 301:
242-
headers = {'retry-after': response.headers['retry-after']}
247+
if response.status == 301:
248+
headers: dict[str, str | list[str]] = {
249+
'retry-after':
250+
response.headers['retry-after']
251+
}
243252
# DRS says no body for 202 responses
244253
return Response(body='', status_code=202, headers=headers)
245-
elif response.status_code == 302:
254+
elif response.status == 302:
246255
retry_url = response.headers['location']
247256
return Response(self._access_url(retry_url))
248257
else:
249258
# For errors, just proxy DSS response
250-
return Response(response.text, status_code=response.status_code)
259+
return Response(response.data, status_code=response.status)
251260

252-
def _dss_get_file(self, file_uuid, replica, **kwargs):
261+
def _dss_get_file(self, file_uuid, replica, **kwargs) -> urllib3.BaseHTTPResponse:
253262
dss_params = {
254263
'replica': replica,
255264
**kwargs
256265
}
257266
url = self.dss_file_url(file_uuid)
258-
return requests.api.get(str(url), params=dss_params, allow_redirects=False)
267+
return self._http_client.request('GET', str(url),
268+
fields=dss_params,
269+
redirect=False)
259270

260271
@classmethod
261272
def dss_file_url(cls, file_uuid: str) -> mutable_furl:
@@ -267,7 +278,7 @@ class GatewayTimeoutError(ChaliceViewError):
267278

268279

269280
@dataclass
270-
class DRSObject:
281+
class DRSObject(HasCachedHttpClient):
271282
""""
272283
Used to build up a https://ga4gh.github.io/data-repository-service-schemas/docs/#_drsobject
273284
"""
@@ -293,7 +304,7 @@ def add_access_method(self,
293304
def to_json(self) -> JSON:
294305
args = _url_query(replica='aws', version=self.version)
295306
url = DRSController.dss_file_url(self.uuid).add(args=args)
296-
headers = requests.api.head(str(url)).headers
307+
headers = self._http_client.request('HEAD', str(url)).headers
297308
version = headers['x-dss-version']
298309
if self.version is not None:
299310
assert version == self.version

0 commit comments

Comments
 (0)