Skip to content

Commit aac2fbc

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

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

src/azul/service/drs_controller.py

Lines changed: 19 additions & 12 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,9 @@
6064
IndexService,
6165
)
6266

67+
log = logging.getLogger(__name__)
6368

64-
class DRSController(ServiceController):
69+
class DRSController(ServiceController, HasCachedHttpClient):
6570

6671
@cached_property
6772
def _service(self) -> IndexService:
@@ -205,22 +210,22 @@ def get_object(self, file_uuid, query_params):
205210
# We only want direct URLs for Google
206211
extra_params = dict(query_params, directurl=access_method.replica == 'gcp')
207212
response = self._dss_get_file(file_uuid, access_method.replica, **extra_params)
208-
if response.status_code == 301:
213+
if response.status == 301:
209214
retry_url = response.headers['location']
210215
query = urllib.parse.urlparse(retry_url).query
211216
query = urllib.parse.parse_qs(query, strict_parsing=True)
212217
token = one(query['token'])
213218
# We use the encoded token string as the key for our access ID.
214219
access_id = encode_access_id(token, access_method.replica)
215220
drs_object.add_access_method(access_method, access_id=access_id)
216-
elif response.status_code == 302:
221+
elif response.status == 302:
217222
retry_url = response.headers['location']
218223
if access_method.replica == 'gcp':
219224
assert retry_url.startswith('gs:')
220225
drs_object.add_access_method(access_method, url=retry_url)
221226
else:
222227
# For errors, just proxy DSS response
223-
return Response(response.text, status_code=response.status_code)
228+
return Response(response.data, status_code=response.status)
224229
return Response(drs_object.to_json())
225230

226231
def get_object_access(self, access_id, file_uuid, query_params):
@@ -238,24 +243,26 @@ def get_object_access(self, access_id, file_uuid, query_params):
238243
'directurl': replica == 'gcp',
239244
'token': token
240245
})
241-
if response.status_code == 301:
246+
if response.status == 301:
242247
headers = {'retry-after': response.headers['retry-after']}
243248
# DRS says no body for 202 responses
244249
return Response(body='', status_code=202, headers=headers)
245-
elif response.status_code == 302:
250+
elif response.status == 302:
246251
retry_url = response.headers['location']
247252
return Response(self._access_url(retry_url))
248253
else:
249254
# For errors, just proxy DSS response
250-
return Response(response.text, status_code=response.status_code)
255+
return Response(response.data, status_code=response.status)
251256

252-
def _dss_get_file(self, file_uuid, replica, **kwargs):
257+
def _dss_get_file(self, file_uuid, replica, **kwargs) -> urllib3.BaseHTTPResponse:
253258
dss_params = {
254259
'replica': replica,
255260
**kwargs
256261
}
257262
url = self.dss_file_url(file_uuid)
258-
return requests.api.get(str(url), params=dss_params, allow_redirects=False)
263+
return self._http_client.request('GET', str(url),
264+
fields=dss_params,
265+
redirect=False)
259266

260267
@classmethod
261268
def dss_file_url(cls, file_uuid: str) -> mutable_furl:
@@ -267,7 +274,7 @@ class GatewayTimeoutError(ChaliceViewError):
267274

268275

269276
@dataclass
270-
class DRSObject:
277+
class DRSObject(HasCachedHttpClient):
271278
""""
272279
Used to build up a https://ga4gh.github.io/data-repository-service-schemas/docs/#_drsobject
273280
"""
@@ -293,7 +300,7 @@ def add_access_method(self,
293300
def to_json(self) -> JSON:
294301
args = _url_query(replica='aws', version=self.version)
295302
url = DRSController.dss_file_url(self.uuid).add(args=args)
296-
headers = requests.api.head(str(url)).headers
303+
headers = self._http_client.request('HEAD', url).headers
297304
version = headers['x-dss-version']
298305
if self.version is not None:
299306
assert version == self.version

0 commit comments

Comments
 (0)