def FetchFileFromIsolatedServer(digest, name_space, isolated_server, http_client): """Sends retrieve request to isolated server and returns response content. Args: digest(str): Hash to file for retrieve request. name_space(str): Name space info for retrieve request. isolated_server(str): Host to isolate server. http_client(RetryHttpClient): http client to send the request. """ post_data = {'digest': digest, 'namespace': {'namespace': name_space}} url = '%s/_ah/api/isolateservice/v1/retrieve' % isolated_server content, error = http_client_util.SendRequestToServer( url, http_client, post_data=post_data) if error: return None, error json_content = json.loads(content) file_url = json_content.get('url') error = None assert file_url or json_content.get( 'content'), 'Response from isolate is missing both url and content.' if file_url: compressed_content, error = http_client_util.SendRequestToServer( file_url, http_client) else: compressed_content = base64.b64decode(json_content['content']) return zlib.decompress( compressed_content) if compressed_content else None, error
def ListTasks(host, tags, http_client): """List tasks based on tags. Args: tags (dict): A dict of tags. http_client (HttpClient): The httpclient object with which to make the server calls. Returns: items (list): A list of SwarmingTaskData for all tasks with queried tags. """ base_url = _LIST_TASK_URL % (host, ParametersToQueryString(tags, 'tags')) items_json = [] cursor = None while True: if not cursor: url = base_url else: url = base_url + '&cursor=%s' % urllib.quote(cursor) new_data, _ = http_client_util.SendRequestToServer(url, http_client) if not new_data: break new_data_json = json.loads(new_data) if new_data_json.get('items'): items_json.extend(new_data_json['items']) if new_data_json.get('cursor'): cursor = new_data_json['cursor'] else: break return [SwarmingTaskData(item) for item in items_json]
def GetSwarmingTaskRequest(host, task_id, http_client): """Returns request data of the given task.""" url = _TASK_ID_URL % (host, task_id) content, error = http_client_util.SendRequestToServer(url, http_client) if not error: json_data = json.loads(content) return SwarmingTaskRequest.FromSerializable(json_data) return None
def testSendRequestToServerRetryTimeout(self, mocked_post): mocked_post.return_value = (403, None, {}) content, error = http_client_util.SendRequestToServer( 'http://www.someurl.com', FinditHttpClient(403, None), post_data={ 'data': 'data' }) self.assertIsNone(content) self.assertEqual(403, error['code'], {})
def GetSwarmingTaskResultById(host, task_id, http_client): """Gets swarming result, checks state and returns outputs ref if needed.""" base_url = _TASK_RESULT_URL % (host, task_id) json_data = {} data, error = http_client_util.SendRequestToServer(base_url, http_client) if not error: json_data = json.loads(data) return json_data, error
def TriggerSwarmingTask(host, request, http_client): """Triggers a new Swarming task for the given request. Args: request (SwarmingTaskRequest): A Swarming task request. http_client (FinditHttpClient): An http client with automatic retry. """ response_data, error = http_client_util.SendRequestToServer( _NEW_TASK_URL % host, http_client, post_data=request.ToSerializable()) if not error: return json.loads(response_data)['task_id'], None return None, error
def GetBotCounts(host, dimensions, http_client): """Gets number of swarming bots for certain dimensions. Args: dimensions (dict): A dict of dimensions. http_client (HttpClient): The httpclient object with which to make the server calls. Returns: bot_counts(SwarmingBotCounts): Numbers of swarming bots in different states. """ url = _BOT_COUNT_URL % (host, ParametersToQueryString(dimensions, 'dimensions')) content, error = http_client_util.SendRequestToServer(url, http_client) if error or not content: return None return SwarmingBotCounts(json.loads(content))
def GetBotsByDimension(dimensions, http_client): url = BOT_LIST_URL % (swarming.SwarmingHost(), swarming_util.ParametersToQueryString( dimensions, 'dimensions')) content, error = http_client_util.SendRequestToServer(url, http_client) if error: logging.error( 'failed to list bots by dimension with %s, falling back to ' 'any selecting any bot', error) return [] if not content: logging.warning('got blank response from %s', url) return [] content_data = json.loads(content) return content_data.get('items', [])
def testSendRequestToServerSucceed(self, mocked_get): mocked_get.return_value = (200, 'content', {}) content, error = http_client_util.SendRequestToServer( 'http://www.someurl.com', FinditHttpClient()) self.assertEqual(content, 'content') self.assertIsNone(error)