Skip to content

Commit a9a58fd

Browse files
authored
Merge pull request dcos#976 from mellenburg/revert_request_wrapper
Revert "Abstract wrapper class"
2 parents 1f46233 + cb81bf6 commit a9a58fd

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

test_util/cluster_api.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import functools
23
import logging
34
import os
45
from urllib.parse import urlparse
@@ -315,7 +316,7 @@ def get_node_url(self, node, port=None):
315316
netloc = '{}:{}'.format(node, port)
316317
return '{}://{}'.format(self.scheme, netloc)
317318

318-
def request_wrapper(self, request_fn, path, host_url=None, node=None, port=None, **kwargs):
319+
def cluster_api_request(self, method, path, host_url=None, node=None, port=None, **kwargs):
319320
"""
320321
Performs the same specialized request as ClusterApi but allows setting node
321322
Args:
@@ -326,26 +327,31 @@ def request_wrapper(self, request_fn, path, host_url=None, node=None, port=None,
326327
if node is not None:
327328
assert host_url is None, 'Cannot set both node and host_url'
328329
host_url = self.get_node_url(node, port=port)
329-
return super().request_wrapper(request_fn, path, host_url=host_url, **kwargs)
330-
331-
def get_client(self, path):
332-
new_client = test_util.helpers.ApiClient(
330+
return self.api_request(method, path, host_url=host_url, **kwargs)
331+
332+
get = functools.partialmethod(cluster_api_request, 'get')
333+
post = functools.partialmethod(cluster_api_request, 'post')
334+
put = functools.partialmethod(cluster_api_request, 'put')
335+
delete = functools.partialmethod(cluster_api_request, 'delete')
336+
options = functools.partialmethod(cluster_api_request, 'options')
337+
head = functools.partialmethod(cluster_api_request, 'head')
338+
patch = functools.partialmethod(cluster_api_request, 'patch')
339+
delete = functools.partialmethod(cluster_api_request, 'delete')
340+
341+
def get_client(self, path, default_headers=None):
342+
return test_util.helpers.ApiClient(
333343
default_host_url=self.dcos_url,
334344
api_base=path,
335345
ca_cert_path=self.ca_cert_path,
336-
default_headers=self.default_headers)
337-
# give spawned clients the node= reqest option
338-
new_client.request_wrapper = self.request_wrapper
346+
default_headers=self.default_headers if default_headers is None else default_headers)
339347

340348
@property
341349
def marathon(self):
342-
marathon_client = test_util.marathon.Marathon(
350+
return test_util.marathon.Marathon(
343351
default_host_url=self.dcos_url,
344352
default_os_user=self.default_os_user,
345353
default_headers=self.default_headers,
346354
ca_cert_path=self.ca_cert_path)
347-
# give spawned clients the node= request option
348-
marathon_client.request_wrapper = self.request_wrapper
349355

350356
@property
351357
def metronome(self):

test_util/helpers.py

+13-19
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def authenticate(self, cluster):
4747
logging.info('Authentication successful')
4848

4949

50-
class ApiClient():
50+
class ApiClient:
5151

5252
def __init__(self, default_host_url, api_base, default_headers=None, ca_cert_path=None):
5353
self.default_host_url = default_host_url
@@ -57,7 +57,7 @@ def __init__(self, default_host_url, api_base, default_headers=None, ca_cert_pat
5757
self.default_headers = default_headers
5858
self.ca_cert_path = ca_cert_path
5959

60-
def request_wrapper(self, request_fn, path, host_url=None, **kwargs):
60+
def api_request(self, method, path, host_url=None, **kwargs):
6161
"""
6262
Makes a request with default headers + user auth headers if necessary
6363
If self.ca_cert_path is set, this method will pass it to requests
@@ -78,25 +78,19 @@ def request_wrapper(self, request_fn, path, host_url=None, **kwargs):
7878

7979
request_url = path_join(host_url if host_url else self.default_host_url, path)
8080
headers.update(kwargs.pop('headers', {}))
81-
logging.info('Request method {}: {}'.format(request_fn.__name__, request_url))
81+
logging.info('Request method {}: {}'.format(method, request_url))
8282
logging.debug('Reqeust kwargs: {}'.format(kwargs))
8383
logging.debug('Request headers: {}'.format(headers))
84-
return request_fn(request_url, headers=headers, **kwargs)
85-
86-
def wrapped_request(self, request_fn, *args, **kwargs):
87-
"""Thin wrapper to allow wrapping requests dynamically by mapping
88-
a function to self.request_wrapper
89-
"""
90-
return self.request_wrapper(request_fn, *args, **kwargs)
91-
92-
get = functools.partialmethod(wrapped_request, requests.request.get)
93-
post = functools.partialmethod(wrapped_request, requests.request.post)
94-
put = functools.partialmethod(wrapped_request, requests.request.put)
95-
delete = functools.partialmethod(wrapped_request, requests.request.delete)
96-
options = functools.partialmethod(wrapped_request, requests.request.options)
97-
head = functools.partialmethod(wrapped_request, requests.request.head)
98-
patch = functools.partialmethod(wrapped_request, requests.request.patch)
99-
delete = functools.partialmethod(wrapped_request, requests.request.delete)
84+
return requests.request(method, request_url, headers=headers, **kwargs)
85+
86+
get = functools.partialmethod(api_request, 'get')
87+
post = functools.partialmethod(api_request, 'post')
88+
put = functools.partialmethod(api_request, 'put')
89+
delete = functools.partialmethod(api_request, 'delete')
90+
options = functools.partialmethod(api_request, 'options')
91+
head = functools.partialmethod(api_request, 'head')
92+
patch = functools.partialmethod(api_request, 'patch')
93+
delete = functools.partialmethod(api_request, 'delete')
10094

10195

10296
def retry_boto_rate_limits(boto_fn, wait=2, timeout=60 * 60):

0 commit comments

Comments
 (0)