Bug 1178474 - Have a common function for calling various endpoints.

This commit is contained in:
Vaibhav Agrawal 2015-06-25 16:59:56 -07:00
Родитель 92c66e2c1a
Коммит 096560caf5
5 изменённых файлов: 47 добавлений и 44 удалений

Просмотреть файл

@ -382,10 +382,11 @@ def mock_post_collection(monkeypatch, set_oauth_credentials):
collection_inst, timeout=None):
jsondata = collection_inst.to_json()
signed_uri = th_client._get_uri(project, collection_inst.endpoint_base,
data=jsondata, oauth_key=oauth_key,
oauth_secret=oauth_secret,
method='POST')
signed_uri = th_client._get_project_uri(project,
collection_inst.endpoint_base,
data=jsondata, oauth_key=oauth_key,
oauth_secret=oauth_secret,
method='POST')
getattr(TestApp(application), 'post')(
signed_uri,
params=jsondata,
@ -404,7 +405,7 @@ def mock_update_parse_status(monkeypatch, set_oauth_credentials):
timestamp = time.time()
jsondata = json.dumps({'parse_status': parse_status,
'parse_timestamp': timestamp})
signed_uri = th_client._get_uri(
signed_uri = th_client._get_project_uri(
project,
th_client.UPDATE_ENDPOINT.format(job_log_url_id),
data=jsondata, oauth_key=oauth_key, oauth_secret=oauth_secret,

Просмотреть файл

@ -20,10 +20,10 @@ def mock_post_json_data(monkeypatch, set_oauth_credentials):
thisone.protocol = 'http'
thisone.host = 'localhost'
uri = thisone._get_uri(project, endpoint, data=jsondata,
oauth_key=oauth_key,
oauth_secret=oauth_secret,
method='POST')
uri = thisone._get_project_uri(project, endpoint, data=jsondata,
oauth_key=oauth_key,
oauth_secret=oauth_secret,
method='POST')
resp = TestApp(application).post_json(
str(uri), params=json.loads(jsondata)

Просмотреть файл

@ -37,11 +37,11 @@ def post_collection(
)
jsondata = th_collection.to_json()
signed_uri = cli._get_uri(project, th_collection.endpoint_base,
data=jsondata,
oauth_key=credentials['consumer_key'],
oauth_secret=credentials['consumer_secret'],
method='POST')
signed_uri = cli._get_project_uri(project, th_collection.endpoint_base,
data=jsondata,
oauth_key=credentials['consumer_key'],
oauth_secret=credentials['consumer_secret'],
method='POST')
response = TestApp(application).post_json(
str(signed_uri), params=th_collection.get_collection_data(),

Просмотреть файл

@ -678,6 +678,9 @@ class TreeherderClient(object):
JOBS_ENDPOINT = 'jobs'
ARTIFACTS_ENDPOINT = 'artifact'
OPTION_COLLECTION_HASH_ENDPOINT = 'optioncollectionhash'
REPOSITORY_ENDPOINT = 'repository'
JOBGROUP_ENDPOINT = 'jobgroup'
JOBTYPE_ENDPOINT = 'jobtype'
def __init__(
self, protocol='https', host='treeherder.mozilla.org',
@ -696,8 +699,8 @@ class TreeherderClient(object):
self.protocol = protocol
self.timeout = timeout
def _get_uri(self, project, endpoint, data=None, oauth_key=None,
oauth_secret=None, method='GET'):
def _get_project_uri(self, project, endpoint, data=None, oauth_key=None,
oauth_secret=None, method='GET'):
uri = '{0}://{1}/api/project/{2}/{3}/'.format(
self.protocol, self.host, project, endpoint
@ -709,11 +712,21 @@ class TreeherderClient(object):
return uri
def _get_json(self, project, endpoint, timeout, **params):
def _get_uri(self, endpoint):
uri = '{0}://{1}/api/{2}'.format(
self.protocol, self.host, endpoint)
return uri
def _get_json(self, endpoint, timeout, project=None, **params):
if timeout is None:
timeout = self.timeout
uri = self._get_uri(project, endpoint)
if project is None:
uri = self._get_uri(endpoint)
else:
uri = self._get_project_uri(project, endpoint)
resp = requests.get(uri, timeout=timeout, params=params)
resp.raise_for_status()
return resp.json()
@ -727,8 +740,9 @@ class TreeherderClient(object):
raise TreeherderClientError("Must provide oauth key and secret "
"to post to treeherder!", [])
uri = self._get_uri(project, endpoint, data=jsondata, oauth_key=oauth_key,
oauth_secret=oauth_secret, method='POST')
uri = self._get_project_uri(project, endpoint, data=jsondata,
oauth_key=oauth_key, oauth_secret=oauth_secret,
method='POST')
resp = requests.post(uri, data=jsondata,
headers={'Content-Type': 'application/json'},
@ -747,11 +761,9 @@ class TreeherderClient(object):
...
}
"""
resp = requests.get('{0}://{1}/api/optioncollectionhash'.format(
self.protocol, self.host), timeout=self.timeout)
resp.raise_for_status()
resp = self._get_json(self.OPTION_COLLECTION_HASH_ENDPOINT, None)
ret = {}
for result in resp.json():
for result in resp:
ret[result['option_collection_hash']] = result['options']
return ret
@ -767,10 +779,7 @@ class TreeherderClient(object):
...
]
"""
response = requests.get('{0}://{1}/api/repository/'.format(
self.protocol, self.host), timeout=self.timeout)
response.raise_for_status()
return response.json()
return self._get_json(self.REPOSITORY_ENDPOINT, None)
def get_job_groups(self):
"""
@ -785,10 +794,7 @@ class TreeherderClient(object):
...
}
"""
resp = requests.get('{0}://{1}/api/jobgroup'.format(
self.protocol, self.host), timeout=self.timeout)
resp.raise_for_status()
return resp.json()
return self._get_json(self.JOBGROUP_ENDPOINT, None)
def get_job_types(self):
"""
@ -805,10 +811,7 @@ class TreeherderClient(object):
...
}
"""
resp = requests.get('{0}://{1}/api/jobtype'.format(
self.protocol, self.host), timeout=self.timeout)
resp.raise_for_status()
return resp.json()
return self._get_json(self.JOBTYPE_ENDPOINT, None)
def get_resultsets(self, project, **params):
"""
@ -819,7 +822,7 @@ class TreeherderClient(object):
:param project: project (repository name) to query data for
:param params: keyword arguments to filter results
"""
response = self._get_json(project, self.RESULTSET_ENDPOINT, None, **params)
response = self._get_json(self.RESULTSET_ENDPOINT, None, project, **params)
return response["results"]
def get_jobs(self, project, **params):
@ -829,7 +832,7 @@ class TreeherderClient(object):
:param project: project (repository name) to query data for
:param params: keyword arguments to filter results
"""
response = self._get_json(project, self.JOBS_ENDPOINT, None, **params)
response = self._get_json(self.JOBS_ENDPOINT, None, project, **params)
return response["results"]
def get_artifacts(self, project, **params):
@ -839,7 +842,7 @@ class TreeherderClient(object):
:param project: project (repository name) to query for
:param params: keyword arguments to filter results
"""
response = self._get_json(project, self.ARTIFACTS_ENDPOINT, None, **params)
response = self._get_json(self.ARTIFACTS_ENDPOINT, None, project, **params)
return response
def post_collection(self, project, oauth_key, oauth_secret,

Просмотреть файл

@ -118,7 +118,7 @@ class PerfherderClient(TreeherderClient):
Gets a set of performance signatures associated with a project and time range
'''
return PerformanceSignatureCollection(self._get_json(
project, self.PERFORMANCE_SERIES_SUMMARY_ENDPOINT, timeout,
self.PERFORMANCE_SERIES_SUMMARY_ENDPOINT, timeout, project,
interval=time_interval))
def get_performance_signature_properties(self, project, signature,
@ -126,9 +126,8 @@ class PerfherderClient(TreeherderClient):
'''
Gets the set of properties associated with a specific signature
'''
property_list = self._get_json(project,
self.SIGNATURE_PROPERTIES_ENDPOINT,
timeout, signatures=signature)
property_list = self._get_json(self.SIGNATURE_PROPERTIES_ENDPOINT,
timeout, project, signatures=signature)
if len(property_list) != 1:
raise TreeherderClientError(
"Expected 1 result for call to '{0}', got '{1}'".format(
@ -143,7 +142,7 @@ class PerfherderClient(TreeherderClient):
'''
Gets a list of series objects associated with a set of signatures
'''
results = self._get_json(project, self.PERFORMANCE_DATA_ENDPOINT, timeout,
results = self._get_json(self.PERFORMANCE_DATA_ENDPOINT, timeout, project,
signatures=signature_list,
interval_seconds=time_interval)
if len(results) != len(signature_list):